Integrate Google Pay Merchant SDK with your app
Stay organized with collections
Save and categorize content based on your preferences.
AI-generated Key Takeaways
This guide provides step-by-step instructions for integrating the Google Pay Merchant SDK into your Android application for seamless payment processing.
Developers learn how to create a PaymentsClient instance, check for Google Pay availability using the IsReadyToPay API, and initiate the payment process with loadPaymentData.
The guide details how to handle the payment response in onActivityResult, including extracting payment data and managing potential errors for a smooth user experience.
It covers essential aspects like setting up an onClickListener to trigger the Google Pay flow and processing the paymentDataResponse object for transaction completion.
This guide explains how to integrate the Google Pay Merchant SDK (or Google Pay SDK) with your app.
Step 1: Create PaymentsClient instance
Create an instance of PaymentsClient in the onCreate method of your
Activity class. This allows interaction with the APIs in the Google Pay SDK
test kit.
Implement the IsReadyToPay
method, as shown in the following example:
privatevoidisReadyToPay(){Task<Boolean>task=paymentsClient.isReadyToPay(request);task.addOnCompleteListener(newOnCompleteListener<Boolean>(){publicvoidonComplete(Task<Boolean>task){try{booleanresult=task.getResult(RuntimeException.class);if(result==true){// Show Google as payment option.}else{// Hide Google as payment option.}}catch(RuntimeExceptionexception){// Handle exception.}}});}
Step 3: Call loadPaymentData
Calling loadPaymentData
starts a Google Pay Activity to facilitate the payment via intent. Once the user
has completed requesting payment, a PaymentData response is returned from the
Activity via onActivityResult. The caller must implement onActivityResult to
handle the PaymentDataResponse. To construct the paymentDataRequest JSON,
refer to the loadPaymentData
section.
Step 4: Create an onClickListener object
The paymentDataRequest object is a Parcelable representing a payment data
request, which provides the necessary information to support a payment. Use the
autoResolveHelper class to auto resolve the Task, and then handle the result
in the onActivityResult method of your Activity class.
payWithGPay.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewview){payWithGPay.setEnabled(false);// This transfers the control to the Google Pay app and the result of the transaction// will be returned in onActivityResult with the given request code.paymentsClient.loadPaymentData(this,paymentDataRequestJson,LOAD_PAYMENT_DATA_REQUEST_CODE);}});
Step 5: Handle the paymentDataResponse object
When you call loadPaymentData, a PaymentData object is returned to
onActivityResult. Parse the paymentDataobject to obtain payment credentials
that can be charged with your payment provider. The format of the JSON is
defined in the
loadPaymentData section.
To ensure forward compatibility and robustness, parse only the specific fields
your application requires from the JSON response. Avoid validating the exact
field count, as our API may evolve to include additional data over time without
constituting a breaking change.
@OverridepublicvoidonActivityResult(intrequestCode,intresultCode,Intentdata){if(requestCode==LOAD_PAYMENT_DATA_REQUEST_CODE){// Handle the transaction result here.switch(resultCode){caseActivity.RESULT_OK:StringpaymentData=WalletUtils.getPaymentDataFromIntent(data);// Handle the payment data response in order to complete the transaction.break;caseActivity.RESULT_FIRST_USER:intstatusCode=data.getIntExtra(WalletConstants.EXTRA_ERROR_CODE,WalletConstants.INTERNAL_ERROR);handleResultStatusCode(statusCode);break;caseActivity.RESULT_CANCELED:// Nothing to here normally - the user simply cancelled without selecting a// payment method.break;}}}privatevoidhandleResultStatusCode(intstatusCode){switch(statusCode){caseWalletConstants.ERROR_CODE_BUYER_ACCOUNT_ERROR:// Prompt the user that there is a problem with their account.break;caseWalletConstants.ERROR_CODE_MERCHANT_ACCOUNT_ERROR:// Merchant account error - handle as necessary.break;caseWalletConstants.ERROR_CODE_UNSUPPORTED_API_VERSION:caseWalletConstants.INTERNAL_ERROR:caseWalletConstants.DEVELOPER_ERROR:default:thrownewIllegalStateException("Internal error.");}}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-04-02 UTC."],[],["This guide outlines the integration of the Google Pay SDK into an app. Key actions include: creating a `PaymentsClient` instance, calling the `IsReadyToPay` API to determine Google Pay's availability, and calling `loadPaymentData` to initiate the payment process. An `onClickListener` is implemented to trigger `loadPaymentData`, which returns a `PaymentData` object. Finally, the app must handle the `paymentDataResponse` in `onActivityResult`, parsing the object for payment credentials and managing transaction results or errors.\n"]]