Google Pay

    Learn how to accept payments using 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 ones 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.

    Step 1: 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 are using Google Play services 16.0.0 or greater as well as Stripe Android SDK v10.2.0 or greater.

    dependencies {
      implementation 'com.google.android.gms:play-services-wallet:16.0.0'
      implementation "com.android.support:appcompat-v7:28.0.0"
      implementation 'com.stripe:stripe-android:10.3.0'
    }
    

    For more details, see Google Pay’s Set up Google Pay API for Android.

    Step 2: 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().

    final PaymentsClient paymentsClient = Wallet.getPaymentsClient(this,
        new Wallet.WalletOptions.Builder()
            .setEnvironment(WalletConstants.ENVIRONMENT_TEST)
            .build());
    
    val paymentsClient = Wallet.getPaymentsClient(
        this,
        Wallet.WalletOptions.Builder()
            .setEnvironment(WalletConstants.ENVIRONMENT_TEST)
            .build()
    )
    

    After creating PaymentsClient, create an instance of IsReadyToPayRequest to determine if Google Pay is available.

    private void isReadyToPay() {
        final IsReadyToPayRequest request = IsReadyToPayRequest.newBuilder()
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_CARD)
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_TOKENIZED_CARD)
            .build();
        paymentsClient.isReadyToPay(request).addOnCompleteListener(
            new OnCompleteListener<Boolean>() {
                public void onComplete(Task<Boolean> task) {
                    try {
                        final boolean result =
                            task.getResult(ApiException.class);
                        if (result == true) {
                            // show Google Pay as payment option
                        } else {
                            // hide Google Pay as payment option
                        }
                    } catch (ApiException exception) { }
                }
            }
        );
    }
    
    private fun isReadyToPay() {
        val request = IsReadyToPayRequest.newBuilder()
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_CARD)
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_TOKENIZED_CARD)
            .build()
        paymentsClient.isReadyToPay(request).addOnCompleteListener { task ->
            try {
                val result = task.getResult(ApiException::class.java)!!
                if (result) {
                    // show Google Pay as payment option
                } else {
                    // hide Google Pay as payment option
                }
            } catch (exception: ApiException) {
            }
        }
    }
    

    Step 3: 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 (such as 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.

    Once you have your tokenization parameters, create the PaymentDataRequest object with the information relevant to the purchase.

    @NonNull
    private JSONObject createPaymentDataRequest() {
        final JSONObject tokenizationSpec =
            new GooglePayConfig().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("JCB")
                            .put("MASTERCARD")
                            .put("VISA"))
    
                    // require billing address
                    .put("billingAddressRequired", true)
                    .put(
                        "billingAddressParameters",
                        new JSONObject()
                            // require full billing address
                            .put("format", "FULL")
    
                            // 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", 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);
    }
    
    private fun createPaymentDataRequest(): JSONObject {
        // create PaymentMethod
        val cardPaymentMethod = JSONObject()
            .put("type", "CARD")
            .put(
                "parameters",
                JSONObject()
                    .put("allowedAuthMethods", JSONArray()
                        .put("PAN_ONLY")
                        .put("CRYPTOGRAM_3DS"))
                    .put("allowedCardNetworks",
                        JSONArray()
                            .put("AMEX")
                            .put("DISCOVER")
                            .put("JCB")
                            .put("MASTERCARD")
                            .put("VISA"))
    
                    // require billing address
                    .put("billingAddressRequired", true)
                    .put(
                        "billingAddressParameters",
                        JSONObject()
                            // require full billing address
                            .put("format", "FULL")
    
                            // require phone number
                            .put("phoneNumberRequired", true)
                    )
            )
            .put("tokenizationSpecification",
                GooglePayConfig().tokenizationSpecification)
    
        // create PaymentDataRequest
        val paymentDataRequest = JSONObject()
            .put("apiVersion", 2)
            .put("apiVersionMinor", 0)
            .put("allowedPaymentMethods",
                JSONArray().put(cardPaymentMethod))
            .put("transactionInfo", JSONObject()
                .put("totalPrice", "10.00")
                .put("totalPriceStatus", "FINAL")
                .put("currencyCode", "USD")
            )
            .put("merchantInfo", JSONObject()
                .put("merchantName", "Example Merchant"))
    
            // require email address
            .put("emailRequired", true)
            .toString()
    
        return PaymentDataRequest.fromJson(paymentDataRequest)
    }
    

    Step 4: Submit the payment

    Now that you’ve established a way to create the payment data request, hook up that method to your “Buy” button’s click handler.

    public class PayWithGoogleActivity 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
            );
        }
    }
    
    class PayWithGoogleActivity : AppCompatActivity() {
        private fun payWithGoogle() {
            AutoResolveHelper.resolveTask(
                paymentsClient.loadPaymentData(createPaymentDataRequest()),
                this@PayWithGoogleActivity,
                LOAD_PAYMENT_DATA_REQUEST_CODE
            )
        }
    
        companion object {
            private const val LOAD_PAYMENT_DATA_REQUEST_CODE = 53
        }
    }
    

    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.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case LOAD_PAYMENT_DATA_REQUEST_CODE: {
                switch (resultCode) {
                    case Activity.RESULT_OK: {
                        PaymentData paymentData = PaymentData.getFromIntent(data);
                        // You can get some data on the user's card, such as the
                        // brand and last 4 digits
                        CardInfo info = paymentData.getCardInfo();
                        // You can also pull the user address from the
                        // PaymentData object.
                        UserAddress address = paymentData.getShippingAddress();
                        // This is the raw JSON string version of your Stripe token.
                        String rawToken = paymentData.getPaymentMethodToken()
                            .getToken();
    
                        // Now that you have a Stripe token object,
                        // charge that by using the id
                        Token stripeToken = Token.fromString(rawToken);
                        if (stripeToken != null) {
                            // This chargeToken function is a call to your own
                            // server, which should then connect to Stripe's
                            // API to finish the charge.
                            chargeToken(stripeToken.getId());
                        }
                        break;
                    }
                    case Activity.RESULT_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.
            }
        }
    }
    
    public override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        when (requestCode) {
            LOAD_PAYMENT_DATA_REQUEST_CODE -> {
                when (resultCode) {
                    Activity.RESULT_OK -> {
                        val paymentData = PaymentData.getFromIntent(data!!)
                        // You can get some data on the user's card, such as the
                        // brand and last 4 digits
                        val info = paymentData!!.cardInfo
                        // You can also pull the user address from the
                        // PaymentData object.
                        val address = paymentData.shippingAddress
                        // This is the raw JSON string version of your Stripe token.
                        val rawToken = paymentData.paymentMethodToken!!
                            .token
    
                        // Now that you have a Stripe token object,
                        // charge that by using the id
                        val stripeToken = Token.fromString(rawToken)
                        if (stripeToken != null) {
                            // This chargeToken function is a call to your own
                            // server, which should then connect to Stripe's
                            // API to finish the charge.
                            chargeToken(stripeToken.id)
                        }
                    }
                    Activity.RESULT_CANCELED -> {
                    }
                    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
                        val status = AutoResolveHelper.getStatusFromIntent(data)
                    }
                    else -> {
                        // Do nothing.
                    }
                }
            }
            else -> {
                // Handle any other startActivityForResult calls you may have made.
            }
        }
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode,
                                 @Nullable Intent 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) {
                }
            }
        );
    }
    
    public override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        when (requestCode) {
            LOAD_PAYMENT_DATA_REQUEST_CODE -> {
                when (resultCode) {
                    Activity.RESULT_OK -> {
                        if (data != null) {
                            onGooglePayResult(data)
                        }
                    }
                    Activity.RESULT_CANCELED -> {
                        // Cancelled
                    }
                    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
                        val status = AutoResolveHelper.getStatusFromIntent(data)
                    }
                    else -> {
                        // Do nothing.
                    }
                }
            }
            else -> {
                // Handle any other startActivityForResult calls you may have made.
            }
        }
    }
    
    private fun onGooglePayResult(data: Intent) {
        val paymentData = PaymentData.getFromIntent(data) ?: return
        val paymentMethodCreateParams =
            PaymentMethodCreateParams.createFromGooglePay(
                JSONObject(paymentData.toJson()))
    
        // now use the `paymentMethodCreateParams` object to create a PaymentMethod
        stripe.createPaymentMethod(
            paymentMethodCreateParams,
            object : ApiResultCallback<PaymentMethod> {
                override fun onSuccess(result: PaymentMethod) {
                }
    
                override fun onError(e: Exception) {
                }
            }
        )
    }
    

    Going live with Google Pay

    1. Build your app in ENVIRONMENT_TEST mode and use the test credentials from Stripe.
    2. Reach out to Google support.
    3. Send the APK pointing to ENVIRONMENT_TEST to Google support when requested.
    4. Google assesses the app against their integration checklist and provides feedback if needed.
    5. Google provides instructions on how to agree to their Terms of Service and ensure production access is granted.
    6. Send your final production APK pointing to ENVIRONMENT_PRODUCTION to Google for a real-world test which includes a few transactions.
    7. If all tests pass, Google clears you to publish the APK.
    8. 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 Stripe.js and 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.

    Was this page helpful? Yes No

    Send

    Thank you for helping improve Stripe's documentation. If you need help or have any questions, please consider contacting support.

    Questions?

    We're always happy to help with code or other questions you might have. Search our documentation, contact support, or connect with our sales team. You can also chat live with other developers in #stripe on freenode.

    On this page