Applies to: In-App Payments SDK - Android
Learn how to enable Google Pay for the Square In-App Payments SDK.
Use the following step-by-step instructions to integrate Google Pay with the In-App Payments SDK on an Android device. Digital wallet payments with Google Pay are available in all regions in which Square operates.
To enable Google Pay with the In-App Payments SDK on Android, the following must be true:
- You've followed the instructions in Build on Android. This topic doesn't cover the general setup of the In-App Payments SDK.
- You've read, and adhere to, Google Pay API Terms of Service, Google Pay API Acceptable Use Policy, and Google's brand guidelines.
- The Google Pay application must be installed on the Android devices that your application is distributed to.
- The device user must have added a Square-supported payment method to Google Pay.
- Add the Google Pay binary dependency to the build.gradle file of your
:app
module. - 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>
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" />
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;Copy a location ID from your application Locations page in the Developer Console.
Important
To test an application in the Square Sandbox, set the Developer Console application page to Sandbox mode and configure the SDK with a Sandbox application ID before completing the following instructions in this step.
Paste your Sandbox location ID to replace the placeholder
"LOCATION_ID"
as the value ofLOCATION_ID
.
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() );Set the Google Pay button
onClickListener
.Replace
YOUR_ACTIVITY_CLASS_NAME
with the type name of your activity and theYOUR_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 Console.googlePayButton = yourActivityView.findViewById(R.id.pay_with_google_button); 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 ); } );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()) );
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.
@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); } } } }