Integrate Google Pay Merchant SDK with your appStay organized with collectionsSave and categorize content based on your preferences.
Page Summary
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 aPaymentsClientinstance, check for Google Pay availability using theIsReadyToPayAPI, and initiate the payment process withloadPaymentData.
The guide details how to handle the payment response inonActivityResult, including extracting payment data and managing potential errors for a smooth user experience.
It covers essential aspects like setting up anonClickListenerto trigger the Google Pay flow and processing thepaymentDataResponseobject for transaction completion.
This guide explains how to integrate the Google Pay Merchant SDK (or Google Pay SDK) with your app.
Step 1: CreatePaymentsClientinstance
Create an instance ofPaymentsClientin theonCreatemethod of yourActivityclass. This allows interaction with the APIs in the Google Pay SDK
test kit.
Implement theIsReadyToPaymethod, 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: CallloadPaymentData
CallingloadPaymentDatastarts a Google Pay Activity to facilitate the payment via intent. Once the user
has completed requesting payment, aPaymentDataresponse is returned from the
Activity viaonActivityResult. The caller must implementonActivityResultto
handle thePaymentDataResponse. To construct thepaymentDataRequestJSON,
refer to theloadPaymentDatasection.
Step 4: Create anonClickListenerobject
ThepaymentDataRequestobject is a Parcelable representing a payment data
request, which provides the necessary information to support a payment. Use theautoResolveHelperclass to auto resolve the Task, and then handle the result
in theonActivityResultmethod of yourActivityclass.
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 thepaymentDataResponseobject
When you callloadPaymentData, aPaymentDataobject is returned toonActivityResult. Parse thepaymentDataobjectto obtain payment credentials
that can be charged with your payment provider. The format of the JSON is
defined in theloadPaymentDatasection.
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"]]