Enable Google Pay

Applies to: In-App Payments SDK - Android

Learn how to enable Google Pay for the Square In-App Payments SDK.

Link to section

Overview

Use the following step-by-step instructions to integrate Google Pay with the In-App Payments SDK on an Android device.

Link to section

Requirements and limitations

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

Link to section

1. Add Google Pay build dependencies

  1. Add the Google Pay binary dependency to the build.gradle file of your :app module.
  2. Update your AndroidManifest.xml file to include the following <meta-data/> element:

app dependency

dependencies { ... implementation 'com.squareup.sdk.in-app-payments:google-pay:1.5.5' implementation 'com.google.android.gms:play-services-wallet:16.0.0' ... }

AndroidManifest.xml

<application ... android:theme="@style/AppTheme"> ... <meta-data android:name="com.google.android.gms.wallet.api.enabled" android:value="true" /> ... </application>
Link to section

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 4, add an onClickListener callback to this control.

<ImageButton android:id="@+id/pay_with_google_button" style="@style/CustomButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="32dp" android:layout_marginStart="8dp" android:background="@drawable/google_pay_button" android:enabled="false" android:src="@drawable/google_pay_logo_button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/button_middle_guide" app:layout_constraintTop_toTopOf="@id/pay_with_card_button" />
Link to section

3. Update your activity class for Google Pay

  1. Declare the following fields as private Activity variables:

    private static final String LOCATION_ID = "LOCATION_ID"; private static final int LOAD_PAYMENT_DATA_REQUEST_CODE = 1; private PaymentsClient paymentsClient; private Button googlePayButton;
  2. Copy a location ID from your application Locations page in the Developer Dashboard.

    Important

    To test an application in the Square Sandbox, set the Developer Dashboard application page to Sandbox mode and configure the SDK with a Sandbox application ID before completing the following instructions in this step.

  3. Paste your Sandbox location ID to replace the placeholder "LOCATION_ID" as the value of LOCATION_ID.

Link to section

4. Enable Google Pay in your activity onCreate method

  1. Create a Google Pay payments client that is configured for a test environment. For more information about creating a client, see Google Pay tutorial.

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

    Replace YOUR_ACTIVITY_CLASS_NAME with the type name of your activity and the 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 page of the Developer Dashboard.

  3. Request the "ready to pay" status and enable the Google Pay button if you're ready to pay. For more information about getting the pay status, see Google Pay tutorial.

    paymentsClient.isReadyToPay( GooglePay.createIsReadyToPayRequest() ) .addOnCompleteListener( this, (task) -> googlePayButton.setEnabled(task.isSuccessful()) );
Link to section

5. Get a Google Pay token in your activity onActivityresult method

Did you know?

Google developer documentation provides a complete reference for the Intent PaymentData object. For more information about handling the response object, see Google Pay tutorial.

Link to section

6. Add a class to collect payment card information and return a payment token

Did you know?

Taking a payment requires your application to make server calls to a backend service that you create. To get a quickstart example about taking a payment, use the Square In-App Payments Server Quickstart to create a backend that takes payments with the payment token generated by the SDK.

Link to section

7. Prepare to distribute your application in production

To test your Google Pay integration, you must register a valid credit card in Google Pay.

Important

Google Pay doesn't allow you to add Square Sandbox test card values. Instead, use the credit card you registered with Google. As long as you're testing in the Sandbox, your credit card isn't charged for any test payments.

When your Google Pay integration has passed your test procedures and you're ready to distribute your application in a production environment, complete the following tasks:

  1. Complete the Google Pay Integration Checklist. If you've integrated Google Pay as described in the setup guide, the exit criteria for basics and branding testing checklist items are 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.
  3. Replace your step 3 Sandbox application ID and location ID with production values.

  4. Set the client environment to production by using WalletConstants.ENVIRONMENT_PRODUCTION.

    paymentsClient = Wallet.getPaymentsClient( this, new Wallet.WalletOptions.Builder() .setEnvironment(WalletConstants.ENVIRONMENT_PRODUCTION) .build());
Link to section

Next step

To add security to a payment method, you can add the 3D Secure security protocol layer to the application. 3D Secure integrates Strong Customer Authentication (SCA) to verify the identity of the payment card holder by using the verifyBuyer function. For more information, see Strong Customer Authentication.

Important

SCA should be called for all customer-initiated transactions, including digital wallet payments. If the seller doesn't have SCA called for digital wallet payments, the transactions might be declined due to lack of authentication.