Android Braintree SDK - onPaymentMethodNonce () never called

We use the Braintree SDK in our app along with PayPal to enable users to make in-app purchases. Unfortunately, we were unable to generate a nonce from PayPal billing information or user-entered credit card information using Braintree.

Below is some context for our specific implementation:

We initialize the Braintree object with our client token as follows:

Braintree braintree = Braintree.getInstance(context, token);

      

We also installed a Braintree listener on our fragment, which implements Braintree.PaymentMethodNonceListener. In ActivityCreated (), we initialize the Braintree object using the above code. onPause () we remove the listener and onResume () add the listener.

In the app, we allow users to pay with PayPal or credit card.

If the user chooses to use PayPal, we will fire the PayPal intent using a unique request code. Using the following block of code, we get the response from the PayPal action completion.

@Override
public void onActivityResult(int requestCode, int responseCode, Intent data) {
    if (requestCode == PAYPAL_REQUEST_CODE) {
        if (responseCode == FragmentActivity.RESULT_OK) {
            braintree.finishPayWithPayPal(getActivity(), responseCode, data);
        }
    }
}

      

According to the Braintree SDK documentation, here's it here ,

In addition, we provide the user with the ability to enter credit card information, which we then use to create a CardBuilder object, which is passed to the Braintree object using the "tokenize" method, as shown in the following snippet:

 CardBuilder cardBuilder = new CardBuilder()
                .cardNumber(cardNumber)
                .expirationDate(expirationDate);
        braintree.tokenize(cardBuilder);

      

As per Braintree developer documentation, look here , after calling tokenize, expect onPaymentMethodNonce callback; at this point, the nonce is taken as the user's entered credit card information.


TL / DR:

Based on our implementation described above, we currently have (2) problems that we were unable to resolve:

1) onActivityResult ()

The method never gets called, even though it entered valid PayPal credentials in the PayPal activity window and resumed its application. After searching the web, we found a potential bug in Android regarding the parent activity for a fragment not calling super.onActivityResult (). onActivityResult not called in Fragment onActivityResult () not called when Activity is started from Fragment onActivityResult not called in android fragment https://code.google.com/p/android/issues/detail?id=15394 Unfortunately, none of the advice in these posts seemed to allow onActivityResult to ever be called in our fragment. Which in turn causes finalPayWithPaypal () never to call the Braintree object. Which finally causes onPaymentMethodNonce to never fire at our nonce listener snippet.

Update for onActivityResult () callback issue:

Per Luke's recommendation in the comments section, I have implemented a resolution for the onActivityResult () callback problem:

In the payment transaction, we will override onActivityResult (), which then calls the Fragment's onActivityResult method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.payment_fragment);
    if (fragment != null){
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

      

The onActivityResult () method is now called when a PayPal operation completes. Thanks for your help Luke.

* Note. Make sure the fragment you are trying to reference is of the same fragment type that findFragmentById () returns! If the Fragment is android.app.Fragment then use Activity getFragmentManager (). FindFragmentById (), which returns the Fragment of the application. If the Fragment is android.support.v4.app.Fragment then use Activity getSupportFragmentManager (). FindFragmentId (), which returns a v4. Otherwise, the Fragment type mismatch will result in null being returned. While this may sound intuitive, it is very annoying to debug given the subtle difference in types!

2) onPaymentMethodNonce ()

The method is never called. After calling tokenize () or finishWithPayPal () on a Braintree object, the call seems to get lost in the "Braintree world". After setting breakpoints and going through the implementation, we were unable to find any trace that Braintree either failed or succeeded in marking a card or processing a PayPal result.


Environment

Gradle Build Information:

  • compileSdkVersion 19
  • buildToolsVersion 20.0.0
  • minSdkVersion 11
  • targetSdkVersion 19
  • versionCode 5
  • versionName 1.4.1

Equipment testing:

  • Samsung DUOS
  • Model GT-S7582
  • Android version 4.2.2

Thank you for your help.

+3


source to share





All Articles