ApplePay API接入流程备份
对接流程
1. Apple Pay 证书
s2s方式截图applepay需要在网站的指定路径下放入证书(请联系UseePay运营提供)
https://网站域名/.well-known/apple-developer-merchantid-domain-association.txt
请注意放入证书后需要再次联系UseePay激活证书
2. 前端
2.1 前端引入Apple Pay
大致流程如下,仅供参考(因为Apple Pay文档可能会升级,请您参考上述Apple Pay官网文档)
2.1.1 在网站引入apple pay
<script src="https://applepay.cdn-apple.com/jsapi/v1.1.0/apple-pay-sdk.js"></script>
2.1.2 渲染Apple Pay button
if (window.ApplePaySession) {
var merchantIdentifier = 'YOUR MERCHANT IDENTIFIER';
var promise = ApplePaySession.canMakePaymentsWithActiveCard(merchantIdentifier);
promise.then(function (canMakePayments) {
if (canMakePayments)
// Display Apple Pay button here.
}); }
merchantIdentifier 取值说明
环境 | 值 |
---|---|
sandbox | merchant.com.sandbox.useepay |
prod | merchant.com.gateway.useepay |
一.获取ApplePay验证文件
请根据你想要添加ApplePay支付按钮的域名*发送给UseePay工作人员,将获得的证书放置在相应页面的指定路径下,如https://checkou.useepay.com/.well-known/apple-developer-merchantid-domain-association.txt
完成后联系UseePay的工作人员,进行网站的绑定。
二.挂载Apple Pay SDK
三.检查Apple Pay是否可用
if (window.ApplePaySession) {
var merchantIdentifier = 'YOUR MERCHANT IDENTIFIER';
var promise = ApplePaySession.canMakePaymentsWithActiveCard(merchantIdentifier);
promise.then(function (canMakePayments) {
if (canMakePayments)
// Display Apple Pay button here.
}); }
merchantIdentifier 沙盒环境使用merchant.com.sandbox.useepay
生产环境使用merchant.com.gateway.useepay
四.构建ApplePay Request
const request = {
"countryCode": "US",
"currencyCode": "",
"merchantCapabilities": [
"supports3DS",
"supportsDebit",
"supportsCredit"
],
"supportedNetworks": [
"visa",
"masterCard",
"amex",
"discover",
],
"total": {
"label": "",
"amount": "",
"type": "final"
}
}
其中currencyCode,label,amount是当前交易币种,交易主体,交易金额,请如实填写
五.构建ApplePaySession
const session = new ApplePaySession(5, request);
参照:https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/creating_an_apple_pay_session
后续使用此session
三.获取MerchantSession
session.onvalidatemerchant = async event => {
// Call your own server to request a new merchant session.
const merchantSession = await Api.merchantSession(sessionRequest);
session.completeMerchantValidation(merchantSession);
};
获取merchantSession 请参照https://www.apifox.cn/web/project/2038036/apis/api-67411929
六. 后续session动作
session.onpaymentmethodselected = event => {
event.eventName = "onpaymentmethodselected"
Api.applepayLogEvent(event);
// Define ApplePayPaymentMethodUpdate based on the selected payment method.
// No updates or errors are needed, pass an empty object.
var newTotal = {
"label": //交易网址//,
"amount": //交易金额//,
"type": "final"
};
var newLineItems = [
// {
// "type": "final",
// "label": "Sales Tax",
// "amount": "1.00"
// },
// {
// "type": "final",
// "label": "Shipping",
// "amount": "0.00"
// }
];
var update = {
newTotal: newTotal,
newLineItems: newLineItems
};
session.completePaymentMethodSelection( update );
};
session.onshippingmethodselected = event => {
event.eventName = "onshippingmethodselected"
// Define ApplePayShippingMethodUpdate based on the selected shipping method.
// No updates or errors are needed, pass an empty object.
const update = {};
session.completeShippingMethodSelection(update);
};
session.onshippingcontactselected = event => {
event.eventName = "onshippingcontactselected"
Api.applepayLogEvent(event);
// Define ApplePayShippingContactUpdate based on the selected shipping contact.
const update = {};
session.completeShippingContactSelection(update);
};
七.进行支付
session.onpaymentauthorized = async event => {
const applepayPaymentRequest = buildApplepayPaymentRequest();
const applepayPaymentResponse = await Api.applepayPayment(applepayPaymentRequest);
session.completePayment(applepayPaymentResponse.applePayPaymentAuthorizationResult);
// 处理apple pay支付结果
processApplepayPaymentResponse(applepayPaymentResponse);
};
参照https://www.apifox.cn/link/project/2038036/apis/api-67385706