Integrate Google Pay Merchant SDK with your app
Stay organized with collections
Save and categorize content based on your preferences.
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.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
paymentsClient = Wallet.getPaymentsClient();
}
Step 2: Call IsReadyToPay
API
Implement the IsReadyToPay
method, as shown in the following example:
private void isReadyToPay() {
Task<Boolean> task = paymentsClient.isReadyToPay(request);
task.addOnCompleteListener(
new OnCompleteListener<Boolean>() {
public void onComplete(Task<Boolean> task) {
try {
boolean result = task.getResult(RuntimeException.class);
if (result == true) {
// Show Google as payment option.
} else {
// Hide Google as payment option.
}
} catch (RuntimeException exception) {
// 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(
new OnClickListener() {
@Override
public void onClick(View view) {
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.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == LOAD_PAYMENT_DATA_REQUEST_CODE) {
// Handle the transaction result here.
switch (resultCode) {
case Activity.RESULT_OK:
String paymentData = WalletUtils.getPaymentDataFromIntent(data);
// Handle the payment data response in order to complete the transaction.
break;
case Activity.RESULT_FIRST_USER:
int statusCode = data.getIntExtra(WalletConstants.EXTRA_ERROR_CODE,
WalletConstants.INTERNAL_ERROR);
handleResultStatusCode(statusCode);
break;
case Activity.RESULT_CANCELED:
// Nothing to here normally - the user simply cancelled without selecting a
// payment method.
break;
}
}
}
private void handleResultStatusCode(int statusCode) {
switch (statusCode) {
case WalletConstants.ERROR_CODE_BUYER_ACCOUNT_ERROR:
// Prompt the user that there is a problem with their account.
break;
case WalletConstants.ERROR_CODE_MERCHANT_ACCOUNT_ERROR:
// Merchant account error - handle as necessary.
break;
case WalletConstants.ERROR_CODE_UNSUPPORTED_API_VERSION:
case WalletConstants.INTERNAL_ERROR:
case WalletConstants.DEVELOPER_ERROR:
default:
throw new IllegalStateException("Internal error.");
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-02 UTC.
[null,null,["Last updated 2025-04-02 UTC."],[[["\u003cp\u003eThis guide provides step-by-step instructions for integrating the Google Pay Merchant SDK into your Android application for seamless payment processing.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers learn how to create a \u003ccode\u003ePaymentsClient\u003c/code\u003e instance, check for Google Pay availability using the \u003ccode\u003eIsReadyToPay\u003c/code\u003e API, and initiate the payment process with \u003ccode\u003eloadPaymentData\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe guide details how to handle the payment response in \u003ccode\u003eonActivityResult\u003c/code\u003e, including extracting payment data and managing potential errors for a smooth user experience.\u003c/p\u003e\n"],["\u003cp\u003eIt covers essential aspects like setting up an \u003ccode\u003eonClickListener\u003c/code\u003e to trigger the Google Pay flow and processing the \u003ccode\u003epaymentDataResponse\u003c/code\u003e object for transaction completion.\u003c/p\u003e\n"]]],["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"],null,["# Integrate Google Pay Merchant SDK with your app\n\nThis guide explains how to integrate the Google Pay Merchant SDK (or Google Pay SDK) with your app.\n\nStep 1: Create `PaymentsClient` instance\n----------------------------------------\n\nCreate an instance of `PaymentsClient` in the `onCreate` method of your\n`Activity` class. This allows interaction with the APIs in the Google Pay SDK\ntest kit. \n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n\n paymentsClient = Wallet.getPaymentsClient();\n }\n\nStep 2: Call `IsReadyToPay` API\n-------------------------------\n\nImplement the [`IsReadyToPay`](/pay/india/api/merchant-sdk/reference/api#isreadytopay)\nmethod, as shown in the following example: \n\n private void isReadyToPay() {\n Task\u003cBoolean\u003e task = paymentsClient.isReadyToPay(request);\n task.addOnCompleteListener(\n new OnCompleteListener\u003cBoolean\u003e() {\n public void onComplete(Task\u003cBoolean\u003e task) {\n try {\n boolean result = task.getResult(RuntimeException.class);\n if (result == true) {\n // Show Google as payment option.\n } else {\n // Hide Google as payment option.\n }\n } catch (RuntimeException exception) {\n // Handle exception.\n }\n }\n });\n }\n\nStep 3: Call `loadPaymentData`\n------------------------------\n\nCalling [`loadPaymentData`](/pay/india/api/merchant-sdk/reference/api#loadpaymentdata)\nstarts a Google Pay Activity to facilitate the payment via intent. Once the user\nhas completed requesting payment, a `PaymentData` response is returned from the\nActivity via `onActivityResult`. The caller must implement `onActivityResult` to\nhandle the `PaymentDataResponse`. To construct the `paymentDataRequest` JSON,\nrefer to the [loadPaymentData](/pay/india/api/merchant-sdk/reference/api#loadpaymentdata)\nsection.\n\nStep 4: Create an `onClickListener` object\n------------------------------------------\n\nThe `paymentDataRequest` object is a Parcelable representing a payment data\nrequest, which provides the necessary information to support a payment. Use the\n`autoResolveHelper` class to auto resolve the Task, and then handle the result\nin the `onActivityResult` method of your `Activity` class. \n\n payWithGPay.setOnClickListener(\n new OnClickListener() {\n @Override\n public void onClick(View view) {\n payWithGPay.setEnabled(false);\n // This transfers the control to the Google Pay app and the result of the transaction\n // will be returned in onActivityResult with the given request code.\n paymentsClient\n .loadPaymentData(\n this, paymentDataRequestJson, LOAD_PAYMENT_DATA_REQUEST_CODE);\n }\n });\n\nStep 5: Handle the `paymentDataResponse` object\n-----------------------------------------------\n\nWhen you call `loadPaymentData`, a `PaymentData` object is returned to\n`onActivityResult`. Parse the `paymentDataobject` to obtain payment credentials\nthat can be charged with your payment provider. The format of the JSON is\ndefined in the\n[loadPaymentData](/pay/india/api/merchant-sdk/reference/api#loadpaymentdata) section.\n\nTo ensure forward compatibility and robustness, parse only the specific fields\nyour application requires from the JSON response. Avoid validating the exact\nfield count, as our API may evolve to include additional data over time without\nconstituting a breaking change. \n\n @Override\n public void onActivityResult(int requestCode, int resultCode, Intent data) {\n if (requestCode == LOAD_PAYMENT_DATA_REQUEST_CODE) {\n // Handle the transaction result here.\n switch (resultCode) {\n case Activity.RESULT_OK:\n String paymentData = WalletUtils.getPaymentDataFromIntent(data);\n // Handle the payment data response in order to complete the transaction.\n break;\n case Activity.RESULT_FIRST_USER:\n int statusCode = data.getIntExtra(WalletConstants.EXTRA_ERROR_CODE,\n WalletConstants.INTERNAL_ERROR);\n handleResultStatusCode(statusCode);\n break;\n case Activity.RESULT_CANCELED:\n // Nothing to here normally - the user simply cancelled without selecting a\n // payment method.\n break;\n }\n }\n }\n\n private void handleResultStatusCode(int statusCode) {\n switch (statusCode) {\n case WalletConstants.ERROR_CODE_BUYER_ACCOUNT_ERROR:\n // Prompt the user that there is a problem with their account.\n break;\n case WalletConstants.ERROR_CODE_MERCHANT_ACCOUNT_ERROR:\n // Merchant account error - handle as necessary.\n break;\n case WalletConstants.ERROR_CODE_UNSUPPORTED_API_VERSION:\n case WalletConstants.INTERNAL_ERROR:\n case WalletConstants.DEVELOPER_ERROR:\n default:\n throw new IllegalStateException(\"Internal error.\");\n }\n }"]]