forked from grandrole/PayPal-iOS-SDK-PhoneGap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPayPalMobilePGPlugin.m
244 lines (178 loc) · 10.4 KB
/
PayPalMobilePGPlugin.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
//
// PayPalMobilePGPlugin.m
//
#import "PayPalMobilePGPlugin.h"
@interface PayPalMobilePGPlugin ()
- (void)sendErrorToDelegate:(NSString *)errorMessage;
- (PayPalConfiguration*)parseConfiguration:(NSDictionary *)jsObject;
@property(nonatomic, strong, readwrite) CDVInvokedUrlCommand *command;
@property(nonatomic, strong, readwrite) PayPalPaymentViewController *paymentController;
@property(nonatomic, strong, readwrite) PayPalFuturePaymentViewController *futurePaymentController;
@property(nonatomic, strong, readwrite) NSString *currentEnvironment;
@end
#pragma mark -
@implementation PayPalMobilePGPlugin
- (void)version:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:[PayPalMobile libraryVersion]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)environment:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:self.currentEnvironment];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (PayPalConfiguration *)parseConfiguration:(NSDictionary *)configuration {
PayPalConfiguration *ppconfiguration = [[PayPalConfiguration alloc] init];
if ([configuration.allKeys containsObject:@"defaultUserEmail"])
ppconfiguration.defaultUserEmail = [configuration valueForKey:@"defaultUserEmail"];
if ([configuration.allKeys containsObject:@"defaultUserPhoneCountryCode"])
ppconfiguration.defaultUserPhoneCountryCode = [configuration valueForKey:@"defaultUserPhoneCountryCode"];
if ([configuration.allKeys containsObject:@"defaultUserPhoneNumber"])
ppconfiguration.defaultUserPhoneNumber = [configuration valueForKey:@"defaultUserPhoneNumber"];
if ([configuration.allKeys containsObject:@"merchantName"])
ppconfiguration.merchantName = [configuration valueForKey:@"merchantName"];
if ([configuration.allKeys containsObject:@"merchantPrivacyPolicyURL"])
ppconfiguration.merchantPrivacyPolicyURL = [configuration valueForKey:@"merchantPrivacyPolicyURL"];
if ([configuration.allKeys containsObject:@"merchantUserAgreementURL"])
ppconfiguration.merchantUserAgreementURL = [configuration valueForKey:@"merchantUserAgreementURL"];
if ([configuration.allKeys containsObject:@"acceptCreditCards"])
ppconfiguration.acceptCreditCards = [[configuration valueForKey:@"acceptCreditCards"] boolValue];
if ([configuration.allKeys containsObject:@"rememberUser"])
ppconfiguration.rememberUser = [[configuration valueForKey:@"rememberUser"] boolValue];
if ([configuration.allKeys containsObject:@"languageOrLocale"])
ppconfiguration.languageOrLocale = [configuration valueForKey:@"languageOrLocale"];
if ([configuration.allKeys containsObject:@"disableBlurWhenBackgrounding"])
ppconfiguration.disableBlurWhenBackgrounding = [[configuration valueForKey:@"disableBlurWhenBackgrounding"] boolValue];
if ([configuration.allKeys containsObject:@"forceDefaultsInSandbox"])
ppconfiguration.forceDefaultsInSandbox = [[configuration valueForKey:@"forceDefaultsInSandbox"] boolValue];
if ([configuration.allKeys containsObject:@"sandboxUserPassword"])
ppconfiguration.sandboxUserPassword = [configuration valueForKey:@"sandboxUserPassword"];
if ([configuration.allKeys containsObject:@"sandboxUserPin"])
ppconfiguration.sandboxUserPin = [configuration valueForKey:@"sandboxUserPin"];
return ppconfiguration;
}
- (void)prepareForPayment:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult = nil;
NSString *productionClientId = [command.arguments objectAtIndex:0];
NSString *sandboxClientId = [command.arguments objectAtIndex:1];
NSString *environment = [command.arguments objectAtIndex:2];
NSString *environmentToUse = nil;
if ([environment isEqualToString:@"PayPalEnvironmentNoNetwork"]) {
environmentToUse = PayPalEnvironmentNoNetwork;
} else if ([environment isEqualToString:@"PayPalEnvironmentProduction"]) {
environmentToUse = PayPalEnvironmentProduction;
} else if ([environment isEqualToString:@"PayPalEnvironmentSandbox"]) {
environmentToUse = PayPalEnvironmentSandbox;
}
if (!productionClientId) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"The provided productionClientId was null or empty"];
} else if (!sandboxClientId) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"The provided sandboxClientId was null or empty"];
} else if (!environmentToUse) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"The provided environmentToUse was null or empty"];
} else {
self.currentEnvironment = environmentToUse;
[PayPalMobile initializeWithClientIdsForEnvironments:@{PayPalEnvironmentProduction : productionClientId,
PayPalEnvironmentSandbox : sandboxClientId}];
[PayPalMobile preconnectWithEnvironment:self.currentEnvironment];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)presentPaymentUI:(CDVInvokedUrlCommand *)command {
// check number and type of arguments
int argumentCount = (signed)[command.arguments count];
if (argumentCount != 2) {
[self sendErrorToDelegate:@"presentPaymentUI requires exactly two arguments: payment and configuration"];
return;
}
NSDictionary *payment = [command.arguments objectAtIndex:0];
if (![payment isKindOfClass:[NSDictionary class]]) {
[self sendErrorToDelegate:@"payment must be a PayPalPayment object"];
return;
}
NSDictionary *configuration = [command.arguments objectAtIndex:1];
if (![configuration isKindOfClass:[NSDictionary class]]) {
[self sendErrorToDelegate:@"payment must be a PayPalConfiguration object"];
return;
}
PayPalPaymentIntent intent;
if ([[payment[@"intent"] lowercaseString] isEqualToString: @"authorize"]) {
intent = PayPalPaymentIntentAuthorize;
} else {
intent = PayPalPaymentIntentSale;
}
PayPalPayment *pppayment = [PayPalPayment paymentWithAmount:[NSDecimalNumber decimalNumberWithString:payment[@"amount"]]
currencyCode:payment[@"currency"]
shortDescription:payment[@"shortDescription"]
intent:intent];
PayPalConfiguration *ppconfiguration = [self parseConfiguration:configuration];
if (!pppayment.processable) {
[self sendErrorToDelegate:@"payment not processable"];
return;
}
PayPalPaymentViewController *controller = [[PayPalPaymentViewController alloc] initWithPayment:pppayment configuration:ppconfiguration delegate:self];
if (!controller) {
[self sendErrorToDelegate:@"could not instantiate PayPalPaymentViewController"]; // should never happen
return;
}
if (!self.currentEnvironment) {
[self sendErrorToDelegate:@"environment set, invoke setEnvironment prior to presenting the payment UI"];
return;
}
self.command = command;
self.paymentController = controller;
if ([self.viewController respondsToSelector:@selector(presentViewController:animated:completion:)]){
[self.viewController presentViewController:controller animated:YES completion:nil];
} else {
[self.viewController presentModalViewController:controller animated:YES];
}
}
- (void)presentFuturePaymentUI:(CDVInvokedUrlCommand *)command {
NSDictionary *configuration = [command.arguments objectAtIndex:0];
if (![configuration isKindOfClass:[NSDictionary class]]) {
[self sendErrorToDelegate:@"payment must be a PayPalConfiguration object"];
return;
}
PayPalConfiguration *ppconfiguration = [self parseConfiguration:configuration];
PayPalFuturePaymentViewController *controller = [[PayPalFuturePaymentViewController alloc] initWithConfiguration:ppconfiguration delegate:self];
self.command = command;
self.futurePaymentController = controller;
if ([self.viewController respondsToSelector:@selector(presentViewController:animated:completion:)]){
[self.viewController presentViewController:controller animated:YES completion:nil];
} else {
[self.viewController presentModalViewController:controller animated:YES];
}
}
#pragma mark - Cordova convenience helpers
- (void)sendErrorToDelegate:(NSString *)errorMessage {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:errorMessage];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.command.callbackId];
}
#pragma mark - PayPalPaymentDelegate implementaiton
- (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController {
[self.viewController dismissModalViewControllerAnimated:YES];
[self sendErrorToDelegate:@"payment cancelled"];
}
- (void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController
didCompletePayment:(PayPalPayment *)completedPayment {
[self.viewController dismissModalViewControllerAnimated:YES];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:completedPayment.confirmation];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.command.callbackId];
}
#pragma mark - PayPalFuturePaymentDelegate implementation
- (void)payPalFuturePaymentDidCancel:(PayPalFuturePaymentViewController *)futurePaymentViewController {
[self.viewController dismissViewControllerAnimated:YES completion:nil];
[self sendErrorToDelegate:@"payment cancelled"];
}
- (void)payPalFuturePaymentViewController:(PayPalFuturePaymentViewController *)futurePaymentViewController
didAuthorizeFuturePayment:(NSDictionary *)futurePaymentAuthorization {
[self.viewController dismissViewControllerAnimated:YES completion:nil];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:futurePaymentAuthorization];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.command.callbackId];
}
@end