Skip to Content
Docs

Pay with Google Pay

Check that the customer can pay with Google Pay, show Google's sheet over your activity, and pay with the card they pick.

checkout.payWithGooglePay() shows the Google Pay sheet over your activity and pays with the card the customer picks. Pay.com decrypts the Google Pay token, so your app handles no card data. Call PayGooglePay.isReadyToPay() first, and show your Google Pay button only when it returns true.

Both are in the pay-googlepay module, which brings in Google's play-services-wallet library. The drop-in shows Google's button and does all of this when you set its googlePay option. To call the Google Pay API yourself, see Present Google Pay yourself.

payWithGooglePay and isReadyToPay are suspend functions, so call them from a coroutine.

import com.pay.sdk.googlepay.PayGooglePay
import com.pay.sdk.googlepay.PayGooglePayConfiguration
import com.pay.sdk.googlepay.payWithGooglePay

val googlePay = PayGooglePayConfiguration()
if (PayGooglePay.isReadyToPay(activity, checkout, googlePay)) {
    val result = checkout.payWithGooglePay(activity, googlePay)
}

Google Pay needs:

  • A payment session (sale or authorization) that offers googlepay with an amount and a currency. Setup sessions don't offer it. Your Pay.com account needs Google Pay turned on, a country, and a merchant ID. checkout.supportsGooglePay indicates whether the session meets these requirements.
  • For live payments, Google's production access for your app, from the Google Pay & Wallet Console, and your Google merchant ID.

The module's manifest turns on the Google Pay API for your app (com.google.android.gms.wallet.api.enabled), so you don't add it.

Parameters

payWithGooglePay(activity, configuration, termsAccepted) and PayGooglePay.isReadyToPay(context, checkout, configuration) take:

ParameterTypeRequiredDescription
activityComponentActivityYesThe activity that shows the Google Pay sheet and any 3DS challenge after it (payWithGooglePay only).
contextContextYesAny Android context, such as your activity (isReadyToPay only).
checkoutPayCheckoutYesThe checkout to check (isReadyToPay only).
configurationPayGooglePayConfigurationNoGoogle Pay's options. See PayGooglePayConfiguration. Pass the same one to both calls. Default: PayGooglePayConfiguration().
termsAcceptedBooleanNoIndicates that the customer accepted your terms and conditions on your screen before tapping Google Pay (payWithGooglePay only). Pay.com records that on the payment and the customer. The sheet doesn't show your terms. Default: false.

PayGooglePayConfiguration

Its package is com.pay.sdk.googlepay. The drop-in takes one as its googlePay option.

ParameterTypeRequiredDescription
merchantIdString?NoYour Google merchant ID. Google requires it for live payments. Default: null, for the Google merchant ID set up with Pay.com for your account.
merchantNameString?NoThe merchant name the sheet shows. Default: null, for your account's statement name.
environmentPayGooglePayConfiguration.Environment?NoGoogle Pay's TEST or PRODUCTION environment. Default: null, for PRODUCTION when the session is live (checkout.session.isLive) and TEST otherwise.
buttonTypePayGooglePayConfiguration.ButtonTypeNoThe drop-in button label (BUY, BOOK, CHECKOUT, DONATE, ORDER, PAY, PLAIN, or SUBSCRIBE). Default: PLAIN (the Google Pay mark only).
buttonThemePayGooglePayConfiguration.ButtonTheme?NoThe drop-in button theme (DARK or LIGHT). Default: null (dark on a light sheet and light on a dark sheet).

Returns

isReadyToPay returns true when the session can take Google Pay (checkout.supportsGooglePay) and Google Pay reports that the device can pay with a card the session accepts. It returns false on any error, without throwing.

payWithGooglePay returns a PayResult:

  • PayResult.Canceled when the customer closed the Google Pay sheet.
  • PayResult.Failed when Google Pay failed, for example because Google doesn't accept the merchant ID.
  • Otherwise, the result of paying with the card the customer picked, as checkout.pay() returns it. A card Google Pay holds as a device token pays with its cryptogram. A card it holds without one (PAN_ONLY) pays as a typed card does, so it can get a 3DS challenge after the sheet.

