Are you interested in attending a Google Pay launchathon in your city? Let us know!

Tutorial

The following steps show how to integrate your application with the Google Pay API.

Step 1: Create PaymentsClient instance

Create an instance of PaymentsClient in the onCreate method in your Activity. This allows for interaction with the Payments APIs.

private PaymentsClient mPaymentsClient;

@Override
public void onCreate() {
  super.onCreate();

  mPaymentsClient =
      Wallet.getPaymentsClient(
          this,
          new Wallet.WalletOptions.Builder()
              .setEnvironment(WalletConstants.ENVIRONMENT_TEST)
              .build());
}

Step 2: Call isReadyToPay API

Implement the isReadyToPay method, as the following example shows:

  private void isReadyToPay() {
    IsReadyToPayRequest request =
        IsReadyToPayRequest.newBuilder()
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_CARD)
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_TOKENIZED_CARD)
            .build();
    Task<Boolean> task = mPaymentsClient.isReadyToPay(request);
    task.addOnCompleteListener(
        new OnCompleteListener<Boolean>() {
          public void onComplete(Task<Boolean> task) {
            try {
              boolean result = task.getResult(ApiException.class);
              if (result == true) {
                // Show Google as payment option.
              } else {
                // Hide Google as payment option.
              }
            } catch (ApiException exception) {
            }
          }
        });
  }

Step 3: Create PaymentDataRequest object

Create the createPaymentDataRequest method. Initiate the PaymentMethodTokenizationParameters object with the proper parameters according to the payment processor you are using. Also, make sure to specify the allowed payment methods using addAllowedPaymentMethods.

private PaymentDataRequest createPaymentDataRequest() {
    PaymentDataRequest.Builder request =
        PaymentDataRequest.newBuilder()
            .setTransactionInfo(
                TransactionInfo.newBuilder()
                    .setTotalPriceStatus(WalletConstants.TOTAL_PRICE_STATUS_FINAL)
                    .setTotalPrice("10.00")
                    .setCurrencyCode("USD")
                    .build())
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_CARD)
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_TOKENIZED_CARD)
            .setCardRequirements(
                CardRequirements.newBuilder()
                    .addAllowedCardNetworks(
                        Arrays.asList(
                            WalletConstants.CARD_NETWORK_AMEX,
                            WalletConstants.CARD_NETWORK_DISCOVER,
                            WalletConstants.CARD_NETWORK_VISA,
                            WalletConstants.CARD_NETWORK_MASTERCARD))
                    .build());

    PaymentMethodTokenizationParameters params =
        PaymentMethodTokenizationParameters.newBuilder()
            .setPaymentMethodTokenizationType(
                WalletConstants.PAYMENT_METHOD_TOKENIZATION_TYPE_PAYMENT_GATEWAY)
            .addParameter("gateway", "example")
            .addParameter("gatewayMerchantId", "exampleGatewayMerchantId")
            .build();

    request.setPaymentMethodTokenizationParameters(params);
    return request.build();
}

Replace example with a gateway identifier of a Google-supported gateway and exampleGatewayMerchantId with a unique merchant identifier provided by your gateway.

For a list of processors currently supported by Google, see Participating Google Pay processors.

The tokenization type can be one of the following:

  • PAYMENT_METHOD_TOKENIZATION_TYPE_PAYMENT_GATEWAY: Use this tokenization type if you use a payment provider that supports the Google Pay API. Include any provider-specific parameters in the PaymentMethodTokenizationParameters object. You will receive a payment token that can be charged directly with your processor.
  • PAYMENT_METHOD_TOKENIZATION_TYPE_DIRECT: Use this tokenization type if you aren’t using a supported payment provider to directly request and handle payment credentials. Include a publicKey parameter within the PaymentMethodTokenizationParameters object, and follow the instructions in Payment data cryptography to decrypt credentials in your secure Payment Card Industry Data Security Standard (PCI DSS) compliant server environment.

Step 4: Create OnClickListener object

Define an OnClickListener for the Checkout action that will make a PaymentDataRequest object, as the following example shows.

findViewById(R.id.buy_button)
    .setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            PaymentDataRequest request = createPaymentDataRequest();
            if (request != null) {
              AutoResolveHelper.resolveTask(
                  mPaymentsClient.loadPaymentData(request),
                  this,
                  // LOAD_PAYMENT_DATA_REQUEST_CODE is a constant value
                  // you define.
                  LOAD_PAYMENT_DATA_REQUEST_CODE);
            }
          }
        });

The PaymentDataRequest object is a Parcelable representing a payment data request, which provides 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.

Step 5: Handle the PaymentData response object

When you call loadPaymentData a PaymentData object is returned to onActivityResult. Parse the PaymentData object to obtain payment credentials that can be charged with your payment provider.

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
      case LOAD_PAYMENT_DATA_REQUEST_CODE:
        switch (resultCode) {
          case Activity.RESULT_OK:
            PaymentData paymentData = PaymentData.getFromIntent(data);
            String token = paymentData.getPaymentMethodToken().getToken();
            break;
          case Activity.RESULT_CANCELED:
            break;
          case AutoResolveHelper.RESULT_ERROR:
            Status status = AutoResolveHelper.getStatusFromIntent(data);
            // Log the status for debugging.
            // Generally, there is no need to show an error to
            // the user as the Google Pay API will do that.
            break;
          default:
            // Do nothing.
        }
        break;
      default:
        // Do nothing.
    }
  }

Step 6: Refer to your payment processor

For a list of processors currently supported by Google, see Participating Google Pay processors.

Send feedback about...

Google Pay API