接入教程
1. 访问Square开发者网站注册账号
2. 创建应用并获取API密钥和访问令牌
3. 选择适合的SDK(如Java、Python)集成到项目中
4. 配置支付参数并测试交易流程
5. 接入Webhook接收实时交易通知
6. 上线前完成沙箱环境全面测试
使用Python创建付款
python
import requests
url = 'https://api.example.com/v2/payments'
headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
data = {
'source_id': 'cnon:card-nonce-ok',
'idempotency_key': 'unique_key_123',
'amount_money': {
'amount': 100,
'currency': 'USD'
}
}
response = requests.post(url, json=data, headers=headers)
print(response.json())
使用PHP处理退款
php
<?php
$url = 'https://api.example.com/v2/refunds';
$apiKey = 'YOUR_API_KEY';
$data = array(
'payment_id' => 'payment_id_123',
'idempotency_key' => 'refund_key_456',
'amount_money' => array(
'amount' => 50,
'currency' => 'USD'
)
);
$options = array(
'http' => array(
'header' => "Authorization: Bearer $apiKey\r\nContent-Type: application/json",
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
?>
使用JavaScript获取交易记录
javascript
const url = 'https://api.example.com/v2/payments';
const apiKey = 'YOUR_API_KEY';
async function fetchTransactions() {
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error fetching transactions:', error);
}
}
fetchTransactions();
常见问题
如何获取API密钥?
请登录Square开发者平台,在应用管理页面创建应用并生成API密钥。确保妥善保管密钥,不要泄露给第三方。
支持哪些支付方式?
Square API支持信用卡、借记卡、数字钱包(如Apple Pay、Google Pay)等多种支付方式,具体请参考官方文档。
如何处理支付回调?
支付完成后,Square会发送Webhook通知到您预设的回调地址。请确保服务器能正确处理POST请求并验证签名。
Aitishiku.com