diff --git a/android/src/main/java/com/pw/droplet/braintree/Braintree.java b/android/src/main/java/com/pw/droplet/braintree/Braintree.java index 3d8ebe3f..ccdb78bb 100644 --- a/android/src/main/java/com/pw/droplet/braintree/Braintree.java +++ b/android/src/main/java/com/pw/droplet/braintree/Braintree.java @@ -27,6 +27,7 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ActivityEventListener; +import com.facebook.react.bridge.ReadableMap; public class Braintree extends ReactContextBaseJavaModule implements ActivityEventListener { private static final int PAYMENT_REQUEST = 1706816330; @@ -133,20 +134,39 @@ public void nonceErrorCallback(String error) { } @ReactMethod - public void paymentRequest(final String callToActionText, final Callback successCallback, final Callback errorCallback) { + public void paymentRequest(final ReadableMap options, final Callback successCallback, final Callback errorCallback) { this.successCallback = successCallback; this.errorCallback = errorCallback; PaymentRequest paymentRequest = null; - if (callToActionText != null) { - paymentRequest = new PaymentRequest() - .submitButtonText(callToActionText) - .clientToken(this.getToken()); - } else { - paymentRequest = new PaymentRequest() - .clientToken(this.getToken()); + String callToActionText = null; + String title = null; + String description = null; + String amount = null; + + if (options.hasKey("callToActionText")) { + callToActionText = options.getString("callToActionText"); + } + + if (options.hasKey("title")) { + title = options.getString("title"); + } + + if (options.hasKey("description")) { + description = options.getString("description"); + } + + if (options.hasKey("amount")) { + amount = options.getString("amount"); } + paymentRequest = new PaymentRequest() + .submitButtonText(callToActionText) + .primaryDescription(title) + .secondaryDescription(description) + .amount(amount) + .clientToken(this.getToken()); + (getCurrentActivity()).startActivityForResult( paymentRequest.getIntent(getCurrentActivity()), PAYMENT_REQUEST diff --git a/index.android.js b/index.android.js index 2453fe7d..0f625f93 100644 --- a/index.android.js +++ b/index.android.js @@ -1,33 +1,47 @@ 'use strict'; -import { NativeModules, processColor } from 'react-native'; +import {NativeModules, processColor} from 'react-native'; var Braintree = NativeModules.Braintree; module.exports = { setup(token) { return new Promise(function(resolve, reject) { - Braintree.setup(token, (test) => resolve(test), (err) => reject(err)); + Braintree.setup(token, test => resolve(test), err => reject(err)); }); }, getCardNonce(cardNumber, expirationMonth, expirationYear, cvv) { return new Promise(function(resolve, reject) { - Braintree.getCardNonce(cardNumber, expirationMonth, expirationYear, cvv, (nonce) => resolve(nonce), (err) => reject(err)) + Braintree.getCardNonce( + cardNumber, + expirationMonth, + expirationYear, + cvv, + nonce => resolve(nonce), + err => reject(err) + ); }); }, showPaymentViewController(config = {}) { var options = { callToActionText: config.callToActionText, + title: config.title, + description: config.description, + amount: config.amount, }; return new Promise(function(resolve, reject) { - Braintree.paymentRequest(options.callToActionText, (nonce) => resolve(nonce), (error) => reject(error)); + Braintree.paymentRequest( + options, + nonce => resolve(nonce), + error => reject(error) + ); }); }, showPayPalViewController() { return new Promise(function(resolve, reject) { - Braintree.paypalRequest((nonce) => resolve(nonce), (error) => reject(error)); + Braintree.paypalRequest(nonce => resolve(nonce), error => reject(error)); }); }, }; diff --git a/index.ios.js b/index.ios.js index 9a3933fd..48d2d9f3 100644 --- a/index.ios.js +++ b/index.ios.js @@ -1,65 +1,72 @@ 'use strict'; -import { NativeModules, processColor } from 'react-native'; +import {NativeModules, processColor} from 'react-native'; var RCTBraintree = NativeModules.Braintree; var Braintree = { + setupWithURLScheme(token, urlscheme) { + return new Promise(function(resolve, reject) { + RCTBraintree.setupWithURLScheme(token, urlscheme, function(success) { + success == true ? resolve(true) : reject('Invalid Token'); + }); + }); + }, - setupWithURLScheme(token, urlscheme) { - return new Promise(function(resolve, reject) { - RCTBraintree.setupWithURLScheme(token, urlscheme, function(success) { - success == true ? resolve(true) : reject("Invalid Token"); - }); - }); - }, - - setup(token) { - return new Promise(function(resolve, reject) { - RCTBraintree.setup(token, function(success) { - success == true ? resolve(true) : reject("Invalid Token"); - }); - }); - }, + setup(token) { + return new Promise(function(resolve, reject) { + RCTBraintree.setup(token, function(success) { + success == true ? resolve(true) : reject('Invalid Token'); + }); + }); + }, - showPaymentViewController(config = {}) { + showPaymentViewController(config = {}) { var options = { tintColor: processColor(config.tintColor), bgColor: processColor(config.bgColor), barBgColor: processColor(config.barBgColor), barTintColor: processColor(config.barTintColor), callToActionText: config.callToActionText, + title: config.title, + description: config.description, + amount: config.amount, }; - return new Promise(function(resolve, reject) { - RCTBraintree.showPaymentViewController(options, function(err, nonce) { - nonce != null ? resolve(nonce) : reject(err); - }); - }); - }, + return new Promise(function(resolve, reject) { + RCTBraintree.showPaymentViewController(options, function(err, nonce) { + nonce != null ? resolve(nonce) : reject(err); + }); + }); + }, - showPayPalViewController() { - return new Promise(function(resolve, reject) { - RCTBraintree.showPayPalViewController(function(err, nonce) { - nonce != null ? resolve(nonce) : reject(err); - }); - }); - }, + showPayPalViewController() { + return new Promise(function(resolve, reject) { + RCTBraintree.showPayPalViewController(function(err, nonce) { + nonce != null ? resolve(nonce) : reject(err); + }); + }); + }, getCardNonce(cardNumber, expirationMonth, expirationYear, cvv) { - return new Promise(function(resolve, reject) { - RCTBraintree.getCardNonce(cardNumber, expirationMonth, expirationYear, cvv, function(err, nonce) { - nonce != null ? resolve(nonce) : reject(err); - }); - }); + return new Promise(function(resolve, reject) { + RCTBraintree.getCardNonce( + cardNumber, + expirationMonth, + expirationYear, + cvv, + function(err, nonce) { + nonce != null ? resolve(nonce) : reject(err); + } + ); + }); }, - getDeviceData(options = {}) { - return new Promise(function(resolve, reject) { - RCTBraintree.getDeviceData(options, function(err, deviceData) { - deviceData != null ? resolve(deviceData) : reject(err); - }); - }); - } - + getDeviceData(options = {}) { + return new Promise(function(resolve, reject) { + RCTBraintree.getDeviceData(options, function(err, deviceData) { + deviceData != null ? resolve(deviceData) : reject(err); + }); + }); + }, }; module.exports = Braintree; diff --git a/ios/RCTBraintree/RCTBraintree.m b/ios/RCTBraintree/RCTBraintree.m index ef586139..bf784a91 100644 --- a/ios/RCTBraintree/RCTBraintree.m +++ b/ios/RCTBraintree/RCTBraintree.m @@ -73,6 +73,10 @@ - (instancetype)init UIColor *barBgColor = options[@"barBgColor"]; UIColor *barTintColor = options[@"barTintColor"]; + NSString *title = options[@"title"]; + NSString *description = options[@"description"]; + NSString *amount = options[@"amount"]; + if (tintColor) dropInViewController.view.tintColor = [RCTConvert UIColor:tintColor]; if (bgColor) dropInViewController.view.backgroundColor = [RCTConvert UIColor:bgColor]; @@ -91,7 +95,11 @@ - (instancetype)init dropInViewController.paymentRequest = paymentRequest; } - + + if (title) [dropInViewController.paymentRequest setSummaryTitle:title]; + if (description) [dropInViewController.paymentRequest setSummaryDescription:description]; + if (amount) [dropInViewController.paymentRequest setDisplayAmount:amount]; + [self.reactRoot presentViewController:navigationController animated:YES completion:nil]; }); } @@ -148,14 +156,14 @@ - (instancetype)init RCT_EXPORT_METHOD(getDeviceData:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) { dispatch_async(dispatch_get_main_queue(), ^{ - + NSLog(@"%@", options); - + NSError *error = nil; NSString *deviceData = nil; NSString *environment = options[@"environment"]; NSString *dataSelector = options[@"dataCollector"]; - + //Initialize the data collector and specify environment if([environment isEqualToString: @"development"]){ self.dataCollector = [[BTDataCollector alloc] @@ -167,7 +175,7 @@ - (instancetype)init self.dataCollector = [[BTDataCollector alloc] initWithEnvironment:BTDataCollectorEnvironmentSandbox]; } - + //Data collection methods if ([dataSelector isEqualToString: @"card"]){ deviceData = [self.dataCollector collectCardFraudData]; @@ -181,14 +189,14 @@ - (instancetype)init error = [NSError errorWithDomain:@"RCTBraintree" code:255 userInfo:details]; NSLog (@"Invalid data collector. Use one of: card, paypal or both"); } - + NSArray *args = @[]; if ( error == nil ) { args = @[[NSNull null], deviceData]; } else { args = @[error.description, [NSNull null]]; } - + callback(args); }); } @@ -241,9 +249,9 @@ - (void)dropInViewControllerDidCancel:(__unused BTDropInViewController *)viewCon - (UIViewController*)reactRoot { UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController; UIViewController *maybeModal = root.presentedViewController; - + UIViewController *modalRoot = root; - + if (maybeModal != nil) { modalRoot = maybeModal; }