Skip to content

第三步 绑定支付和监听事件

绑定支付事件

pay(method, params)

说明:点击支付的时候调用此方法

Method 详情
参数说明
method支付方式,如"CreditCard"、"PIX"等
Params 详情
参数类型必填说明
billingAddressObject必填注意:国家字段必填 账单信息(BillingAddress详情)
shippingAddressObject选填邮寄信息(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 详情
参数类型说明
codeNumber(3)返回码,详见附录
uniqueIdString(32)系统返回唯一交易 ID
redirectUrlString(255)跳转到收银台的URL
messageString(255)系统返回信息
statusString(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);
};