How To Integrate Google Pay in Your Android Application (With Demo)

In this tutorial, we will learn to integrate the google pay payment gateway into our android application.

How To Integrate Google Pay in Your Android Application (With Demo)

Integration Steps

We will Use Braintree google pay API to integrate google pay in our project, so first of all, you need to add these lines in your app-level build.gradle file

    implementation 'com.braintreepayments.api:braintree:2.22.0'
    implementation 'com.braintreepayments.api:google-payment:2.0.1'
    implementation ''

The next step is to add metadata in your android manifest file copy the below code and put it in your android manifest file.


Braintree has provided  Drop-in UI API and custom API for google pay integration, we will learn to integrate both API. So First step is to generate Braintree Client Token. Client Token is required for google pay integration using Braintree API. For to get a client token you need to host some PHP code in your server. I have provided a sample PHP server-side code. you can download it from the link given below. 

Download PHP Files

After Downloading, you will see a zip file by the name googlepay-serverside. unzip that file and then open the folder inside this zip file. in that folder, you will an index.php file. open index.php file and put your own braintree API keys there.

How to Get your Own Braintree API Keys?

For API keys you first have to register on the Braintree website Click here to register your account on the Braintree website for testing purposes you can register with a sandbox account. After registration, Go to the dashboard and there click on the setting icon at the top right side of the windows there you have to generate your API keys simply copy those API keys and put them in index.php file. 

one more thing that you have to do is, enable the google pay payment gateway from the Braintree dashboard. 

Android Side Google pay integration Code

If you have completed all previous steps then now you are ready to write code for google pay payment gateway android side implementation.

The first step is to generation Braintree Client token, for that we need to send GET request to the index.php file. For to Send GET request to index.php file First we need to host PHP code on the server. you can host it on localhost or on any online web hosting server.

Many Libraries are available on GitHub that we can use for GET requests. we are using loopj library in this turorial, if you want to use this one just simply add this line in your app-level build.gradle file

  implementation ''

Now let's start  writing code for to get client token.

        AsyncHttpClient client = new AsyncHttpClient();

        client.get("your index.php file url", new TextHttpResponseHandler() {

            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Toast.makeText(DeliveryActivity.this, "thiss "+throwable.getMessage(), Toast.LENGTH_SHORT).show();

            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                String clientToken = responseString; // this is the client token that we need for google pay object   
              //  Toast.makeText(DeliveryActivity.this, "client toen"+clientToken, Toast.LENGTH_SHORT).show();

                try {
                 BraintreeFragment   mBraintreeFragment = BraintreeFragment.newInstance(DeliveryActivity.this, clientToken); // brantree fragment also required

                   // mBraintreeFragment = BraintreeFragment.newInstance(DeliveryActivity.this, clientToken);
                    // mBraintreeFragment is ready to use!
                   // setupBraintreeAndStartExpressCheckout();

                } catch (Exception e) {
                    // There was an issue with your authorization string.


as you can see in the above code after getting the client token we called onbraintreesubmit method with Braintreefragment and client token as parameters.

so here is the method code

this code is for Braintree Drop-in UI API. if you want to use custom UI then you can skip this step

    public void onBraintreeSubmit(BraintreeFragment btfragment,String clientToken) {
      DropInRequest dropInRequest = new DropInRequest()
        startActivityForResult(dropInRequest.getIntent(this), REQUEST_CODE);

In Braintree dropship API you also need to enable google pay so for that you have to write this code. as you can see we already called enableGooglePay method in onBraintreeSubmit method 

here is enableGooglePay Method Code

private void enableGooglePay(DropInRequest dropInRequest) {
        GooglePaymentRequest googlePaymentRequest = new GooglePaymentRequest()
                // We recommend collecting and passing billing address information
                // with all Google Pay transactions as a best practice.
        // Optional in sandbox; if set in sandbox, this value must be
        // a valid production Google Merchant ID.


now we will write code for API payment response,  you will get Success or Failure response in onActivityResult method here is the complete code of onActivityResult

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
                String nones = result.getPaymentMethodNonce().getNonce();
              } else if (resultCode == RESULT_CANCELED) {
                // the user canceled
            } else {
                // handle errors here, an exception may be available in
                Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);

            else {
       //     Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show();


if we will receive Result ok in on activity result method it's mean the transaction has done successfully. 

Now let's talk about Custom google pay integration using Braintree API. for custom integration you don't need to generate a client token you can simply call the below method and it's done. 

    GooglePayment.isReadyToPay(mBraintreeFragment, new BraintreeResponseListener() {
            public void onResponse(Boolean isReadyToPay) {
                if (isReadyToPay) {
                    GooglePaymentRequest googlePaymentRequest = new GooglePaymentRequest()
                            // We recommend collecting billing address information, at minimum
                            // billing postal code, and passing that billing postal code with all
                            // Google Pay card transactions as a best practice.
                    GooglePayment.requestPayment(mBraintreeFragment, googlePaymentRequest);

                    Toast.makeText(DeliveryActivity.this, "This Device Not Supported Google Pay", Toast.LENGTH_SHORT).show();

as you can see we have to pass an object of  BraintreeFragment in the isReadyToPay method. I already told you how you can create an Object of BraintreeFragment

when you will run this code you will receive a success or failure response in the onActivityResult override method of activity. If you will receive success response it's mean the transaction successfully done.

if you have any query please write a comment below.

What's Your Reaction?