-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLPayPalWrapper.m
200 lines (165 loc) · 8.38 KB
/
OLPayPalWrapper.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
//
// Modified MIT License
//
// Copyright (c) 2010-2017 Kite Tech Ltd. https://www.kite.ly
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The software MAY ONLY be used with the Kite Tech Ltd platform and MAY NOT be modified
// to be used with any competitor platforms. This means the software MAY NOT be modified
// to place orders with any competitors to Kite Tech Ltd, all orders MUST go through the
// Kite Tech Ltd platform servers.
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "OLPayPalWrapper.h"
@implementation OLPayPalWrapper
+ (id)payPalShippingAddressWithRecipientName:(NSString *)name withLine1:(NSString *)line1 withLine2:(NSString *)line2 withCity:(NSString *)city withState:(NSString *)state withPostalCode:(NSString *)postalCode withCountryCode:(NSString *)countryCode{
Class PayPalShippingAddressClass = NSClassFromString(@"PayPalShippingAddress");
SEL aSelector = NSSelectorFromString(@"shippingAddressWithRecipientName:withLine1:withLine2:withCity:withState:withPostalCode:withCountryCode:");
IMP imp = [PayPalShippingAddressClass methodForSelector:aSelector];
id (*func)(id, SEL, id, id, id, id, id, id, id) = (void *)imp;
id shippingAddress = func(PayPalShippingAddressClass, aSelector, name, line1, line2, city, state, postalCode, countryCode);
return shippingAddress;
}
+ (id)payPalPaymentWithAmount:(NSDecimalNumber *)amount currencyCode:(NSString *)currencyCode shortDescription:(NSString *)shortDescription intent:(NSInteger)intent shippingAddress:(id)shippingAddress{
Class PayPalPaymentClass = NSClassFromString(@"PayPalPayment");
id payment = [[PayPalPaymentClass alloc] init];
SEL aSelector = NSSelectorFromString(@"setAmount:");
IMP imp = [payment methodForSelector:aSelector];
void (*func)(id, SEL, id) = (void *)imp;
func(payment, aSelector, amount);
aSelector = NSSelectorFromString(@"setCurrencyCode:");
imp = [payment methodForSelector:aSelector];
func = (void *)imp;
func(payment, aSelector, currencyCode);
aSelector = NSSelectorFromString(@"setShortDescription:");
imp = [payment methodForSelector:aSelector];
func = (void *)imp;
func(payment, aSelector, shortDescription);
aSelector = NSSelectorFromString(@"setIntent:");
imp = [payment methodForSelector:aSelector];
void (*func1)(id, SEL, NSInteger) = (void *)imp;
func1(payment, aSelector, intent);
aSelector = NSSelectorFromString(@"setShippingAddress:");
imp = [payment methodForSelector:aSelector];
func = (void *)imp;
func(payment, aSelector, shippingAddress);
#ifdef DEBUG
aSelector = NSSelectorFromString(@"processable");
imp = [payment methodForSelector:aSelector];
BOOL (*func2)(id, SEL) = (void *)imp;
NSAssert(func2(payment, aSelector), @"oops, payment not processable");
#endif
return payment;
}
+ (id)payPalConfigurationWithShippingAddressOption:(NSInteger)option acceptCreditCards:(BOOL)acceptCCs{
Class payPalConfigurationClass = NSClassFromString(@"PayPalConfiguration");
id config = [[payPalConfigurationClass alloc] init];
SEL aSelector = NSSelectorFromString(@"setAcceptCreditCards:");
IMP imp = [config methodForSelector:aSelector];
void (*func)(id, SEL, BOOL) = (void *)imp;
func(config, aSelector, acceptCCs);
aSelector = NSSelectorFromString(@"setPayPalShippingAddressOption:");
imp = [config methodForSelector:aSelector];
void (*func1)(id, SEL, NSInteger) = (void *)imp;
func1(config, aSelector, option);
return config;
}
+ (id)payPalPaymentViewControllerWithPayment:(id)payment
configuration:(id)configuration
delegate:(id)delegate{
Class payPalPaymentViewControllerClass = NSClassFromString(@"PayPalPaymentViewController");
id payPalPaymentViewController = [payPalPaymentViewControllerClass alloc];
SEL aSelector = NSSelectorFromString(@"initWithPayment:configuration:delegate:");
IMP imp = [payPalPaymentViewController methodForSelector:aSelector];
id (*func)(id, SEL, id, id, id) = (void *)imp;
payPalPaymentViewController = func(payPalPaymentViewController, aSelector, payment, configuration, delegate);
return payPalPaymentViewController;
}
+ (NSDictionary *)confirmationWithPayment:(id)payment{
SEL aSelector = NSSelectorFromString(@"confirmation");
IMP imp = [payment methodForSelector:aSelector];
id (*func)(id, SEL) = (void *)imp;
return func(payment, aSelector);
}
+ (void)initializeWithClientIdsForEnvironments:(NSDictionary *)dict{
Class PayPalMobileClass = NSClassFromString(@"PayPalMobile");
SEL aSelector = NSSelectorFromString(@"initializeWithClientIdsForEnvironments:");
IMP imp = [PayPalMobileClass methodForSelector:aSelector];
void (*func)(id, SEL, id) = (void *)imp;
func(PayPalMobileClass, aSelector, dict);
}
+ (void)preconnectWithEnvironment:(NSString *)env{
Class PayPalMobileClass = NSClassFromString(@"PayPalMobile");
SEL aSelector = NSSelectorFromString(@"preconnectWithEnvironment:");
IMP imp = [PayPalMobileClass methodForSelector:aSelector];
void (*func)(id, SEL, id) = (void *)imp;
func(PayPalMobileClass, aSelector, env);
}
+ (BOOL)isPayPalAvailable{
Class PayPalMobileClass = NSClassFromString(@"PayPalMobile");
if (![PayPalMobileClass class]){
return NO;
}
Class PayPalShippingAddressClass = NSClassFromString(@"PayPalShippingAddress");
if (![PayPalShippingAddressClass class]){
NSLog(@"Warning: PayPal API version mismatch.");
return NO;
}
SEL aSelector = NSSelectorFromString(@"shippingAddressWithRecipientName:withLine1:withLine2:withCity:withState:withPostalCode:withCountryCode:");
if (![PayPalShippingAddressClass respondsToSelector:aSelector]){
NSLog(@"Warning: PayPal API version mismatch.");
return NO;
}
Class PayPalPaymentClass = NSClassFromString(@"PayPalPayment");
if (![PayPalPaymentClass class]){
NSLog(@"Warning: PayPal API version mismatch.");
return NO;
}
for (NSString *s in @[@"setAmount:", @"setCurrencyCode:", @"setShortDescription:", @"setIntent:", @"setShippingAddress:", @"processable"]){
aSelector = NSSelectorFromString(s);
if (![PayPalPaymentClass instancesRespondToSelector:aSelector]){
NSLog(@"Warning: PayPal API version mismatch.");
return NO;
}
}
Class payPalConfigurationClass = NSClassFromString(@"PayPalConfiguration");
if (![payPalConfigurationClass class]){
NSLog(@"Warning: PayPal API version mismatch.");
return NO;
}
for (NSString *s in @[@"setAcceptCreditCards:", @"setPayPalShippingAddressOption:"]){
aSelector = NSSelectorFromString(s);
if (![payPalConfigurationClass instancesRespondToSelector:aSelector]){
NSLog(@"Warning: PayPal API version mismatch.");
return NO;
}
}
Class payPalPaymentViewControllerClass = NSClassFromString(@"PayPalPaymentViewController");
if (![payPalPaymentViewControllerClass class]){
NSLog(@"Warning: PayPal API version mismatch.");
return NO;
}
aSelector = NSSelectorFromString(@"initWithPayment:configuration:delegate:");
if (![payPalPaymentViewControllerClass instancesRespondToSelector:aSelector]){
NSLog(@"Warning: PayPal API version mismatch.");
return NO;
}
return YES;
}
@end