Skip to content

Commit

Permalink
[Android] Add 3DSecure (#75)
Browse files Browse the repository at this point in the history
* [Android] Add 3DSecure

* Handle errors
  • Loading branch information
Minishlink authored and kraffslol committed Dec 19, 2017
1 parent 96fd6a0 commit cbf4def
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
28 changes: 26 additions & 2 deletions android/src/main/java/com/pw/droplet/braintree/Braintree.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.content.Context;
import android.app.Activity;

import com.braintreepayments.api.ThreeDSecure;
import com.braintreepayments.api.PaymentRequest;
import com.braintreepayments.api.models.PaymentMethodNonce;
import com.braintreepayments.api.BraintreePaymentActivity;
Expand All @@ -22,6 +23,7 @@
import com.braintreepayments.api.PayPal;
import com.braintreepayments.api.interfaces.PaymentMethodNonceCreatedListener;
import com.braintreepayments.api.interfaces.BraintreeErrorListener;
import com.braintreepayments.api.models.CardNonce;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -41,6 +43,8 @@ public class Braintree extends ReactContextBaseJavaModule implements ActivityEve

private BraintreeFragment mBraintreeFragment;

private ReadableMap threeDSecureOptions;

public Braintree(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(this);
Expand Down Expand Up @@ -72,7 +76,18 @@ public void onCancel(int requestCode) {
this.mBraintreeFragment.addListener(new PaymentMethodNonceCreatedListener() {
@Override
public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
nonceCallback(paymentMethodNonce.getNonce());
if (threeDSecureOptions != null && paymentMethodNonce instanceof CardNonce) {
CardNonce cardNonce = (CardNonce) paymentMethodNonce;
if (!cardNonce.getThreeDSecureInfo().isLiabilityShiftPossible()) {
nonceErrorCallback("3DSECURE_NOT_ABLE_TO_SHIFT_LIABILITY");
} else if (!cardNonce.getThreeDSecureInfo().isLiabilityShifted()) {
nonceErrorCallback("3DSECURE_LIABILITY_NOT_SHIFTED");
} else {
nonceCallback(paymentMethodNonce.getNonce());
}
} else {
nonceCallback(paymentMethodNonce.getNonce());
}
}
});
this.mBraintreeFragment.addListener(new BraintreeErrorListener() {
Expand Down Expand Up @@ -216,6 +231,10 @@ public void paymentRequest(final ReadableMap options, final Callback successCall
amount = options.getString("amount");
}

if (options.hasKey("threeDSecure")) {
this.threeDSecureOptions = options.getMap("threeDSecure");
}

paymentRequest = new PaymentRequest()
.submitButtonText(callToActionText)
.primaryDescription(title)
Expand Down Expand Up @@ -244,7 +263,12 @@ public void onActivityResult(Activity activity, final int requestCode, final int
PaymentMethodNonce paymentMethodNonce = data.getParcelableExtra(
BraintreePaymentActivity.EXTRA_PAYMENT_METHOD_NONCE
);
this.successCallback.invoke(paymentMethodNonce.getNonce());

if (this.threeDSecureOptions != null) {
ThreeDSecure.performVerification(this.mBraintreeFragment, paymentMethodNonce.getNonce(), String.valueOf(this.threeDSecureOptions.getDouble("amount")));
} else {
this.successCallback.invoke(paymentMethodNonce.getNonce());
}
break;
case BraintreePaymentActivity.BRAINTREE_RESULT_DEVELOPER_ERROR:
case BraintreePaymentActivity.BRAINTREE_RESULT_SERVER_ERROR:
Expand Down
1 change: 1 addition & 0 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
title: config.title,
description: config.description,
amount: config.amount,
threeDSecure: config.threeDSecure,
};
return new Promise(function(resolve, reject) {
Braintree.paymentRequest(
Expand Down

0 comments on commit cbf4def

Please sign in to comment.