forked from naoufal/react-native-payments
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApplePayManager.m
139 lines (109 loc) · 4.67 KB
/
ApplePayManager.m
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#import "ApplePayManager.h"
#import "RCTUtils.h"
#import "RCTLog.h"
#import "StripeManager.h"
@implementation ApplePayManager
RCT_EXPORT_MODULE()
// Checks if user can make payments with ApplePay
//
// This functionality may not be supported by their hardware, or it may be
// restricted by parental controls
RCT_EXPORT_METHOD(canMakePayments: (RCTResponseSenderBlock)callback)
{
if([PKPaymentAuthorizationViewController canMakePayments]) {
callback(@[@true]);
} else {
callback(@[@false]);
}
}
// Checks if user can make payments with ApplePay and if a payment card is
// configured
//
// This functionality may not be supported by their hardware, or it may be
// restricted by parental controls
RCT_EXPORT_METHOD(canMakePaymentsUsingNetworks: (RCTResponseSenderBlock)callback)
{
NSArray *paymentNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];
if ([PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:paymentNetworks]) {
callback(@[@true]);
} else {
callback(@[@false]);
}
}
RCT_EXPORT_METHOD(paymentRequest: (NSDictionary *)args
items: (NSArray *)items
callback:(RCTResponseSenderBlock)callback)
{
// Set callback to self so we can use it later
self.callback = callback;
// Set Payment Processor
if (!args[@"paymentProcessor"] || !args[@"paymentProcessorPublicKey"]) {
RCTLogError(@"[ApplePay] Your must provide a payment processor and a public key.");
}
self.paymentProcessor = args[@"paymentProcessor"];
self.paymentProcessorPublicKey = args[@"paymentProcessorPublicKey"];
self.backendUrl = args[@"backendUrl"];
// Setup Payment Request
PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
// Merchant/Currency Setup
request.countryCode = args[@"countryCode"];
request.currencyCode = args[@"currencyCode"];
request.merchantIdentifier = args[@"merchantIdentifier"];
request.merchantCapabilities = PKMerchantCapabilityDebit;
request.supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];
// Setup product, discount, shipping and total
NSMutableArray *summaryItems = [NSMutableArray array];
for (NSDictionary *i in items) {
NSLog(@"Item: %@", i[@"label"]);
PKPaymentSummaryItem *item = [[PKPaymentSummaryItem alloc] init];
item.label = i[@"label"];
item.amount = [NSDecimalNumber decimalNumberWithString:i[@"amount"]];
[summaryItems addObject:item];
}
// Add Payment Items to request
request.paymentSummaryItems = summaryItems;
// Show Payment Sheet
PKPaymentAuthorizationViewController *const viewController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest: request];
viewController.delegate = self;
UIViewController *ctrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[ctrl presentViewController:viewController animated:YES completion:nil];
NSLog(@"Payment Authorization Controller visible.");
}
RCT_EXPORT_METHOD(success:(RCTResponseSenderBlock)callback)
{
self.completion(PKPaymentAuthorizationStatusSuccess);
callback(@[[NSNull null]]);
}
RCT_EXPORT_METHOD(failure:(RCTResponseSenderBlock)callback)
{
self.completion(PKPaymentAuthorizationStatusFailure);
callback(@[[NSNull null]]);
}
// Payment authorized handler
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
self.payment = payment;
// Set completion to self so we can call it from JS
NSLog(@"completion: %@", completion);
self.completion = completion;
// Select Payment Processor
NSLog(@"Payment Processor: %@", self.paymentProcessor);
if ([self.paymentProcessor isEqual: @"stripe"]) {
[StripeManager handlePaymentAuthorizationWithPayment:self.paymentProcessorPublicKey
payment:payment
completion:completion
callback:self.callback];
} else {
RCTLogError(@"[ApplePay] Your payment provider is not yet supported.");
return;
}
}
// Finish handler
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
NSLog(@"Payment Authorization Controller dismissed.");
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end