This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iOSBraintreePhoneGapPlugin.m
131 lines (104 loc) · 4.9 KB
/
iOSBraintreePhoneGapPlugin.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
//
// BraintreePhoneGapPlugin.m
// BraintreePhoneGap
//
// Created by Roman Punskyy on 18/05/2013.
//
//
#import "iOSBraintreePhoneGapPlugin.h"
@interface iOSBraintreePhoneGapPlugin ()
@property (nonatomic, strong) CDVInvokedUrlCommand *command;
@property (strong, nonatomic) BTPaymentViewController *paymentViewController;
- (void)paymentCancel:(id)sender;
@end
@implementation iOSBraintreePhoneGapPlugin
- (void)initWithSettings:(CDVInvokedUrlCommand *)command
{
// check number of arguments, send error if not 5
if ([command.arguments count] != 5) {
NSString *errorMsg = [NSString stringWithFormat:@"There should be 5 arguments but %i was given !", command.arguments. count];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:errorMsg];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
// grab the parameters
NSString *env = [command.arguments objectAtIndex:0];
NSString *bt_sandbox_m_id = [command.arguments objectAtIndex:1];
NSString *bt_sandbox_cs_enc_key = [command.arguments objectAtIndex:2];
NSString *bt_production_cs_enc_key = [command.arguments objectAtIndex:3];
NSString *bt_production_m_id = [command.arguments objectAtIndex:4];
// initilise Venmo Touch framework depending on the environment
if ([env isEqualToString:@"sandbox"]) {
[VTClient
startWithMerchantID:bt_sandbox_m_id
braintreeClientSideEncryptionKey:bt_sandbox_cs_enc_key
environment:VTEnvironmentSandbox];
} else {
[VTClient
startWithMerchantID:bt_production_cs_enc_key
braintreeClientSideEncryptionKey:bt_production_m_id
environment:VTEnvironmentSandbox];
}
// send ok
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)paymentCancel:(id)sender
{
// user pressed cancel
// dismiss view and report to caller
[self.viewController dismissViewControllerAnimated:YES completion:^{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.command.callbackId];
}];
}
- (void)getCreditCardInfo:(CDVInvokedUrlCommand *)command
{
// present braintree credit card controller
self.command = command;
self.paymentViewController = [BTPaymentViewController paymentViewControllerWithVenmoTouchEnabled:YES];
self.paymentViewController.delegate = self;
// check if zip entry show be shown or not
BOOL showZip = NO;
if ([command.arguments count]) {
showZip = [[command.arguments objectAtIndex:0] boolValue];
}
self.paymentViewController.requestsZipInManualCardEntry = showZip;
// Add paymentViewController to a navigation controller.
UINavigationController *paymentNavigationController = [[UINavigationController alloc] initWithRootViewController:self.paymentViewController];
// Add the cancel button
self.paymentViewController.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self
action:@selector(paymentCancel:)];
[self.viewController presentModalViewController:paymentNavigationController animated:YES];
}
- (void)dismiss:(CDVInvokedUrlCommand *)command
{
// the developer needs to check if the credit card processes on the server or if he is happy with the data,
// hence we need to export dismissing of the view
[self.paymentViewController prepareForDismissal];
[self.viewController dismissViewControllerAnimated:YES completion:^{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.command.callbackId];
}];
}
#pragma mark - BTPaymentViewControllerDelegate
- (void)paymentViewController:(BTPaymentViewController *)paymentViewController
didSubmitCardWithInfo:(NSDictionary *)cardInfo
andCardInfoEncrypted:(NSDictionary *)cardInfoEncrypted
{
// send credit card info to the caller
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"cardInfo": cardInfo, @"cardInfoEncrypted": cardInfoEncrypted}];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.command.callbackId];
}
- (void)paymentViewController:(BTPaymentViewController *)paymentViewController
didAuthorizeCardWithPaymentMethodCode:(NSString *)paymentMethodCode
{
// send credit card token to the caller
NSDictionary *methodCode = @{@"venmo_sdk_payment_method_code": paymentMethodCode};
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"token": methodCode}];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.command.callbackId];
}
@end