AI-generated Key Takeaways
-
The
com.google.android.gms.wallet
library provides tools for integrating Google Pay into Android applications, including generating aPaymentsClient
, checking user readiness for payment, and loading a payment sheet. -
The
PaymentsClient
class enables interaction with Google Pay APIs, offering methods likeisReadyToPay()
to assess the user's payment capabilities andloadPaymentData()
to initiate payment transactions. -
IsReadyToPayRequest
andPaymentDataRequest
objects are used to configure theisReadyToPay()
andloadPaymentData()
methods, respectively, and can be constructed from JSON strings. -
PaymentData
facilitates handling Google Pay responses, offering methods liketoJson()
for outputting the response data, whilePayButton
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 |
|
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. |