@nativescript/apple-pay
为 NativeScript iOS 应用程序提供 Apple Pay 支持的插件。
npm i --save @nativescript/apple-pay

@nativescript/apple-pay

ns plugin add @nativescript/apple-pay

使用

为了让 Apple Pay 在您的 iOS 应用程序中工作,您需要完成以下步骤。这些步骤也已在Apple PassKit 文档中概述。

  • 创建一个商户 ID。

  • 创建一个支付处理证书。

  • 在 Xcode 中启用 Apple Pay。

按照配置 Apple Pay(iOS,watchOS)中的说明进行操作,该说明将引导您创建以下内容

  • 商户 ID。您向 Apple 注册的一个标识符,用于唯一标识您的业务作为能够接受支付的商户。此 ID 永不失效,可以在多个网站和 iOS 应用中使用。有关设置步骤,请参阅创建商户标识符

  • 支付处理证书。与您的商户 ID 相关的证书,用于保护交易数据。Apple Pay 服务器使用证书的公钥来加密支付数据。您(或您的支付服务提供商)使用私钥来解密数据以处理支付。请参阅创建支付处理证书以获取设置步骤。

  • 最后,您需要在 Xcode 项目中启用 Apple Pay 功能。请参阅启用 Apple Pay以获取设置步骤。

有关配置步骤的视频,请参阅:配置您的开发者帐户以使用 Apple Pay

一旦完成您的 Apple 开发者帐户的配置,您就可以在 iOS 应用程序中使用 PassKit 框架进行 Apple Pay。

<ios>
<ApplePayBtn
tap="onApplePayTap"
buttonType="InStore"
>
</ApplePayBtn>
</ios>
import { ApplePayBtn, ApplePayContactFields, ApplePayEvents, ApplePayItems, ApplePayMerchantCapability, ApplePayNetworks, ApplePayPaymentItemType, ApplePayRequest, ApplePayTransactionStatus, AuthorizePaymentEventData } from '@nativescript/apple-pay';

export function onApplePayTap() {
try {
// just ensuring this runs only on iOS
if (global.isIOS) {
const applePayBtn = args.object as ApplePayBtn;

// setup the event listeners for the delegate for apple pay for our button
applePayBtn.once(ApplePayEvents.DidAuthorizePaymentHandler, async (args: AuthorizePaymentEventData) => {
console.log(ApplePayEvents.DidAuthorizePaymentHandler);

// this is where you do backend processing with your payment provider (Stripe, PayPal, etc.)
// this payload is just a sample, your payload to a provider will likely be different
// you can see here how to access the encrypted values from Apple Pay inside the `args.data.paymentData`
const payloadToBackend = {
transaction_type: 'purchase',
merchant_ref: args.data.paymentData.header.transactionId,
method: '3DS',
'3DS': {
merchantIdentifier: <SomeIdentifierFromPaymentProvider>,
data: args.data.paymentData.data,
signature: args.data.paymentData.signature,
version: args.data.paymentData.version,
header: args.data.paymentData.header
}
};

const result = await someHttpMethodToYourProviderBackend(payloadToBackend);

if (result && result.value === true) {
// need this to call when the payment is successful to close the payment sheet correctly on iOS
args.completion(ApplePayTransactionStatus.Success);
// now you can follow through with your user flow since the transactin has been successful with your provider
} else {
// payment failed on the backend, so show the FAILURE to close the Apple Pay sheet
args.completion(ApplePayTransactionStatus.Failure);
}
});

// these are the items your customer is paying for
const paymentItems = [
{
amount: 20.50,
label: 'Balance',
type: ApplePayPaymentItemType.Final,
},
] as ApplePayItems[];

const request = {
paymentItems,
merchantId: <Your_Apple_Pay_Merchant_ID>, // the merchant ID for this app
merchantCapabilities: ApplePayMerchantCapability.ThreeDS,
countryCode: 'US',
currencyCode: 'USD',
shippingContactFields: [ApplePayContactFields.Name, ApplePayContactFields.PostalAddress],
billingContactFields: [ApplePayContactFields.Name, ApplePayContactFields.PostalAddress],
supportedNetworks: [ApplePayNetworks.Amex, ApplePayNetworks.Visa, ApplePayNetworks.Discover, ApplePayNetworks.MasterCard],
} as ApplePayRequest;

// `createPaymentRequest` will call into the Apple Pay SDK and present the user with the payment sheet for the configuration provided
await applePayBtn.createPaymentRequest(request).catch((err) => {
console.log('Apple Pay Error', err);
});
}
} catch (error) {
console.log(error);
}
}

