In-App Payments SDK

Enable Google Pay

This document provides step-by-step instructions for integrating Google Pay with the In-App Payments SDK on an Android device.

Android
Java (Android)
Android
Save

Limitations

Prerequisites

To enable Google Pay with In-App Payments SDK on Android, the following must be true:

Step 1: Add Google Pay build dependencies

  1. Add the Google Pay binary dependency to the build.gradle file of your :app module.
dependencies {
...
 implementation 'com.squareup.sdk.in-app-payments:google-pay:1.1.0'
 implementation 'com.google.android.gms:play-services-wallet:16.0.0'
...
}
  1. Update your AndroidManifest.xml file to include the following <meta-data/> element.
<application
  ...
  android:theme="@style/AppTheme">
...
  <meta-data
    android:name="com.google.android.gms.wallet.api.enabled"
    android:value="true" />
...
</application>

Step 2: Add a Google Pay item to your activity layout

Add a clickable item to your activity layout for starting a Google Pay payment.
In Step 5, you will add an onClickListener to this control.

Step 3: Declare fields in your activity that support Google Pay

  private static final String LOCATION_ID = "LOCATION_ID";
  private static final int LOAD_PAYMENT_DATA_REQUEST_CODE = 1;
  private PaymentsClient paymentsClient;
  private Button googlePayButton;

Step 4: Enable Google Pay in your activity onCreate method

  1. Create a Google Pay payments client that is configured for a testing environment.

See the Google Pay tutorial for more information about creating a client.

paymentsClient = Wallet.getPaymentsClient(
  this,
  new Wallet.WalletOptions.Builder()
      .setEnvironment(WalletConstants.ENVIRONMENT_TEST)
.build());
  1. Set Google Pay button onClickListener.

Replace YOUR_ACTIVITY_CLASS_NAME with the type name of the your activity and YOUR_LOCATION_ID string in the example code with a valid Square location ID. The available location IDs for a Square account can be found on the Locations tab of the Square Developer Portal.

googlePayButton.setOnClickListener((view) -> {
  googlePayButton.setEnabled(false);
  AutoResolveHelper.resolveTask(
      paymentsClient.loadPaymentData(
          GooglePay.createPaymentDataRequest(LOCATION_ID,TransactionInfo.newBuilder()
            .setTotalPriceStatus(WalletConstants.TOTAL_PRICE_STATUS_NOT_CURRENTLY_KNOWN)
            .setCurrencyCode("USD")
            .build())),
      (YOUR_ACTIVITY_CLASS_NAME) view.getContext(),
      LOAD_PAYMENT_DATA_REQUEST_CODE);
});
  1. Request "ready to pay" status and enable Google Pay button if ready to pay. See the Google Pay tutorial for more information about getting pay status.
paymentsClient.isReadyToPay(GooglePay.createIsReadyToPayRequest())
        .addOnCompleteListener(this, (task) ->
           googlePayButton.setEnabled(task.isSuccessful()));

Step 5: Get Google Pay token in your activity onActivityresult method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == LOAD_PAYMENT_DATA_REQUEST_CODE) {
    googlePayButton.setEnabled(true);
    if (resultCode == Activity.RESULT_OK) {
      PaymentData paymentData = PaymentData.getFromIntent(data);
      if (paymentData != null && paymentData.getPaymentMethodToken() != null) {
        String googlePayToken = paymentData.getPaymentMethodToken().getToken();

        //Request a nonce, save Card on File, and take a payment
        GooglePayToSquare googlePayToSquare = new GooglePayToSquare();
        googlePayToSquare.activity = this;
        googlePayToSquare.createNonce(googlePayToken);
      }
    }
  }
}

Step 6: Add a class to collect payment card information and return a nonce.

public final class GooglePayToSquare {

  @Nullable Activity activity;
  String nonce;

  public void createNonce(String googlePayToken) {
    GooglePay.requestGooglePayNonce(googlePayToken).enqueue(result -> {
      if (activity == null) {
        return;
      }
      if (result.isSuccess()) {
        nonce = result.getSuccessValue().getNonce();
        Toast.makeText(activity,
            nonce,
            Toast.LENGTH_SHORT)
            .show();
     // MyBackendServiceResponse response = ...// TODO Call your backend service
     // with the nonce. See Square In-App Payments Server Quick Start for a backend
     // service example.

      } else if (result.isError()) {

        Toast.makeText(activity,
            result.getErrorValue().getMessage(),
            Toast.LENGTH_SHORT)
            .show();
      }
    });
  }

  private void takePayment(String nonce, Long paymentAmount) {
   //Make a server API call to take the payment
  }
}

Step 7: Prepare to distribute your app in production

When your Google Pay integration has passed your testing procedures and you are ready to distribute your app in a production environment, complete the following tasks:

  1. Set the client environment to production by using WalletConstants.ENVIRONMENT_PRODUCTION.
paymentsClient = Wallet.getPaymentsClient(
  this,
  new Wallet.WalletOptions.Builder()
      .setEnvironment(WalletConstants.ENVIRONMENT_PRODUCTION)
.build());
  1. Complete the Google Pay Integration checklist. If you have integrated Google Pay as described in the setup guide steps, the exit criteria for basics and branding testing checklist items will be met. The exit criteria for Functional testing checklist items 4 – 9 can only be met by your application logic.

  2. Complete a Google Pay API Production Access Enablement Request Form.

    • The Tokenization Method must be "gateway".
    • The Payment Processor or Gateway form field must contain "square".

Contact Developer Support, join our Slack channel, or ask for help on Stack Overflow