Client reference

  • The com.google.android.gms.wallet library provides tools for integrating Google Pay into Android applications, including generating a PaymentsClient, checking user readiness for payment, and loading a payment sheet.

  • The PaymentsClient class enables interaction with Google Pay APIs, offering methods like isReadyToPay() to assess the user's payment capabilities and loadPaymentData() to initiate payment transactions.

  • IsReadyToPayRequest and PaymentDataRequest objects are used to configure the isReadyToPay() and loadPaymentData() methods, respectively, and can be constructed from JSON strings.

  • PaymentData facilitates handling Google Pay responses, offering methods like toJson() for outputting the response data, while PayButton provides a pre-built UI component for initiating Google Pay transactions and can be customized with options like button theme and corner radius.

The com.google.android.gms.wallet documentation contains full documentation for the library across multiple versions of the Google Pay API. Below is a summary of objects and methods most applicable to the most recent version of the Google Pay API: version 2.0

Wallet

Generate a PaymentsClient for a test or production environment.

Example

mPaymentsClient = Wallet.getPaymentsClient(
  this,
  new Wallet.WalletOptions.Builder()
      .setEnvironment(WalletConstants.ENVIRONMENT_TEST)
      .build()
);

PaymentsClient

A client for interacting with the Google Pay APIs.

isReadyToPay(IsReadyToPayRequest)

Use the isReadyToPay(IsReadyToPayRequest) method to determine a user's ability to return a form of payment from the Google Pay API.

If the device supports Google Pay, and the user either has already saved a payment method or can add one, IsReadyToPay returns true. To determine whether the user already saved a payment method in Google Pay, set IsReadyToPayRequest.existingPaymentMethodRequired to true.

loadPaymentData(PaymentDataRequest)

Present a Google Pay payment sheet allowing selection of a payment method and optionally configured parameters.

IsReadyToPayRequest

Build an IsReadyToPayRequest Java object suitable for use with PaymentsClient.isReadyToPay().

fromJson(java.lang.String)

Create an IsReadyToPayRequest Java object from a JSON-formatted string. See the IsReadyToPayRequest object reference for the expected structure of the provided JSON-formatted string.

PaymentDataRequest

Build a PaymentDataRequest Java object suitable for use with PaymentsClient.loadPaymentData.

fromJson(java.lang.String)

Create a PaymentDataRequest Java object from a JSON-formatted string. See the PaymentDataRequest object reference for the expected structure of the provided JSON-formatted string.

PaymentData

Handle a Google Pay API JSON-formatted response for use in your app.

toJson()

Output a Google Pay API response as a JSON-formatted string. See the PaymentData object reference for more information about the structure of the JSON object.

PayButton

public void initialize(ButtonOption buttonOptions)

The convenience method is used to initialize a Google Pay payment button styled with the latest Google Pay branding.

Arguments

Name Description
buttonOptions An object that configures the Google Pay payment button. See ButtonOptions for configurable properties.

Example: Initialize the payment button with custom configurations:

PayButton googlePayPaymentButton = layoutBinding.googlePayPaymentButton;
JSONArray paymentMethods = new JSONArray().put(getBaseCardPaymentMethod());
googlePayPaymentButton.initialize(
       ButtonOptions.newBuilder()
               .setButtonTheme(ButtonConstants.ButtonTheme.DARK)
               .setButtonType(ButtonConstants.ButtonType.BUY)
               .setCornerRadius(100)
               .setAllowedPaymentMethods(paymentMethods.toString())
               .build()
);

public void setVisibility(View view)

The convenience method sets the payment button visibility.

Arguments

Name Description
View

View.VISIBLE sets the payment button to visible (default).

View.GONE hides the button from users.

Example:

payButton.setVisibility(View.VISIBLE);

public void setOnClickListener(View.OnClickListener listener)

Method is called when the user clicks on the Google Pay payment button.

Arguments

Name Description
listener An event listener callback to call when a click event is delivered to the Google Pay payment button.

BasePaymentDataCallbacksService

Base service class which hosts the payment data callbacks initiated by Google Play services within a PaymentsClient.loadPaymentData(PaymentDataRequest) flow.

Extend this service in your application and declare it in your AndroidManifest.xml with the com.google.android.gms.permission.BIND_PAYMENTS_CALLBACK_SERVICE permission and an intent filter for com.google.android.gms.wallet.callback.PAYMENT_DATA_CALLBACKS.

createPaymentDataCallbacks()

Returns an implementation of BasePaymentDataCallbacks to handle incoming callback events. Called once per callback event.

BasePaymentDataCallbacks

Abstract base class containing the payment data callback handlers implemented by the merchant to support Dynamic Price Updates and Payment Authorization.

onPaymentDataChanged(intermediatePaymentData, onCompleteListener)

This method handles payment data changes in the payment sheet such as shipping address and shipping options.

Arguments

Name Type Description
intermediatePaymentData IntermediatePaymentData An object that contains the selected address and shipping option in the payment sheet. For details, see IntermediatePaymentData.
onCompleteListener OnCompleteListener<PaymentDataRequestUpdate> A listener to resolve with the updated transaction information, shipping options, and payment data errors. Pass a PaymentDataRequestUpdate instance to complete().

Example

override fun onPaymentDataChanged(
    intermediatePaymentData: IntermediatePaymentData,
    onCompleteListener: OnCompleteListener<PaymentDataRequestUpdate>
) {
    val paymentDataRequestUpdateJson = handlePaymentDataChanged(intermediatePaymentData.toJson())
    val update = PaymentDataRequestUpdate.fromJson(paymentDataRequestUpdateJson.toString())
    onCompleteListener.complete(update)
}

onPaymentAuthorized(paymentData, onCompleteListener)

This method is called when a payment is authorized in the payment sheet.

Arguments

Name Type Description
paymentData PaymentData An object that contains the requested shopper data. For details, see PaymentData.
onCompleteListener OnCompleteListener<PaymentAuthorizationResult> A listener to resolve with the authorization result. Pass a PaymentAuthorizationResult instance to complete().

Example

override fun onPaymentAuthorized(
    paymentData: PaymentData,
    onCompleteListener: OnCompleteListener<PaymentAuthorizationResult>
) {
    val paymentAuthorizationResultJson = handlePaymentAuthorized(paymentData.toJson())
    val result = PaymentAuthorizationResult.fromJson(paymentAuthorizationResultJson.toString())
    onCompleteListener.complete(result)
}

IntermediatePaymentData

Handle intermediate callback data passed to BasePaymentDataCallbacks.onPaymentDataChanged.

toJson()

Outputs the callback request data as a JSON-formatted string. See the IntermediatePaymentData object reference for more information about the structure of the JSON object.

getLastSavedState()

Returns the Bundle containing the state saved during the previous callback iteration.

PaymentDataRequestUpdate

Build a PaymentDataRequestUpdate Java object to return in BasePaymentDataCallbacks.onPaymentDataChanged.

fromJson(java.lang.String)

Create a PaymentDataRequestUpdate Java object from a JSON-formatted string. See the PaymentDataRequestUpdate object reference for the expected structure of the provided JSON-formatted string.

withUpdatedSavedState(Bundle)

Attaches an updated state Bundle to persist between callback invocations.

PaymentAuthorizationResult

Build a PaymentAuthorizationResult Java object to return in BasePaymentDataCallbacks.onPaymentAuthorized.

fromJson(java.lang.String)

Create a PaymentAuthorizationResult Java object from a JSON-formatted string. See the PaymentAuthorizationResult response object reference for the expected structure of the provided JSON-formatted string.