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.
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.
To enable Google Pay with the In-App Payments SDK on Android, the following must be true:
You're a US, Canadian, UK, or AU Square seller. Google Pay for the In-App Payments SDK is only available for Square sellers based in the United States, Canada, the United Kingdom, or Australia.
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.
:app
module.<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 Dashboard.
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.
Paste your Sandbox location ID to replace the placeholder "LOCATION_ID"
as the value of LOCATION_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 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.
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())
);
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);
}
}
}
}
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.
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
}
}
To test your Google Pay integration, you must register a valid credit card in Google Pay.
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:
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.
Complete a Google Pay API Production Access Enablement Request Form:
Replace your step 3 Sandbox application ID and location ID with production values.
Set the client environment to production by using WalletConstants.ENVIRONMENT_PRODUCTION
.
paymentsClient = Wallet.getPaymentsClient(
this,
new Wallet.WalletOptions.Builder()
.setEnvironment(WalletConstants.ENVIRONMENT_PRODUCTION)
.build());
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.
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.
If you need more assistance, contact Developer and App Marketplace Support or ask for help in the Developer Forums.