第三步 绑定支付和监听事件
绑定支付事件
pay(method, params)
说明:点击支付的时候调用此方法
Method 详情
| 参数 | 说明 |
|---|---|
method | 支付方式,如"CreditCard"、"PIX"等 |
Params 详情
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
billingAddress | Object | 必填 | 注意:国家字段必填 账单信息(BillingAddress详情) |
shippingAddress | Object | 选填 | 邮寄信息(ShippingAddress详情) |
使用实例
javascript
element.addEventListener("click", () => {
loading = true;
testPayment.pay("CreditCard", {
billingAddress: {
firstName: "Pinkie",
lastName: "Olson",
country: "US",
email: "[email protected]",
city: "Lake Kacie",
address: "3986 Grimes Islands",
phone: "+919876543210",
zipCode: "36000",
},
shippingAddress: {
firstName: "Cecil",
lastName: "McDermott",
country: "US",
email: "[email protected]",
city: "New Mylenemouth",
address: "350 Bradtke Courts",
phone: "+919876543210",
state: "AL",
zipCode: "36000",
},
});
});onPaymentCompleted(response)
说明:当支付完成后调用此方法
使用实例
javascript
testPayment.onPaymentCompleted = response => {
loading = false;
if (response.code === 301) {
window.location.href = response.redirectUrl;
return;
}
// TODO: 处理支付成功和失败提示的情况
};Response 详情
| 参数 | 类型 | 说明 |
|---|---|---|
code | Number(3) | 返回码,详见附录 |
uniqueId | String(32) | 系统返回唯一交易 ID |
redirectUrl | String(255) | 跳转到收银台的URL |
message | String(255) | 系统返回信息 |
status | String(16) | 请勿使用,将要在下个版本废弃 |
请求成功
json
{
"message": "Approved",
"code": 100,
"uniqueId": "2051934696534740993"
}请求成功后,使用redirectUrl跳转3D或本地支付页面
json
{
"message": "Redirect",
"redirectUrl": "https://test.cc/redirect/test/3d/81d4cbd6ef504558b73318466d26d1a2",
"code": 301,
"uniqueId": "2051940804829646849"
}请求失败
json
{
"message": "transaction failed",
"code": 101,
"uniqueId": "2051943610370334721"
}绑定其他事件
onInitCompleted(event)
说明:当支付初始化完成后调用此方法
Event 详情
| 参数 | 说明 |
|---|---|
event | 固定值"initialization_completed" |
methods | 用于查看支持的支付方式,如["CreditCard","PIX"] |
使用实例
javascript
testPayment.onInitCompleted = event => {
// TODO: 处理Loading状态
loading = false;
};onError(error)
说明:当支付初始化过程中发生错误时调用此方法
Error 详情
| 参数 | 说明 |
|---|---|
event | 固定值"error" |
message | 说明错误原因 |
使用实例
javascript
testPayment.onError = error => {
// TODO: 用于查看初始化过程中发生错误的详细信息,从而调整支付配置
console.error(error);
};