Skip to Content
Docs

Validate a session

Run an async check before a payment attempt starts and return false to block it.

onValidateSession is an async callback that runs before a payment attempt starts. Return true to continue or false to block the payment.

  • Universal checkout (checkout.universal()): Runs for every payment method when the customer submits the form or selects a wallet or alternative payment method.
  • Headless checkout (checkout.headless()): Runs only for applepay and googlepay. For cards and APMs, run validation before calling checkout.headless().

Callback signature

The callback receives a ValidateSessionData object and returns a promise that resolves to a boolean.

type ValidateSessionData = {
  paymentMethodType: string
  fundingType?: 'prepaid' | 'debit' | 'credit' | null
  walletResponse?: ApplePayPayment | GooglePayPaymentData
}

type OnValidateSession = (data: ValidateSessionData) => Promise<boolean>

Parameters

The ValidateSessionData object has these fields:

ParameterTypeDescription
paymentMethodTypestringPayment method being validated (card, click_to_pay, googlepay, applepay, paypal, or an APM identifier such as paysafecard or skrill).
fundingType'prepaid' | 'debit' | 'credit' | nullCard funding type when available. In Universal checkout, included only if you registered a checkout.EVENT_TYPES.CARD_FUNDING_TYPE_UPDATE listener with checkout.on(). In Headless checkout, included for Apple Pay and Google Pay.
walletResponseApplePayPayment | GooglePayPaymentDataRaw native wallet response for Apple Pay and Google Pay only.

Returns

Promise<boolean>. Return true to allow the payment or false to block it. In Universal checkout, throwing an error also blocks the payment. In Headless checkout, returning false calls onFailure and rejects checkout.headless().

Example

Pass onValidateSession in checkout.universal() options or as the 4th argument of checkout.headless(). Pay.com() doesn't accept it.

checkout.universal({
  container: '#pay-com-checkout',
  onValidateSession: async ({ paymentMethodType, fundingType, walletResponse }) => {
    if (paymentMethodType === 'card') {
      return validateCardFundingType(fundingType)
    }
    if (paymentMethodType === 'applepay' || paymentMethodType === 'googlepay') {
      return validateWalletPayment(walletResponse)
    }
    return isPaymentMethodAllowed(paymentMethodType)
  }
})

onValidateSession replaces the deprecated apmsOnClickValidation option. If both are passed, only onValidateSession runs.

Keep onValidateSession fast, because it blocks the customer's checkout flow. Perform authoritative order, eligibility, and risk checks on your server.

Next steps

Was this page helpful?