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

No comments:

Post a Comment