Google Pay

Client-Side Implementationanchor

Choose an integration methodanchor

You can accept Google Pay with either our Drop-in UI or a custom integration.

Drop-in integrationanchor

Our Drop-in UI is the fastest way to set up your client-side integration.

For full details, see Drop-in Setup and Integration.

Custom integrationanchor

Add Google Play Services Walletanchor

Google Pay is only available with Google Play Services Wallet 16.0.1 or later. If you are not including Google Play Services, add com.google.android.gms:play-services-wallet:16.0.1 to your build.gradle or visit Google's developer documentation for additional information.

Ensure you have also included the wallet enabled meta-data tag in your AndroidManifest.xml:

  1. XML
<meta-data android:name="com.google.android.gms.wallet.api.enabled" android:value="true"/>

Add Braintree GooglePayment componentanchor

To use Google Pay, include Braintree's google-payment component in your dependencies:

  1. Groovy
dependencies {
  // Braintree dependencies
  implementation 'com.braintreepayments.api:braintree:3.20.1'
  implementation 'com.braintreepayments.api:google-payment:3.20.1'

  // Google dependencies
  implementation 'com.google.android.gms:play-services-wallet:16.0.1'
}

Initializationanchor

First use a tokenization key or get a client token from your server and initialize BraintreeFragment.

Then register a listener to receive a payment method nonce once the Google Pay flow finishes.

Before showing the Google Pay button, use the GooglePayment#isReadyToPay method to check whether the user's current device is compatible. When this method returns true, show the Google Pay button. When it returns false, display other checkout options.

  1. Android
GooglePayment.isReadyToPay(mBraintreeFragment, new BraintreeResponseListener<Boolean>() {
  @Override
  public void onResponse(Boolean isReadyToPay) {
    if (isReadyToPay) {
      // Show Google Pay button
    }
  }
});

You will need a button element on your page styled according to Google's brand guidelines.

The CARD payment method type is supplied by the Braintree Android SDK as an allowed payment method when making the IsReadyToPayRequest. It also provides the PAYPAL payment method type if you are using Google Pay v2 and currently accept PayPal payments. If you desire different behavior for the IsReadyToPayRequest, you can follow Google's documentation and make the IsReadyToPayRequest outside of the Braintree Android SDK.

Requesting a paymentanchor

To accept Google Pay, you can either use our SDK for the entire integration or integrate with Google directly.

Direct integration with Google Play Services can be useful if you already have an existing integration with the Google Payment API or wish to integrate directly for other reasons; otherwise, we recommend using our SDK.

Braintree SDK integrationanchor

Google Pay requires a price to be specified using the TransactionInfo object. Build a TransactionInfo object and provide it at the time of payment.

  1. Android
GooglePaymentRequest googlePaymentRequest = new GooglePaymentRequest()
  .transactionInfo(TransactionInfo.newBuilder()
    .setTotalPrice("1.00")
    .setTotalPriceStatus(WalletConstants.TOTAL_PRICE_STATUS_FINAL)
    .setCurrencyCode("USD")
    .build())
  // 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.
  .billingAddressRequired(true);

Call GooglePayment#requestPayment when your Google Pay button is clicked to kick off the Google Pay flow.

  1. Android
GooglePayment.requestPayment(mBraintreeFragment, googlePaymentRequest);

If completed successfully, a GooglePaymentCardNonce or PayPalAccountNonce will be passed to your PaymentMethodNonceCreatedListener. If canceled, your BraintreeCancelListener will be called; if unsuccessful, an error will be passed to your BraintreeErrorListener.

Direct Google Play Services integrationanchor

To tokenize with the result of a successful direct Google Play Services integration, call GooglePayment#tokenize on the PaymentData received from PaymentsClient#loadPaymentData:

  1. Android
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == GOOGLE_PAYMENT_REQUEST_CODE && resultCode == RESULT_OK) {
    GooglePayment.tokenize(mBraintreeFragment, PaymentData.getFromIntent(data))
  }
}

The resulting GooglePaymentCardNonce or PayPalAccountNonce will be returned in your PaymentMethodNonceCreatedListener implementation.


Next Page: Server-side