Dedicated Server

Paypal Integration in android using Braintree SDK

performing paypal payments using braintree android sdk
Braintree is a division of PayPal and is a company based in Chicago that specializes in mobile and web payment systems. Braintree provides client libraries and integration examples in Ruby, Python, PHP, Java, .NET, and Node JS. It also provides mobile libraries for iOS and Android, and Braintree.js for in-browser card encryption.
Braintree payment gateway is basically a paypal service, so we would require braintree sdks to be integrated in our android code. We can then link our paypal credentials with braintree dashboard.


We will see how to integrate braintree  sdk in android and perform paypal transactions
  1. First of all go here https://www.paypal.com/in/webapps/mpp/merchant and create a paypal merchant account. You may choose to create shoppers account or business account or freelancers account based on your needs.
  2. As soon as the above step is complete, head over to the paypal developer website here https://developer.paypal.com/ and login with the above generated credentials. Once you are logged in click on your username at the top right corner and then click on dashboard. Now click on "create app" option in your dashboard. Now fill in the app name for your sandbox environment and complete the app creation. As soon as the app is created, you can see that the client id, client secret has beem generated has been generated.
  3. Now go here and create braintree sandbox account >> https://sandbox.braintreegateway.com/ . Once the account is created, goto Settings >> processing >> paypal. Now toggle paypal to turn it on. Now click on "options" next to the paypal field. Now enter the paypal email, client id, secret id generated in step 2 and click update to save. Now we have successfully linked paypal with braintree.
  4. Once you are logged in with the braintree sandbox account, you can goto Account >> Merchant Account Info and then click "new sandbox merchant account" to create a braintree merchant account for testing purposes. The braintree merchant account is not like a bank account, basically this account directs funds from your customers to your business bank account.
  5. You may create any number of  braintree merchant account like shown above. While creating the braintree merchant account, it can be set as the default merchant account so that the transactions will be using it as the default one.
As of now the initial dashboard setup is now complete.

Follow the below steps for integration :

  1.  The first step is the generation of client token at the server side using your server side braintree library. We will be following .NET library for server side integrations. Check here for other formats available
Following library supports both .NET Framework 4.5.2+, .NET Core 1.0+ and higher.

  • Version: 4.8.0
  • SHA256: a700b98818c1f4266701160a1059d75f1cb17aa8d8ae12696c6812264781d932
        2.  Once the braintree library is setup login to your braintree sandbox account here >>                       https://sandbox.braintreegateway.com/ and goto settings >> API Keys. Now click on 
"Generate new API key". Now you will get a public key, private key and merchant id if you click the "view" option in the generated API Key. Now add the above generated values as shown below

        3. Now that you have setup the braintree sandbox, the next step is the generation of client token, which will be required at the client side to initialise the client side braintree sdk(in our case >> android braintree sdk). Inorder to allow return customers to choose previously chosen payment method options, you can make use of a customerid like shown below

Now generate the client token and expose it to the clients like shown below

         4. Now, the client(android app) requests the client token and intialieses the client side sdk using this client token. Once the sdk is initialised, the client generates a payment_method_nonce value and sends it to the server, the server should accept the payment_method_nonce value like shown below

         5. You have now successfully accepted the payment_method_nonce value from the client which depicts client side authorisation to create transaction. So create a transaction  like shown below, using an amount and the payment_method_nonce you received from client

Now let us see some client side code using android

Add the following dependency in your app level build.gradle

implementation 'com.braintreepayments.api:drop-in:3.7.0' 
implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4' 
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

In our server side implementation, we have already created an api to expose client token(check step 3 in server side integration). We will need to get this client token to initialise our braintree payment gateway sdk in our app side. So write a network call to fetch client token like shown below(here we are using retrofit library for network calls)
and create RequestInterface.java like this Please note that you have to set the base url and endpoint according to the one that you have created in step 3 in server side integration
As you can see above we need to pass the client token like this onBraintreeSubmit(response.body().toString()); to the braintree sdk from the onResponse method of our retrofit instance. So we need to write the code for this method like shown below
Now we will need to override onActivityResult as shown below, so that we can get the results from the braintree sdk
As shown above, we have successfully obtained the nonce value from the braintree sdk like this String payment_method_nonce=result.getPaymentMethodNonce().getNonce(); . Now we will need to send this nonce value along with an amount to our server side implementation to perform the transaction at the server side. So create submitNonce() method like shown below :
now update your RequestInterface.java like shown below

11 comments:

  1. response parsing error while following same procedure

    ReplyDelete
    Replies
    1. are you able to fetch the client id generated by your server side sdk

      Delete
  2. i just want to have an android app which supports paypal. (no service stuff) isn't possible to just have an acitive paypal acount and as soon as people uses the app and pay for the service, the money lands into my account? This seems to complicated to me. I can't run you example either. I think you are missing steps or things are "trivial" understandable for you and not for the readers without paypal/braintree knowledge. please improve it and give more details.

    ReplyDelete
    Replies
    1. sorry for the late reply, i wasn't around for a while. About your question, you can't directly accept payments from an android app just by having an active paypal account, you will need to have a paypal merchant account account for that about which i have discussed in the post.

      Delete
  3. which url we have to apply in your_payments_endpoints

    ReplyDelete
  4. I am getting an error "ClassLoader referenced unknown path: system/framework/mediatek-cta.jar" Please give me solution for this.

    ReplyDelete
  5. I am getting an error "D/Err: com.braintreepayments.api.exceptions.InvalidArgumentException: Tokenization Key or client token was invalid." please help me with this as soon as possible.

    ReplyDelete
  6. I am getting a unknown status in response of submitnonce please help me with this.

    ReplyDelete
  7. I really loved reading your blog. I also found your posts very interesting. If you want to open merchant account then visit our website.

    ReplyDelete
  8. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.

    ESTIMATES

    ReplyDelete
  9. Hi, I am receiving error "Merchant account not found" error while clicking on paypal option from drop-in UI on android side.

    I have only single merchant account available on my braintree sandbox account.

    And on server side python team is using the above code as mentioned in Step 2 to generate client token.

    I searched out all over, but found nothing. Can you please help?

    ReplyDelete