@nativescript/apple-pay
ns plugin add @nativescript/apple-pay
Usage
In order for Apple Pay to work in your iOS application you will need to complete the following steps. These steps are also outlined in the Apple PassKit documentation.
Follow the instructions in Configure Apple Pay (iOS, watchOS), which guide you to create the following:
-
Merchant ID.
An identifier you register with Apple that uniquely identifies your business as a merchant able to accept payments. This ID never expires, and can be used in multiple websites and iOS apps. See Create a merchant identifier for the setup steps.
-
Payment Processing Certificate.
A certificate associated with your merchant ID, used to secure transaction data. Apple Pay servers use the certificateās public key to encrypt payment data. You (or your payment service provider) use the private key to decrypt the data to process payments. See Create a payment processing certificate for the setup steps.
-
Finally, you need to enable the Apple Pay capability in your Xcode project.
See Enable Apple Pay for the setup steps.
For a video showing the configuration steps, see: Configuring Your Developer Account for Apple Pay.
Once that configuration is done for your Apple developer account, you will be able to use the PassKit framework for Apple Pay inside your iOS application.
<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 {
if (global.isIOS) {
const applePayBtn = args.object as ApplePayBtn;
applePayBtn.once(ApplePayEvents.DidAuthorizePaymentHandler, async (args: AuthorizePaymentEventData) => {
console.log(ApplePayEvents.DidAuthorizePaymentHandler);
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) {
args.completion(ApplePayTransactionStatus.Success);
} else {
args.completion(ApplePayTransactionStatus.Failure);
}
});
const paymentItems = [
{
amount: 20.50,
label: 'Balance',
type: ApplePayPaymentItemType.Final,
},
] as ApplePayItems[];
const request = {
paymentItems,
merchantId: <Your_Apple_Pay_Merchant_ID>,
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;
await applePayBtn.createPaymentRequest(request).catch((err) => {
console.log('Apple Pay Error', err);
});
}
} catch (error) {
console.log(error);
}
}
API
Methods
createPaymentRequest(request: ApplePayRequest) | Creates the Apple Pay payment request and presents the user with the payment sheet. |
Enums
ApplePayEvents
DidAuthorizePaymentHandler | Tells the delegate that the user has authorized the payment request and asks for a result. |
AuthorizationDidFinish | Tells the delegate that payment authorization has completed. |
ApplePayContactFields
EmailAddress | Indicates an email address field. |
Name | Indicates a name field. |
PhoneNumber | Indicates a phone number field. |
PhoneticName | Indicates a phonetic name field. |
PostalAddress | Indicates a postal address field. |
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
Success | PKPaymentAuthorizationStatus.Success |
Failure | PKPaymentAuthorizationStatus.Failure |
InvalidBillingPostalAddress | PKPaymentAuthorizationStatus.InvalidBillingPostalAddress |
InvalidShippingPostalAddress | PKPaymentAuthorizationStatus.InvalidShippingPostalAddress |
InvalidShippingContact | PKPaymentAuthorizationStatus.InvalidShippingContact |
PINRequired | PKPaymentAuthorizationStatus.PINRequired |
PINIncorrect | PKPaymentAuthorizationStatus.PINIncorrect |
PINLockout | PKPaymentAuthorizationStatus.PINLockout, |
ApplePayPaymentItemType
Final | PKPaymentSummaryItemType.Final |
Pending | PKPaymentSummaryItemType.Pending |
ApplePayButtonType
Plain | PKPaymentButtonType.Plain |
Buy | PKPaymentButtonType.Buy |
Book | PKPaymentButtonType.Book |
Checkout | PKPaymentButtonType.Checkout |
Donate | PKPaymentButtonType.Donate |
InStore | PKPaymentButtonType.Book |
Subscribe | PKPaymentButtonType.Subscribe |
ApplePayButtonStyle
White | PKPaymentButtonStyle.White |
WhiteOutline | PKPaymentButtonStyle.WhiteOutline |
Black | PKPaymentButtonStyle.Black |
Interfaces
ApplePayRequest
interface ApplePayRequest {
paymentItems: Array<ApplePayItems>;
merchantId: string;
merchantCapabilities: number;
countryCode: string;
currencyCode: string;
supportedNetworks: Array<ApplePayNetworks>;
billingContactFields?: Array<ApplePayContactFields>;
shippingContactFields?: Array<ApplePayContactFields>;
shippingMethods?: Array<ApplePayShippingMethods>;
}
ApplePayItems
interface ApplePayItems {
label: string;
amount: number;
type: ApplePayPaymentItemType;
}
AuthorizePaymentEventData
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;
}
AuthorizationDidFinishEventData
interface AuthorizationDidFinishEventData extends EventData {
eventName: string;
object: any;
}
ApplePayPaymentData
interface ApplePayPaymentData {
data;
header;
signature;
version: string;
}
License
Apache License Version 2.0