Google Pay
Google Pay allows customers to make payments in your app or website using any credit or debit card saved to their Google Account, including those from Google Play, YouTube, Chrome, or an Android device. Use the Google Pay API to request any credit or debit card stored in your customer’s Google account.
Google Pay is fully compatible with Stripe’s products and features (e.g., subscriptions), allowing you to use it in place of a traditional payment form whenever possible. Use it to accept payments for physical or digital goods, donations, subscriptions, and more.
Accepting Google Pay in your Android app
Use the Stripe Android SDK to start accepting Google Pay in your Android apps.
Set up your integration
To use Google Pay, first enable the Google Pay API by adding the following to the <application>
tag of your AndroidManifest.xml:
<application> ... <meta-data android:name="com.google.android.gms.wallet.api.enabled" android:value="true" /> </application>
This guide assumes you’re using Google Play services 18.1.1 or greater and the latest version of the Stripe Android SDK.
dependencies { implementation 'com.google.android.gms:play-services-wallet:18.1.1' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.stripe:stripe-android:16.2.0' }
For more details, see Google Pay’s Set up Google Pay API for Android.
Instantiate PaymentsClient
Next, create an instance of PaymentsClient in your Activity
. This allows you to access the Payments APIs. Do this before you start the payment flow, such as in Activity#onCreate()
.
public class CheckoutActivity extends AppCompatActivity { private PaymentsClient paymentsClient; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); PaymentConfiguration.init(this, PUBLISHABLE_KEY); paymentsClient = Wallet.getPaymentsClient( this, new Wallet.WalletOptions.Builder() .setEnvironment(WalletConstants.ENVIRONMENT_TEST) .build() ); } }
After creating PaymentsClient, create an instance of IsReadyToPayRequest and fire it to determine if Google Pay is available.
public class CheckoutActivity extends AppCompatActivity { // continued from above private void isReadyToPay() { final IsReadyToPayRequest request = createIsReadyToPayRequest(); paymentsClient.isReadyToPay(request) .addOnCompleteListener( new OnCompleteListener<Boolean>() { public void onComplete(Task<Boolean> task) { try { if (task.isSuccessful()) { // show Google Pay as payment option } else { // hide Google Pay as payment option } } catch (ApiException exception) { } } } ); } /** * See https://developers.google.com/pay/api/android/reference/request-objects#example * for an example of the generated JSON. */ @NonNull private IsReadyToPayRequest createIsReadyToPayRequest() throws JSONException { final JSONArray allowedAuthMethods = new JSONArray(); allowedAuthMethods.put("PAN_ONLY"); allowedAuthMethods.put("CRYPTOGRAM_3DS"); final JSONArray allowedCardNetworks = new JSONArray(); allowedCardNetworks.put("AMEX"); allowedCardNetworks.put("DISCOVER"); allowedCardNetworks.put("MASTERCARD"); allowedCardNetworks.put("VISA"); final JSONObject cardParameters = new JSONObject(); cardParameters.put("allowedAuthMethods", allowedAuthMethods); cardParameters.put("allowedCardNetworks", allowedCardNetworks); final JSONObject cardPaymentMethod = new JSONObject(); cardPaymentMethod.put("type", "CARD"); cardPaymentMethod.put("parameters", cardParameters); final JSONArray allowedPaymentMethods = new JSONArray(); allowedPaymentMethods.put(cardPaymentMethod); final JSONObject isReadyToPayRequestJson = new JSONObject(); isReadyToPayRequestJson.put("apiVersion", 2); isReadyToPayRequestJson.put("apiVersionMinor", 0); isReadyToPayRequestJson.put("allowedPaymentMethods", allowedPaymentMethods); return IsReadyToPayRequest.fromJson(isReadyToPayRequestJson.toString()); } }
Create a payment request
When the user is ready to pay, create a PaymentDataRequest JSON object. The PaymentDataRequest
object is a JSONObject
representing a request for method of payment (e.g., a credit card) and other details. It provides the necessary information to support a payment, including transaction information such as total cost and currency.
To create a PaymentDataRequest
JSONObject with Stripe configured as your gateway, set the PaymentMethod’s TokenizationSpecification using GooglePayConfig#getTokenizationSpecification().
Configure CardParameters.billingAddressRequired, BillingAddressParameters, and PaymentDataRequest.emailRequired according to your needs, as shown below.
When you have your tokenization parameters, create the PaymentDataRequest
object with the information relevant to the purchase.
public class CheckoutActivity extends AppCompatActivity { // continued from above @NonNull private PaymentDataRequest createPaymentDataRequest() { final JSONObject tokenizationSpec = new GooglePayConfig(this).getTokenizationSpecification(); final JSONObject cardPaymentMethod = new JSONObject() .put("type", "CARD") .put( "parameters", new JSONObject() .put("allowedAuthMethods", new JSONArray() .put("PAN_ONLY") .put("CRYPTOGRAM_3DS")) .put("allowedCardNetworks", new JSONArray() .put("AMEX") .put("DISCOVER") .put("MASTERCARD") .put("VISA")) // require billing address .put("billingAddressRequired", true) .put( "billingAddressParameters", new JSONObject() // require full billing address .put("format", "MIN") // require phone number .put("phoneNumberRequired", true) ) ) .put("tokenizationSpecification", tokenizationSpec); // create PaymentDataRequest final JSONObject paymentDataRequest = new JSONObject() .put("apiVersion", 2) .put("apiVersionMinor", 0) .put("allowedPaymentMethods", new JSONArray().put(cardPaymentMethod)) .put("transactionInfo", new JSONObject() .put("totalPrice", "10.00") .put("totalPriceStatus", "FINAL") .put("currencyCode", "USD") ) .put("merchantInfo", new JSONObject() .put("merchantName", "Example Merchant")) // require email address .put("emailRequired", true) .toString(); return PaymentDataRequest.fromJson(paymentDataRequest); } }
Submit the payment
Now that you’ve established a way to create the payment data request, hook that method up to your Buy button’s click handler.
public class CheckoutActivity extends AppCompatActivity { private static final int LOAD_PAYMENT_DATA_REQUEST_CODE = 53; private void payWithGoogle() { AutoResolveHelper.resolveTask( paymentsClient.loadPaymentData(createPaymentDataRequest()), this, LOAD_PAYMENT_DATA_REQUEST_CODE ); } }
Invoking AutoResolveHelper#resolveTask() with a PaymentDataRequest will launch Google Pay. Receive the result in Activity#onActivityResult()
. Use PaymentMethodCreateParams#createFromGooglePay(JSONObject) to create a PaymentMethodCreateParams
object from the JSON representation of the PaymentData
, as shown below.
public class CheckoutActivity extends AppCompatActivity { @Override public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case LOAD_PAYMENT_DATA_REQUEST_CODE: { switch (resultCode) { case Activity.RESULT_OK: { if (data != null) { onGooglePayResult(data); } break; } case Activity.RESULT_CANCELED: { // Canceled break; } case AutoResolveHelper.RESULT_ERROR: { // Log the status for debugging // Generally there is no need to show an error to // the user as the Google Payment API will do that final Status status = AutoResolveHelper.getStatusFromIntent(data); break; } default: { // Do nothing. } } break; } default: { // Handle any other startActivityForResult calls you may have made. } } } private void onGooglePayResult(@NonNull Intent data) { final PaymentData paymentData = PaymentData.getFromIntent(data); if (paymentData == null) { return; } final PaymentMethodCreateParams paymentMethodCreateParams = PaymentMethodCreateParams.createFromGooglePay( new JSONObject(paymentData.toJson())); stripe.createPaymentMethod( paymentMethodCreateParams, new ApiResultCallback<PaymentMethod>() { @Override public void onSuccess(@NonNull PaymentMethod result) { } @Override public void onError(@NonNull Exception e) { } } ); } }
Going live with Google Pay
- Build your app in
ENVIRONMENT_TEST
with your Stripe test publishable key. - Reach out to Google support.
- Send the APK pointing to
ENVIRONMENT_TEST
to Google support when requested. - Google assesses the app against their integration checklist and provides feedback if needed.
- Google provides instructions on how to agree to their Terms of Service and ensure production access is granted.
- Send your final production APK pointing to
ENVIRONMENT_PRODUCTION
to Google for a real-world test which includes a few transactions. - If all tests pass, Google clears you to publish the APK.
- Notify Google support when your APK is 100% visible to users.
Accepting Google Pay on the web
You can start accepting Google Pay payments on the web using Checkout or Elements. Using Google Pay in Checkout requires no additional configuration. For Elements, refer to our Payment Request Button documentation to learn how to add Google Pay to your site.
If you prefer to integrate directly against the Google Pay API, follow Google’s instructions.