接入教程
1. 注册Adyen商户账号并获取API密钥
2. 根据文档配置支付环境参数
3. 调用支付发起接口创建交易
4. 处理支付回调验证交易状态
5. 使用结算接口完成资金处理
使用Python发起支付请求
import requests
import json
# Adyen支付API端点
url = 'https://api.adyen.com/v1/payments'
# 请求头,包含API密钥
headers = {
'X-API-Key': 'YOUR_API_KEY',
'Content-Type': 'application/json'
}
# 示例支付请求载荷
payload = {
"amount": {
"value": 1000,
"currency": "EUR"
},
"reference": "your_unique_reference",
"paymentMethod": {
"type": "scheme",
"number": "4111111111111111",
"expiryMonth": "03",
"expiryYear": "2030",
"holderName": "John Smith",
"cvc": "737"
},
"returnUrl": "https://your-return-url.com"
}
# 发起POST请求
response = requests.post(url, headers=headers, data=json.dumps(payload))
# 打印响应
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}")
使用PHP处理支付通知
<?php
// Adyen支付API端点
$url = 'https://api.adyen.com/v1/payments';
// API密钥
$apiKey = 'YOUR_API_KEY';
// 请求头
$headers = [
'X-API-Key: ' . $apiKey,
'Content-Type: application/json'
];
// 示例支付请求数据
$data = [
'amount' => [
'value' => 1000,
'currency' => 'EUR'
],
'reference' => 'your_unique_reference',
'paymentMethod' => [
'type' => 'scheme',
'number' => '4111111111111111',
'expiryMonth' => '03',
'expiryYear' => '2030',
'holderName' => 'John Smith',
'cvc' => '737'
],
'returnUrl' => 'https://your-return-url.com'
];
// 初始化cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 执行请求
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭cURL
curl_close($ch);
// 输出结果
echo "状态码: " . $httpCode . "\n";
echo "响应内容: " . $response . "\n";
?>
使用JavaScript获取支付方式
// Adyen支付API端点
const url = 'https://api.adyen.com/v1/paymentMethods';
// API密钥
const apiKey = 'YOUR_API_KEY';
// 请求头
const headers = {
'X-API-Key': apiKey,
'Content-Type': 'application/json'
};
// 请求载荷
const payload = {
merchantAccount: 'YourMerchantAccount',
countryCode: 'NL',
amount: {
value: 1000,
currency: 'EUR'
},
channel: 'Web'
};
// 发起POST请求
fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log('可用的支付方式:', data.paymentMethods);
})
.catch(error => {
console.error('请求失败:', error);
});
常见问题
如何获取Adyen支付API的API密钥?
您需要先在Adyen平台上注册商户账户。登录商户后台后,进入“Developers”或“API Credentials”部分,可以创建和管理API密钥。请妥善保管您的API密钥,不要在前端代码中硬编码或公开。
支付请求中哪些字段是必需的?
一个基本的支付请求通常必须包含以下字段:amount(金额,包含value和currency)、reference(商户唯一参考号)、paymentMethod(支付方式详情,如卡号、有效期等)以及returnUrl(支付完成后的返回URL)。具体必需字段可能因支付方式而异,请参考官方文档。
如何处理支付结果通知?
Adyen支付API支持通过webhook发送支付结果通知。您需要在商户后台配置一个通知接收URL。当支付状态发生变化时,Adyen会向该URL发送一个包含支付详情的POST请求。您的服务器需要验证该请求的签名(使用HMAC密钥),然后根据通知内容更新订单状态。
Aitishiku.com