Skip to content

Commit

Permalink
Merge pull request #49 from aroth/master
Browse files Browse the repository at this point in the history
Add support for title, description and amount options.
  • Loading branch information
kraffslol authored Jul 5, 2017
2 parents 0853ad3 + 06aee2c commit 5c78c99
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 65 deletions.
36 changes: 28 additions & 8 deletions android/src/main/java/com/pw/droplet/braintree/Braintree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions index.android.js
Original file line number Diff line number Diff line change
@@ -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));
});
},
};
93 changes: 50 additions & 43 deletions index.ios.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 17 additions & 9 deletions ios/RCTBraintree/RCTBraintree.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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];
});
}
Expand Down Expand Up @@ -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]
Expand All @@ -167,7 +175,7 @@ - (instancetype)init
self.dataCollector = [[BTDataCollector alloc]
initWithEnvironment:BTDataCollectorEnvironmentSandbox];
}

//Data collection methods
if ([dataSelector isEqualToString: @"card"]){
deviceData = [self.dataCollector collectCardFraudData];
Expand All @@ -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);
});
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5c78c99

Please sign in to comment.