Skip to content

Commit 698aff1

Browse files
committed
Add support for title, description and amount options.
Android: https://developers.braintreepayments.com/guides/drop-in/android/v1 title => primaryDescription description => secondaryDescription amount => amount iOS: https://developers.braintreepayments.com/guides/drop-in/ios/v3 summaryTitle => title summaryDescription => description displayAmount => amount
1 parent 11fe781 commit 698aff1

File tree

4 files changed

+113
-65
lines changed

4 files changed

+113
-65
lines changed

android/src/main/java/com/pw/droplet/braintree/Braintree.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.facebook.react.bridge.ReactContextBaseJavaModule;
2020
import com.facebook.react.bridge.ReactMethod;
2121
import com.facebook.react.bridge.ActivityEventListener;
22+
import com.facebook.react.bridge.ReadableMap;
2223

2324
public class Braintree extends ReactContextBaseJavaModule implements ActivityEventListener {
2425
private static final int PAYMENT_REQUEST = 1706816330;
@@ -86,20 +87,39 @@ public void nonceCallback(String nonce) {
8687
}
8788

8889
@ReactMethod
89-
public void paymentRequest(final String callToActionText, final Callback successCallback, final Callback errorCallback) {
90+
public void paymentRequest(final ReadableMap options, final Callback successCallback, final Callback errorCallback) {
9091
this.successCallback = successCallback;
9192
this.errorCallback = errorCallback;
9293
PaymentRequest paymentRequest = null;
9394

94-
if (callToActionText != null) {
95-
paymentRequest = new PaymentRequest()
96-
.submitButtonText(callToActionText)
97-
.clientToken(this.getToken());
98-
} else {
99-
paymentRequest = new PaymentRequest()
100-
.clientToken(this.getToken());
95+
String callToActionText = null;
96+
String title = null;
97+
String description = null;
98+
String amount = null;
99+
100+
if (options.hasKey("callToActionText")) {
101+
callToActionText = options.getString("callToActionText");
102+
}
103+
104+
if (options.hasKey("title")) {
105+
title = options.getString("title");
106+
}
107+
108+
if (options.hasKey("description")) {
109+
description = options.getString("description");
110+
}
111+
112+
if (options.hasKey("amount")) {
113+
amount = options.getString("amount");
101114
}
102115

116+
paymentRequest = new PaymentRequest()
117+
.submitButtonText(callToActionText)
118+
.primaryDescription(title)
119+
.secondaryDescription(description)
120+
.amount(amount)
121+
.clientToken(this.getToken());
122+
103123
(getCurrentActivity()).startActivityForResult(
104124
paymentRequest.getIntent(getCurrentActivity()),
105125
PAYMENT_REQUEST

index.android.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
'use strict';
22

3-
import { NativeModules, processColor } from 'react-native';
3+
import {NativeModules, processColor} from 'react-native';
44
var Braintree = NativeModules.Braintree;
55

66
module.exports = {
77
setup(token) {
88
return new Promise(function(resolve, reject) {
9-
Braintree.setup(token, (test) => resolve(test), (err) => reject(err));
9+
Braintree.setup(token, test => resolve(test), err => reject(err));
1010
});
1111
},
1212

1313
getCardNonce(cardNumber, expirationMonth, expirationYear, cvv) {
1414
return new Promise(function(resolve, reject) {
15-
Braintree.getCardNonce(cardNumber, expirationMonth, expirationYear, cvv, (nonce) => resolve(nonce), (err) => reject(err))
15+
Braintree.getCardNonce(
16+
cardNumber,
17+
expirationMonth,
18+
expirationYear,
19+
cvv,
20+
nonce => resolve(nonce),
21+
err => reject(err)
22+
);
1623
});
1724
},
1825

1926
showPaymentViewController(config = {}) {
2027
var options = {
2128
callToActionText: config.callToActionText,
29+
title: config.title,
30+
description: config.description,
31+
amount: config.amount,
2232
};
2333
return new Promise(function(resolve, reject) {
24-
Braintree.paymentRequest(options.callToActionText, (nonce) => resolve(nonce), (error) => reject(error));
34+
Braintree.paymentRequest(
35+
options,
36+
nonce => resolve(nonce),
37+
error => reject(error)
38+
);
2539
});
2640
},
2741

2842
showPayPalViewController() {
2943
return new Promise(function(resolve, reject) {
30-
Braintree.paypalRequest((nonce) => resolve(nonce), (error) => reject(error));
44+
Braintree.paypalRequest(nonce => resolve(nonce), error => reject(error));
3145
});
3246
},
3347
};

index.ios.js

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,72 @@
11
'use strict';
22

3-
import { NativeModules, processColor } from 'react-native';
3+
import {NativeModules, processColor} from 'react-native';
44
var RCTBraintree = NativeModules.Braintree;
55

66
var Braintree = {
7+
setupWithURLScheme(token, urlscheme) {
8+
return new Promise(function(resolve, reject) {
9+
RCTBraintree.setupWithURLScheme(token, urlscheme, function(success) {
10+
success == true ? resolve(true) : reject('Invalid Token');
11+
});
12+
});
13+
},
714

8-
setupWithURLScheme(token, urlscheme) {
9-
return new Promise(function(resolve, reject) {
10-
RCTBraintree.setupWithURLScheme(token, urlscheme, function(success) {
11-
success == true ? resolve(true) : reject("Invalid Token");
12-
});
13-
});
14-
},
15-
16-
setup(token) {
17-
return new Promise(function(resolve, reject) {
18-
RCTBraintree.setup(token, function(success) {
19-
success == true ? resolve(true) : reject("Invalid Token");
20-
});
21-
});
22-
},
15+
setup(token) {
16+
return new Promise(function(resolve, reject) {
17+
RCTBraintree.setup(token, function(success) {
18+
success == true ? resolve(true) : reject('Invalid Token');
19+
});
20+
});
21+
},
2322

24-
showPaymentViewController(config = {}) {
23+
showPaymentViewController(config = {}) {
2524
var options = {
2625
tintColor: processColor(config.tintColor),
2726
bgColor: processColor(config.bgColor),
2827
barBgColor: processColor(config.barBgColor),
2928
barTintColor: processColor(config.barTintColor),
3029
callToActionText: config.callToActionText,
30+
title: config.title,
31+
description: config.description,
32+
amount: config.amount,
3133
};
32-
return new Promise(function(resolve, reject) {
33-
RCTBraintree.showPaymentViewController(options, function(err, nonce) {
34-
nonce != null ? resolve(nonce) : reject(err);
35-
});
36-
});
37-
},
34+
return new Promise(function(resolve, reject) {
35+
RCTBraintree.showPaymentViewController(options, function(err, nonce) {
36+
nonce != null ? resolve(nonce) : reject(err);
37+
});
38+
});
39+
},
3840

39-
showPayPalViewController() {
40-
return new Promise(function(resolve, reject) {
41-
RCTBraintree.showPayPalViewController(function(err, nonce) {
42-
nonce != null ? resolve(nonce) : reject(err);
43-
});
44-
});
45-
},
41+
showPayPalViewController() {
42+
return new Promise(function(resolve, reject) {
43+
RCTBraintree.showPayPalViewController(function(err, nonce) {
44+
nonce != null ? resolve(nonce) : reject(err);
45+
});
46+
});
47+
},
4648

4749
getCardNonce(cardNumber, expirationMonth, expirationYear, cvv) {
48-
return new Promise(function(resolve, reject) {
49-
RCTBraintree.getCardNonce(cardNumber, expirationMonth, expirationYear, cvv, function(err, nonce) {
50-
nonce != null ? resolve(nonce) : reject(err);
51-
});
52-
});
50+
return new Promise(function(resolve, reject) {
51+
RCTBraintree.getCardNonce(
52+
cardNumber,
53+
expirationMonth,
54+
expirationYear,
55+
cvv,
56+
function(err, nonce) {
57+
nonce != null ? resolve(nonce) : reject(err);
58+
}
59+
);
60+
});
5361
},
5462

55-
getDeviceData(options = {}) {
56-
return new Promise(function(resolve, reject) {
57-
RCTBraintree.getDeviceData(options, function(err, deviceData) {
58-
deviceData != null ? resolve(deviceData) : reject(err);
59-
});
60-
});
61-
}
62-
63+
getDeviceData(options = {}) {
64+
return new Promise(function(resolve, reject) {
65+
RCTBraintree.getDeviceData(options, function(err, deviceData) {
66+
deviceData != null ? resolve(deviceData) : reject(err);
67+
});
68+
});
69+
},
6370
};
6471

6572
module.exports = Braintree;

ios/RCTBraintree/RCTBraintree.m

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,15 @@ - (instancetype)init
7373
UIColor *barBgColor = options[@"barBgColor"];
7474
UIColor *barTintColor = options[@"barTintColor"];
7575

76+
NSString *title = options[@"title"];
77+
NSString *description = options[@"description"];
78+
NSString *amount = options[@"amount"];
79+
7680
if (tintColor) dropInViewController.view.tintColor = [RCTConvert UIColor:tintColor];
7781
if (bgColor) dropInViewController.view.backgroundColor = [RCTConvert UIColor:bgColor];
82+
if (title) [dropInViewController.paymentRequest setTitle:title];
83+
if (description) [dropInViewController.paymentRequest setDescription:description];
84+
if (amount) [dropInViewController.paymentRequest setAmount:amount];
7885

7986
dropInViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(userDidCancelPayment)];
8087

@@ -91,7 +98,7 @@ - (instancetype)init
9198

9299
dropInViewController.paymentRequest = paymentRequest;
93100
}
94-
101+
95102
[self.reactRoot presentViewController:navigationController animated:YES completion:nil];
96103
});
97104
}
@@ -147,14 +154,14 @@ - (instancetype)init
147154
RCT_EXPORT_METHOD(getDeviceData:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
148155
{
149156
dispatch_async(dispatch_get_main_queue(), ^{
150-
157+
151158
NSLog(@"%@", options);
152-
159+
153160
NSError *error = nil;
154161
NSString *deviceData = nil;
155162
NSString *environment = options[@"environment"];
156163
NSString *dataSelector = options[@"dataCollector"];
157-
164+
158165
//Initialize the data collector and specify environment
159166
if([environment isEqualToString: @"development"]){
160167
self.dataCollector = [[BTDataCollector alloc]
@@ -166,7 +173,7 @@ - (instancetype)init
166173
self.dataCollector = [[BTDataCollector alloc]
167174
initWithEnvironment:BTDataCollectorEnvironmentSandbox];
168175
}
169-
176+
170177
//Data collection methods
171178
if ([dataSelector isEqualToString: @"card"]){
172179
deviceData = [self.dataCollector collectCardFraudData];
@@ -180,14 +187,14 @@ - (instancetype)init
180187
error = [NSError errorWithDomain:@"RCTBraintree" code:255 userInfo:details];
181188
NSLog (@"Invalid data collector. Use one of: card, paypal or both");
182189
}
183-
190+
184191
NSArray *args = @[];
185192
if ( error == nil ) {
186193
args = @[[NSNull null], deviceData];
187194
} else {
188195
args = @[error.description, [NSNull null]];
189196
}
190-
197+
191198
callback(args);
192199
});
193200
}
@@ -240,9 +247,9 @@ - (void)dropInViewControllerDidCancel:(__unused BTDropInViewController *)viewCon
240247
- (UIViewController*)reactRoot {
241248
UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
242249
UIViewController *maybeModal = root.presentedViewController;
243-
250+
244251
UIViewController *modalRoot = root;
245-
252+
246253
if (maybeModal != nil) {
247254
modalRoot = maybeModal;
248255
}

0 commit comments

Comments
 (0)