payWithGooglePay reads the session's total again first, so the sheet opens with the current one. If it can't, it returns PayResult.Failed with that error, without showing the sheet. If the total changes while the sheet is open, nothing is charged and the result is PayResult.Failed(PayError.AmountChanged). Show the new total, then let the customer tap your Google Pay button again.

If Android recreates your activity while the sheet is open, for example on a rotation, a switch to dark mode, or after reclaiming it in the background, the payment carries on with the new instance, as long as the coroutine that called payWithGooglePay survives, such as a ViewModel's scope. If the activity finishes for good while the sheet is open, nothing is charged and the result is PayResult.Canceled.

PayResult is a hint for your UI. Fulfill an order only after your server retrieves the session with Retrieve a payment session or receives the signed payment_session.completed webhook.

Errors

payWithGooglePay throws a PayError when it didn't pay:

ErrorWhen
PayError.Unsupported(detail)The session can't take Google Pay (checkout.supportsGooglePay is false). The sheet doesn't open.
PayError.PaymentInProgressAnother payment is running on this checkout.

Other failures come back as PayResult.Failed(error):

ErrorWhen
PayError.Unsupported(detail)Google Pay failed, and detail has its status code. Or Google Pay returned a token the SDK can't read.
PayError.UnexpectedResponse(detail)Google Pay succeeded without returning payment data.
PayError.AmountChanged(amount, currency)The total changed while the sheet was open. Nothing was charged.
Any otherAs for checkout.pay(), for example PayError.Declined.

Google's button and client

PayGooglePay also has two members for Google's own PayButton and PaymentsClient.

MemberDescription
PayGooglePay.allowedPaymentMethods(checkout)The allowedPaymentMethods JSON for Google's PayButton, from the session's card networks. Throws PayError.Unsupported when the session can't take Google Pay.
PayGooglePay.paymentsClient(context, checkout, configuration)A Google Pay PaymentsClient in the configuration's environment, for calls of your own to the Google Pay API.

Example

Google's PayButton is a View. In Compose, show it in an AndroidView.

@Composable
fun GooglePayButton(checkout: PayCheckout, googlePay: PayGooglePayConfiguration, onClick: () -> Unit) {
    val context = LocalContext.current
    var ready by remember { mutableStateOf(false) }
    LaunchedEffect(checkout) { ready = PayGooglePay.isReadyToPay(context, checkout, googlePay) }
    if (!ready) return

    val allowedPaymentMethods = remember(checkout) { PayGooglePay.allowedPaymentMethods(checkout) }
    AndroidView(
        factory = { viewContext ->
            PayButton(viewContext).apply {
                initialize(ButtonOptions.newBuilder().setAllowedPaymentMethods(allowedPaymentMethods).build())
                setOnClickListener { onClick() }
            }
        },
        modifier = Modifier.fillMaxWidth(),
    )
}

In a View layout, add com.google.android.gms.wallet.button.PayButton and initialize it the same way, once isReadyToPay returns true. When the customer taps it:

// The sandbox. For live payments, leave environment out and set your merchantId.
private val googlePay = PayGooglePayConfiguration(environment = PayGooglePayConfiguration.Environment.TEST)

private fun payWithGooglePay() {
    lifecycleScope.launch {
        val result = try {
            checkout.payWithGooglePay(this@CheckoutActivity, googlePay)
        } catch (e: PayError) {
            return@launch showMessage(e) // Google Pay isn't available, or a payment is running
        }
        when (result) {
            is PayResult.Succeeded -> showReceipt(result.ids)  // A hint: your server confirms first
            is PayResult.Processing -> showPending(result.ids) // Your server decides
            PayResult.Canceled -> Unit                         // The customer closed the sheet
            is PayResult.Failed -> showMessage(result.error)
        }
    }
}

Next steps

Was this page helpful?