Choose an integration method

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

Drop-in integration

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 integration

Get the SDK

See the client SDK setup guide for Android v2. Google Pay requires version 2.7.0 or higher.

Google Pay is only available with Google Play Services Wallet 11.4.0 or later. If you are not including Google Play Services, add com.google.android.gms:play-services-wallet:+ 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:

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

Initialization

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.

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

The Braintree Android SDK uses WalletConstants#PAYMENT_METHOD_CARD and WalletConstants#PAYMENT_METHOD_TOKENIZED_CARD as allowed payment methods when making the IsReadyToPayRequest. 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 payment

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 integration

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

Java
Copy
Copied
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 transactions as a best practice.
  .billingAddressRequired(true);

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

Java
Copy
Copied
GooglePayment.requestPayment(mBraintreeFragment, googlePaymentRequest);

If completed successfully, a GooglePaymentCardNonce 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 integration

PaymentMethodTokenizationParameters from Braintree can be used to integrate directly with the Google Payment API.

Request the PaymentMethodTokenizationParameters to use when building the PaymentDataRequest:

Java
Copy
Copied
GooglePayment.getTokenizationParameters(mBraintreeFragment, new TokenizationParametersListener() {
  @Override
  public void onResult(PaymentMethodTokenizationParameters parameters, Collection<Integer> allowedCardNetworks) {
    PaymentDataRequest paymentDataRequest = PaymentDataRequest.newBuilder()
      .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_CARD)
      .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_TOKENIZED_CARD)
      .setPaymentMethodTokenizationParameters(parameters)
      .setCardRequirements(CardRequirements.newBuilder()
        .addAllowedCardNetworks(allowedCardNetworks)
        // We recommend collecting billing address information, at minimum
        // billing postal code, and passing that billing postal code with all
        // Google Pay transactions as a best practice.
        .setBillingAddressRequired(true)
        .build())
      .build();
  }
});

If you receive a successful result after calling PaymentsClient#loadPaymentData, call GooglePayment#tokenize to get a nonce.

Java
Copy
Copied
@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 will be returned in your PaymentMethodNonceCreatedListener implementation.

Next Page: Server-side →

Still have questions?

If you can’t find an answer, contact our Support team