-
Notifications
You must be signed in to change notification settings - Fork 33
/
RNReaderSDKCheckout.m
252 lines (220 loc) · 10.9 KB
/
RNReaderSDKCheckout.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright 2018 Square Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "RNReaderSDKCheckout.h"
#import "RNReaderSDKErrorUtilities.h"
#import "Converters/SQRDCheckoutResult+RNReaderSDKAdditions.h"
#import <CoreLocation/CoreLocation.h>
#import <AVFoundation/AVFoundation.h>
@interface RNReaderSDKCheckout ()
@property (strong, readwrite) RCTPromiseResolveBlock checkoutResolver;
@property (strong, readwrite) RCTPromiseRejectBlock checkoutRejecter;
@end
// Define all the error codes and messages below
// These error codes and messages **MUST** align with iOS error codes and javascript error codes
// Search KEEP_IN_SYNC_CHECKOUT_ERROR to update all places
// Expected errors:
static NSString *const RNReaderSDKCheckoutCancelled = @"CHECKOUT_CANCELED";
static NSString *const RNReaderSDKCheckoutSdkNotAuthorized = @"CHECKOUT_SDK_NOT_AUTHORIZED";
// React native module debug error codes
static NSString *const RNReaderSDKRNCheckoutAlreadyInProgress = @"rn_checkout_already_in_progress";
static NSString *const RNReaderSDKRNCheckoutInvalidParameter = @"rn_checkout_invalid_parameter";
// react native module debug messages
static NSString *const RNReaderSDKRNMessageCheckoutAlreadyInProgress = @"A checkout operation is already in progress. Ensure that the in-progress checkout is completed before calling startCheckoutAsync again.";
static NSString *const RNReaderSDKRNMessageCheckoutInvalidParameter = @"Invalid parameter found in checkout parameters.";
@implementation RNReaderSDKCheckout
RCT_EXPORT_MODULE();
RCT_REMAP_METHOD(startCheckout,
jsCheckoutParameters
: (NSDictionary *)jsParams
startCheckoutWithResolver
: (RCTPromiseResolveBlock)resolve
rejecter
: (RCTPromiseRejectBlock)reject)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (self.checkoutResolver != nil) {
reject(RNReaderSDKUsageError, [RNReaderSDKErrorUtilities createNativeModuleError:RNReaderSDKRNCheckoutAlreadyInProgress debugMessage:RNReaderSDKRNMessageCheckoutAlreadyInProgress], nil);
return;
}
NSString *paramError = nil;
if ([self validateJSCheckoutParameters:jsParams errorMsg:¶mError] == NO) {
NSString *paramErrorDebugMessage = [NSString stringWithFormat:@"%@ %@", RNReaderSDKRNMessageCheckoutInvalidParameter, paramError];
reject(RNReaderSDKUsageError, [RNReaderSDKErrorUtilities createNativeModuleError:RNReaderSDKRNCheckoutInvalidParameter debugMessage:paramErrorDebugMessage], nil);
return;
}
NSDictionary *jsAmountMoney = jsParams[@"amountMoney"];
SQRDMoney *amountMoney = nil;
if (jsAmountMoney[@"currencyCode"]) {
amountMoney = [[SQRDMoney alloc] initWithAmount:[jsAmountMoney[@"amount"] longValue] currencyCode:SQRDCurrencyCodeMake(jsAmountMoney[@"currencyCode"])];
} else {
amountMoney = [[SQRDMoney alloc] initWithAmount:[jsAmountMoney[@"amount"] longValue]];
}
SQRDCheckoutParameters *checkoutParams = [[SQRDCheckoutParameters alloc] initWithAmountMoney:amountMoney];
if (jsParams[@"note"]) {
checkoutParams.note = jsParams[@"note"];
}
if (jsParams[@"skipReceipt"]) {
checkoutParams.skipReceipt = ([jsParams[@"skipReceipt"] boolValue] == YES);
}
if (jsParams[@"alwaysRequireSignature"]) {
checkoutParams.alwaysRequireSignature = ([jsParams[@"alwaysRequireSignature"] boolValue] == YES);
}
if (jsParams[@"allowSplitTender"]) {
checkoutParams.allowSplitTender = ([jsParams[@"allowSplitTender"] boolValue] == YES);
}
if (jsParams[@"tipSettings"]) {
SQRDTipSettings *tipSettings = [self buildTipSettings:jsParams[@"tipSettings"]];
checkoutParams.tipSettings = tipSettings;
}
if (jsParams[@"additionalPaymentTypes"]) {
checkoutParams.additionalPaymentTypes = [self buildAdditionalPaymentTypes:jsParams[@"additionalPaymentTypes"]];
}
SQRDCheckoutController *checkoutController = [[SQRDCheckoutController alloc] initWithParameters:checkoutParams delegate:self];
self.checkoutResolver = resolve;
self.checkoutRejecter = reject;
UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
[checkoutController presentFromViewController:rootViewController];
});
}
- (void)checkoutController:(SQRDCheckoutController *)checkoutController didFinishCheckoutWithResult:(SQRDCheckoutResult *)result
{
self.checkoutResolver([result jsonDictionary]);
[self clearCheckoutHooks];
}
- (void)checkoutController:(SQRDCheckoutController *)checkoutController didFailWithError:(NSError *)error
{
NSString *message = error.localizedDescription;
NSString *debugCode = error.userInfo[SQRDErrorDebugCodeKey];
NSString *debugMessage = error.userInfo[SQRDErrorDebugMessageKey];
self.checkoutRejecter([self getCheckoutErrorCode:error.code],
[RNReaderSDKErrorUtilities serializeErrorToJson:debugCode message:message debugMessage:debugMessage],
error);
[self clearCheckoutHooks];
}
- (void)checkoutControllerDidCancel:(SQRDCheckoutController *)checkoutController
{
// Return transaction cancel as an error in order to align with Android implementation
self.checkoutRejecter(RNReaderSDKCheckoutCancelled, [RNReaderSDKErrorUtilities createNativeModuleError:RNReaderSDKCheckoutCancelled debugMessage:@"The user canceled the transaction."], nil);
[self clearCheckoutHooks];
}
- (void)clearCheckoutHooks
{
self.checkoutResolver = nil;
self.checkoutRejecter = nil;
}
- (BOOL)validateJSCheckoutParameters:(NSDictionary *)jsCheckoutParameters errorMsg:(NSString **)errorMsg
{
// check types of all parameters
if (!jsCheckoutParameters[@"amountMoney"] || ![jsCheckoutParameters[@"amountMoney"] isKindOfClass:[NSDictionary class]]) {
*errorMsg = @"'amountMoney' is missing or not an object";
return NO;
}
if (jsCheckoutParameters[@"skipReceipt"] && ![jsCheckoutParameters[@"skipReceipt"] isKindOfClass:[NSNumber class]]) {
*errorMsg = @"'skipReceipt' is not a boolean";
return NO;
}
if (jsCheckoutParameters[@"alwaysRequireSignature"] && ![jsCheckoutParameters[@"alwaysRequireSignature"] isKindOfClass:[NSNumber class]]) {
*errorMsg = @"'alwaysRequireSignature' is not a boolean";
return NO;
}
if (jsCheckoutParameters[@"allowSplitTender"] && ![jsCheckoutParameters[@"allowSplitTender"] isKindOfClass:[NSNumber class]]) {
*errorMsg = @"'allowSplitTender' is not a boolean";
return NO;
}
if (jsCheckoutParameters[@"note"] && ![jsCheckoutParameters[@"note"] isKindOfClass:[NSString class]]) {
*errorMsg = @"'note' is not a string";
return NO;
}
if (jsCheckoutParameters[@"tipSettings"] && ![jsCheckoutParameters[@"tipSettings"] isKindOfClass:[NSDictionary class]]) {
*errorMsg = @"'tipSettings' is not an object";
return NO;
}
if (jsCheckoutParameters[@"additionalPaymentTypes"] && ![jsCheckoutParameters[@"additionalPaymentTypes"] isKindOfClass:[NSArray class]]) {
*errorMsg = @"'additionalPaymentTypes' is not an array";
return NO;
}
// check amountMoney
NSDictionary *amountMoney = jsCheckoutParameters[@"amountMoney"];
if (!amountMoney[@"amount"] || ![amountMoney[@"amount"] isKindOfClass:[NSNumber class]]) {
*errorMsg = @"'amount' is not an integer";
return NO;
}
if (amountMoney[@"currencyCode"] && ![amountMoney[@"currencyCode"] isKindOfClass:[NSString class]]) {
*errorMsg = @"'currencyCode' is not a string";
return NO;
}
// check tipSettings
NSDictionary *tipSettings = jsCheckoutParameters[@"tipSettings"];
if (tipSettings != nil) {
if ((tipSettings[@"showCustomTipField"] && ![tipSettings[@"showCustomTipField"] isKindOfClass:[NSNumber class]])) {
*errorMsg = @"'showCustomTipField' is not a boolean";
return NO;
}
if (tipSettings[@"showSeparateTipScreen"] && ![tipSettings[@"showSeparateTipScreen"] isKindOfClass:[NSNumber class]]) {
*errorMsg = @"'showSeparateTipScreen' is not a boolean";
return NO;
}
if (tipSettings[@"tipPercentages"] && ![tipSettings[@"tipPercentages"] isKindOfClass:[NSArray class]]) {
*errorMsg = @"'tipPercentages' is not an array";
return NO;
}
}
return YES;
}
- (SQRDTipSettings *)buildTipSettings:(NSDictionary *)tipSettingConfig
{
SQRDTipSettings *tipSettings = [SQRDTipSettings alloc];
if (tipSettingConfig[@"showCustomTipField"]) {
tipSettings.showCustomTipField = ([tipSettingConfig[@"showCustomTipField"] boolValue] == YES);
}
if (tipSettingConfig[@"showSeparateTipScreen"]) {
tipSettings.showSeparateTipScreen = ([tipSettingConfig[@"showSeparateTipScreen"] boolValue] == YES);
}
if (tipSettingConfig[@"tipPercentages"]) {
NSMutableArray *tipPercentages = [[NSMutableArray alloc] init];
for (NSNumber *percentage in tipSettingConfig[@"tipPercentages"]) {
[tipPercentages addObject:percentage];
}
tipSettings.tipPercentages = tipPercentages;
}
return tipSettings;
}
- (SQRDAdditionalPaymentTypes)buildAdditionalPaymentTypes:(NSArray *)additionalPaymentTypes
{
SQRDAdditionalPaymentTypes sqrdAdditionalPaymentTypes = 0;
for (NSString *typeName in additionalPaymentTypes) {
if ([typeName isEqualToString:@"cash"]) {
sqrdAdditionalPaymentTypes |= SQRDAdditionalPaymentTypeCash;
} else if ([typeName isEqualToString:@"manual_card_entry"]) {
sqrdAdditionalPaymentTypes |= SQRDAdditionalPaymentTypeManualCardEntry;
} else if ([typeName isEqualToString:@"other"]) {
sqrdAdditionalPaymentTypes |= SQRDAdditionalPaymentTypeOther;
}
}
return sqrdAdditionalPaymentTypes;
}
- (NSString *)getCheckoutErrorCode:(NSInteger)nativeErrorCode
{
NSString *errorCode = @"UNKNOWN";
if (nativeErrorCode == SQRDCheckoutControllerErrorUsageError) {
errorCode = RNReaderSDKUsageError;
} else {
switch (nativeErrorCode) {
case SQRDCheckoutControllerErrorSDKNotAuthorized:
errorCode = RNReaderSDKCheckoutSdkNotAuthorized;
break;
}
}
return errorCode;
}
@end