Skip to Content
Docs

Present Google Pay yourself

Get the session's Google Pay requests as JSON for your own Google Pay integration, then pay with the payment data Google returns.

When you call the Google Pay API yourself instead of using checkout.payWithGooglePay(), the checkout builds the Google Pay requests for the session, as Pay.com.js does, and checkout.pay() pays with the payment data Google returns. You don't need the pay-googlepay module for this, only Google's own Google Pay library.

checkout.supportsGooglePay returns true when the session supports Google Pay. checkout.googlePayIsReadyToPayRequest() and checkout.googlePayPaymentDataRequest() return the requests as JSON strings, for IsReadyToPayRequest.fromJson() and PaymentDataRequest.fromJson().

if (checkout.supportsGooglePay) {
    val readyRequest = IsReadyToPayRequest.fromJson(checkout.googlePayIsReadyToPayRequest())
    val paymentRequest = PaymentDataRequest.fromJson(checkout.googlePayPaymentDataRequest())
}

// With the PaymentData Google returns:
val result = checkout.pay(PayPaymentRequest.GooglePay(paymentData.toJson()), activity)

Parameters

googlePayPaymentDataRequest(merchantId, merchantName) takes:

ParameterTypeRequiredDescription
merchantIdString?NoYour Google merchant ID, as merchantInfo.merchantId. Google requires one 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, as merchantInfo.merchantName. Default: null, for your account's statement name.

googlePayIsReadyToPayRequest() takes no parameters. PayPaymentRequest.GooglePay takes the following parameter:

ParameterTypeRequiredDescription
paymentDataStringYesGoogle's PaymentData serialized as JSON (paymentData.toJson()).

Returns

supportsGooglePay is true when the session offers googlepay, is a payment session (SALE or AUTHORIZATION) with an amount and a currency, and your account has a country and a merchant ID. Google Pay's isReadyToPay indicates whether the device can pay.

Both requests are for version 2.0 of the Google Pay API, with one CARD payment method:

FieldValue
allowedAuthMethodsPAN_ONLY and CRYPTOGRAM_3DS, or only CRYPTOGRAM_3DS when your account's Google Pay settings require a cryptogram.
allowedCardNetworksThe session's card brands that Google Pay has a network for (UnionPay has none). AMEX, DISCOVER, JCB, MASTERCARD, and VISA when none do, or the session doesn't limit brands.
allowPrepaidCards, allowCreditCardsSet only when the session limits the card funding types.
billingAddressRequiredtrue, with the MIN format, so the token carries the cardholder's name. Left out when your account's Google Pay settings turn name collection off.
tokenizationSpecificationPAYMENT_GATEWAY, with gateway: "paycom" and your Pay.com merchant ID as gatewayMerchantId. In the PaymentDataRequest only.

The PaymentDataRequest also has merchantInfo, from the parameters, and transactionInfo containing the session's amount as a FINAL totalPrice, its currency, your account's country, and COMPLETE_IMMEDIATE_PURCHASE.

The payment is charged only if the session's total is still the one in the last PaymentDataRequest you built. If the amount may have changed, call refreshSession() first, then build the request.

Paying with PayPaymentRequest.GooglePay returns a PayResult as checkout.pay() does. A device token (CRYPTOGRAM_3DS) pays with its cryptogram. A PAN_ONLY card pays as a typed card does, so it can get a 3DS challenge.

Errors

The request methods throw one error.

ErrorWhen
PayError.Unsupported(detail)googlePayIsReadyToPayRequest() and googlePayPaymentDataRequest() throw it when supportsGooglePay is false. Inspect detail for the reason.

Paying returns the errors of checkout.pay(), with PayError.Unsupported in PayResult.Failed when the payment data has no token, or an incomplete one.

Example

class CheckoutActivity : ComponentActivity() {
    private lateinit var checkout: PayCheckout

    // TEST in the sandbox; ENVIRONMENT_PRODUCTION for live payments.
    private val paymentsClient by lazy {
        Wallet.getPaymentsClient(this, Wallet.WalletOptions.Builder().setEnvironment(WalletConstants.ENVIRONMENT_TEST).build())
    }

    private val googlePaySheet = registerForActivityResult(TaskResultContracts.GetPaymentDataResult()) { taskResult ->
        when (taskResult.status.statusCode) {
            CommonStatusCodes.SUCCESS -> taskResult.result?.let { paymentData ->
                lifecycleScope.launch {
                    val result = checkout.pay(PayPaymentRequest.GooglePay(paymentData.toJson()), this@CheckoutActivity)
                    showResult(result) // A hint: your server confirms the payment first
                }
            }
            CommonStatusCodes.CANCELED -> Unit // The customer closed the sheet
            else -> showMessage("Google Pay isn't available right now.")
        }
    }

    private fun startGooglePay() {
        lifecycleScope.launch {
            try {
                checkout.refreshSession() // So the sheet shows the current total
            } catch (e: PayError) {
                return@launch showMessage("Google Pay isn't available right now.")
            }
            val request = PaymentDataRequest.fromJson(checkout.googlePayPaymentDataRequest())
            googlePaySheet.launch(paymentsClient.loadPaymentData(request))
        }
    }
}

Next steps

Was this page helpful?