Braintree SDK integration and common problem solving

Keywords: SDK Android git Session

Introduction to Braintree android sdk integration

  • Interactive schematic diagrams:

  • Interactive steps:
  1. Your app or web front-end requests a client token from your server in order to initialize the client SDK
  2. Your server generates and sends a client token back to your client with the server SDK
  3. Once the client SDK is initialized and the customer has submitted payment information, the SDK communicates that information and returns a payment method nonce
  4. You then send the payment method nonce to your server
  5. Your server code receives the payment method nonce from your client and then uses the server SDK to create a transaction or perform other functions detailed in the guides
  • Summary of Interactive Steps:

    Get token from server as initialization parameter to initialize drop-in UI, initialize order related data to transfer drop-in UI, enter the interface provided by sdk to enter the corresponding credit card number information, validate, submit to braintree server, and return after successA nonce object that submits a nonce to the server, and the server and braintree complete the specific transaction next steps.

app-side configuration and snippets

  • Build.gradleConfiguration

    dependencies {
    
    //The current version of BGA, not the latest
    implementation 'com.braintreepayments.api:braintree:2.3.12'
    implementation 'com.braintreepayments.api:drop-in:2.3.8'
    implementation 'com.braintreepayments.api:data-collector:2.3.12'
    
    }
  • Get server-side token

          //Payment using BraintreeToken interface
    public static void getBraintreeToken(Object tag, CustomCallback callback) {
    
        HashMap<String, String> paramsMap = new HashMap<>();
        //PrefsCache.getString("useCustomerId"); //Whether to record the customer, enter the card number information next time
        paramsMap.put("useCustomerId", "1");
        okGoGet(ApiUrl.getBraintreeToken, paramsMap, tag, callback);
    
    }
  • Initialize drop-in UI

     //Current BGA Version
     PaymentRequest paymentRequest = new PaymentRequest()
                    .collectDeviceData(true)
                    .primaryDescription(totalAmount)
                    .secondaryDescription(Session.getInstance().shopcartNum + " " + items)
                    .amount(mOrderConfirmModel.cartAmount)
                    .submitButtonText(purchase)
                    .clientToken(clientToken);
    
     startActivityForResult(paymentRequest.getIntent(this), BRAINTREE_REQUEST_CODE);
    
    //new version
      private DropInRequest getDropInRequest() {
    
            DropInRequest dropInRequest = new DropInRequest()
                    .amount("1.00")
                    .clientToken(mAuthorization)
                    .collectDeviceData(Settings.shouldCollectDeviceData(this))
                    .requestThreeDSecureVerification(
                            Settings.isThreeDSecureEnabled(this))
                    .androidPayCart(getAndroidPayCart())
                    .androidPayShippingAddressRequired(
                            Settings.isAndroidPayShippingAddressRequired(this))
                    .androidPayPhoneNumberRequired(
                            Settings.isAndroidPayPhoneNumberRequired(this))
                    .androidPayAllowedCountriesForShipping(
                            Settings.getAndroidPayAllowedCountriesForShipping(this));
    
            return dropInRequest;
       }
    
      startActivityForResult(getDropInRequest().getIntent(this), DROP_IN_REQUEST);  
    
  • Get nonce and DeviceData submitted to the server

    //BGA current code (no DeviceData returned, new sdk returned with nonce)
      @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == BRAINTREE_REQUEST_CODE) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    if (data != null) {
                        PaymentMethodNonce mNonce =data.getParcelableExtra
                        (BraintreePaymentActivity.EXTRA_PAYMENT_METHOD_NONCE);
                        braintreePurchased(mNonce);
                    }
                    break;
                default:
                    break;
            }
        }
    
    //New version of sdk code
     @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         DropInResult result = data.getParcelableExtra(
         DropInResult.EXTRA_DROP_IN_RESULT);
         displayResult(result.getPaymentMethodNonce(), result.getDeviceData());
    
        }
    }   
    
    //DeviceData    
    DataCollector.collectDeviceData(braintreeFragment, kountMerchantId, new BraintreeResponseListener<String> {
          @Override
          public void onResponse(String deviceData) {
            // send deviceData to your server
          }
    });

Common problem

  1. DeviceData was acquired, along with the latest version when nonce was acquired
  2. BraintreeFragment was created without saying that the application was switched to the background
  3. Network is easy to report if it is listening: SERVER_ERROR, tests are encountered.
  4. Note the currencies supported by braintree and refresh clientToken when switching currencies
  5. braintree can set whether to log customer information and exempt from input next time (background control)
  6. There is a limit on how much braintree can pay for the order. The BG side is between $1 and $500.

Posted by barrylee on Wed, 08 Jul 2020 09:14:12 -0700