-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPayPalMobilePGPlugin.m
156 lines (117 loc) · 5.87 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
//
// PayPalMobilePGPlugin.m
//
#import "PayPalMobilePGPlugin.h"
@interface PayPalMobilePGPlugin ()
- (void)sendErrorToDelegate:(NSString *)errorMessage;
@property(nonatomic, strong, readwrite) CDVInvokedUrlCommand *command;
@property(nonatomic, strong, readwrite) PayPalPaymentViewController *paymentController;
@end
#pragma mark -
@implementation PayPalMobilePGPlugin
- (void)version:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:[PayPalPaymentViewController libraryVersion]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)environment:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:[[PayPalPaymentViewController environment] lowercaseString]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setEnvironment:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
NSString *environment = [command.arguments objectAtIndex:0];
NSString *environmentToUse = nil;
if ([environment isEqualToString:@"PayPalEnvironmentNoNetwork"]) {
environmentToUse = PayPalEnvironmentNoNetwork;
} else if ([environment isEqualToString:@"PayPalEnvironmentProduction"]) {
environmentToUse = PayPalEnvironmentProduction;
} else if ([environment isEqualToString:@"PayPalEnvironmentSandbox"]) {
environmentToUse = PayPalEnvironmentSandbox;
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"The provided environment is not supported"];
}
if (environmentToUse) {
[PayPalPaymentViewController setEnvironment:environmentToUse];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)prepareForPayment:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult = nil;
NSString *clientId = [command.arguments objectAtIndex:0];
if (clientId.length > 0) {
[PayPalPaymentViewController prepareForPaymentUsingClientId:clientId];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"The provided clientId was null or empty"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)presentPaymentUI:(CDVInvokedUrlCommand *)command {
// check number and type of arguments
if ([command.arguments count] != 4) {
[self sendErrorToDelegate:@"presentPaymentUI requires precisely four arguments"];
return;
}
NSString *clientId = [command.arguments objectAtIndex:0];
if (![clientId isKindOfClass:[NSString class]]) {
[self sendErrorToDelegate:@"clientId must be a string"];
return;
}
NSString *email = [command.arguments objectAtIndex:1];
if (![email isKindOfClass:[NSString class]]) {
[self sendErrorToDelegate:@"email must be a string"];
return;
}
NSString *payerId = [command.arguments objectAtIndex:2];
if (payerId && ![payerId isKindOfClass:[NSString class]]) {
[self sendErrorToDelegate:@"payerId must be a string or null"];
return;
}
NSDictionary *payment = [command.arguments objectAtIndex:3];
if (![payment isKindOfClass:[NSDictionary class]]) {
[self sendErrorToDelegate:@"payment must be a PayPalPayment object"];
return;
}
NSString *amount = payment[@"amount"];
NSString *currency = payment[@"currency"];
NSString *shortDescription = payment[@"shortDescription"];
PayPalPayment *pppayment = [PayPalPayment paymentWithAmount:[NSDecimalNumber decimalNumberWithString:amount]
currencyCode:currency
shortDescription:shortDescription];
if (!pppayment.processable) {
[self sendErrorToDelegate:@"payment not processable"];
return;
}
PayPalPaymentViewController *controller = [[PayPalPaymentViewController alloc] initWithClientId:clientId
receiverEmail:email
payerId:payerId
payment:pppayment
delegate:self];
if (!controller) {
[self sendErrorToDelegate:@"could not instantiate PayPalPaymentViewController"]; // should never happen
return;
}
self.command = command;
self.paymentController = controller;
[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 {
[self.viewController dismissModalViewControllerAnimated:YES];
[self sendErrorToDelegate:@"payment cancelled"];
}
- (void)payPalPaymentDidComplete:(PayPalPayment *)completedPayment {
[self.viewController dismissModalViewControllerAnimated:YES];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:completedPayment.confirmation];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.command.callbackId];
}
@end