Integrate Google Pay Merchant SDK with your app

This guide explains how to integrate the Google Pay Merchant SDK (or Google Pay SDK) with your app.

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();
}

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.
         }
       }
     });
}

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.

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);
     }
   });

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.

@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.");
 }
}