API

方法

名称 描述
createPaymentRequest(request: ApplePayRequest) 创建 Apple Pay 支付请求并向用户展示支付表单。

枚举

ApplePayEvents

描述
DidAuthorizePaymentHandler 通知代理用户已授权支付请求并请求结果。
AuthorizationDidFinish 通知代理支付授权已完成。

ApplePayContactFields

描述
EmailAddress 指示电子邮件地址字段。
名称 指示姓名字段。
PhoneNumber 指示电话号码字段。
PhoneticName 指示音译姓名字段。
PostalAddress 指示邮政地址字段。

ApplePayNetworks

Amex
CarteBancaire
CarteBancaires
ChinaUnionPay
Discover
Eftpos
Electron
Elo
IDCredit
Interac
Jcb
Mada
Maestro
MasterCard
PrivateLabel
QuicPay
Suica
Visa
VPay

ApplePayMerchantCapability

ThreeDS PKMerchantCapability.Capability3DS
EMV PKMerchantCapability.CapabilityEMV
Credit PKMerchantCapability.CapabilityCredit
Debit PKMerchantCapability.CapabilityDebit

ApplePayMerchantCapaApplePayTransactionStatusbility

成功 PKPaymentAuthorizationStatus.Success
失败 PKPaymentAuthorizationStatus.Failure
无效的计费邮政地址 PKPaymentAuthorizationStatus.InvalidBillingPostalAddress
无效的送货地址 PKPaymentAuthorizationStatus.InvalidShippingPostalAddress
无效的送货联系方式 PK支付授权状态.无效的送货联系方式
需要PIN码 PK支付授权状态.需要PIN码
PIN码错误 PK支付授权状态.PIN码错误
PIN码锁定 PK支付授权状态.PIN码锁定,

Apple Pay支付项目类型

最终 PK支付摘要项目类型.最终
挂起 PK支付摘要项目类型.挂起

Apple Pay按钮类型

普通 PK支付按钮类型.普通
购买 PK支付按钮类型.购买
预订 PK支付按钮类型.预订
结账 PK支付按钮类型.结账
捐赠 PK支付按钮类型.捐赠
店内 PK支付按钮类型.预订
订阅 PK支付按钮类型.订阅

Apple Pay按钮样式

白色 PK支付按钮样式.白色
白色轮廓 PK支付按钮样式.白色轮廓
黑色 PK支付按钮样式.黑色

接口

Apple Pay请求

interface ApplePayRequest {
paymentItems: Array<ApplePayItems>;
merchantId: string; // the merchant ID for this app
merchantCapabilities: number;
countryCode: string;
currencyCode: string;
supportedNetworks: Array<ApplePayNetworks>;
billingContactFields?: Array<ApplePayContactFields>;
shippingContactFields?: Array<ApplePayContactFields>;
shippingMethods?: Array<ApplePayShippingMethods>;
}

Apple Pay项目

interface ApplePayItems {
label: string;
amount: number;
type: ApplePayPaymentItemType;
}

授权支付事件数据

interface AuthorizePaymentEventData extends EventData {
eventName: string;
object: any;
data?: {
payment: PKPayment;
token: PKPaymentToken;
paymentData: ApplePayPaymentData;
billingAddress;
billingContact: PKContact;
shippingAddress;
shippingContact: PKContact;
shippingMethod: PKShippingMethod;
};
completion: (status: ApplePayTransactionStatus) => void;
}

授权完成事件数据

interface AuthorizationDidFinishEventData extends EventData {
eventName: string;
object: any;
}

Apple Pay支付数据

interface ApplePayPaymentData {
/**
* Encrypted payment data.
*/
data;

/**
* Additional version-dependent information used to decrypt and verify the payment.
*/
header;

/**
* Signature of the payment and header data. The signature includes the signing certificate, its intermediate CA certificate, and information about the signing algorithm.
*/
signature;

/**
* Version information about the payment token.
* The token uses EC_v1 for ECC-encrypted data, and RSA_v1 for RSA-encrypted data.
*/
version: string;
}

许可

Apache许可证版本2.0