Skip to content

Commit

Permalink
added credit card validation for android
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnas Milaševičius committed May 24, 2017
1 parent 11fe781 commit 7f32a6b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ dependencies {
compile "com.facebook.react:react-native:+"
compile 'com.braintreepayments.api:braintree:2.+'
compile 'com.braintreepayments.api:drop-in:2.+'
compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1'
}
49 changes: 48 additions & 1 deletion android/src/main/java/com/pw/droplet/braintree/Braintree.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.pw.droplet.braintree;

import java.util.Map;
import java.util.HashMap;

import com.google.gson.Gson;

import android.content.Intent;
import android.content.Context;
import android.app.Activity;
Expand All @@ -9,10 +14,13 @@
import com.braintreepayments.api.BraintreePaymentActivity;
import com.braintreepayments.api.BraintreeFragment;
import com.braintreepayments.api.exceptions.InvalidArgumentException;
import com.braintreepayments.api.exceptions.BraintreeError;
import com.braintreepayments.api.exceptions.ErrorWithResponse;
import com.braintreepayments.api.models.CardBuilder;
import com.braintreepayments.api.Card;
import com.braintreepayments.api.PayPal;
import com.braintreepayments.api.interfaces.PaymentMethodNonceCreatedListener;
import com.braintreepayments.api.interfaces.BraintreeErrorListener;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
Expand Down Expand Up @@ -59,6 +67,41 @@ public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
nonceCallback(paymentMethodNonce.getNonce());
}
});
this.mBraintreeFragment.addListener(new BraintreeErrorListener() {
@Override
public void onError(Exception error) {
if (error instanceof ErrorWithResponse) {
ErrorWithResponse errorWithResponse = (ErrorWithResponse) error;
BraintreeError cardErrors = errorWithResponse.errorFor("creditCard");
if (cardErrors != null) {
Gson gson = new Gson();
final Map<String, String> errors = new HashMap<>();
BraintreeError numberError = cardErrors.errorFor("number");
BraintreeError cvvError = cardErrors.errorFor("cvv");
BraintreeError expirationDateError = cardErrors.errorFor("expirationDate");
BraintreeError expirationYearError = cardErrors.errorFor("expirationYear");

if (numberError != null) {
errors.put("card_number", numberError.getMessage());
}

if (cvvError != null) {
errors.put("cvv", cvvError.getMessage());
}

if (expirationDateError != null) {
errors.put("expiration_date", expirationDateError.getMessage());
}

if (expirationYearError != null) {
errors.put("expiration_year", expirationYearError.getMessage());
}

nonceErrorCallback(gson.toJson(errors));
}
}
}
});
this.setToken(token);
successCallback.invoke(this.getToken());
} catch (InvalidArgumentException e) {
Expand All @@ -76,7 +119,7 @@ public void getCardNonce(final String cardNumber, final String expirationMonth,
.expirationMonth(expirationMonth)
.expirationYear(expirationYear)
.cvv(cvv)
.validate(false);
.validate(true);

Card.tokenize(this.mBraintreeFragment, cardBuilder);
}
Expand All @@ -85,6 +128,10 @@ public void nonceCallback(String nonce) {
this.successCallback.invoke(nonce);
}

public void nonceErrorCallback(String error) {
this.errorCallback.invoke(error);
}

@ReactMethod
public void paymentRequest(final String callToActionText, final Callback successCallback, final Callback errorCallback) {
this.successCallback = successCallback;
Expand Down

0 comments on commit 7f32a6b

Please sign in to comment.