-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapple-pay.ts
86 lines (75 loc) · 3.57 KB
/
apple-pay.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { DemoSharedApplePay } from '@demo/shared';
import { ApplePayBtn, ApplePayContactFields, ApplePayEvents, ApplePayItems, ApplePayMerchantCapability, ApplePayNetworks, ApplePayPaymentItemType, ApplePayRequest, ApplePayTransactionStatus, AuthorizePaymentEventData } from '@nativescript/apple-pay';
import { EventData, Page } from '@nativescript/core';
const MERCHANTID = '388333172821828292929';
const SomeIdentifierFromPaymentProvider = '89290109290129912088';
export function navigatingTo(args: EventData) {
const page = <Page>args.object;
page.bindingContext = new DemoModel();
}
export class DemoModel extends DemoSharedApplePay {
async onApplePayTap(args) {
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 this.someHttpMethodToYourProviderBackend(payloadToBackend);
if (result) {
// 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 transaction 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.5,
label: 'Balance',
type: ApplePayPaymentItemType.Final,
},
] as unknown as ApplePayItems[];
const request = {
paymentItems,
merchantId: MERCHANTID, // 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);
}
}
someHttpMethodToYourProviderBackend(payload) {
console.log('🟢 Send the payload to your payment provider 🟢 \n This should be documented by the provider on exactly what values they need.');
return true;
}
}