Skip to Content
Docs

Approve or stop a payment

Run a final check before payment submission for any payment method, and either proceed or cancel.

beforePay is a pay.checkout() option that runs once per payment attempt, immediately before the SDK submits the payment. It applies to every payment method, including card forms (Universal checkout, checkout.fields(), and client-side encryption with checkout.cse()), saved payment methods, Click to Pay, wallets, alternative payment methods, checkout.headless(), and express checkout. On PayPal's own button, from checkout.paypal(), it runs before PayPal creates the order. Use it to create or reserve an order, or to stop the payment. See Express checkout for express checkout usage.

const checkout = pay.checkout({ clientSecret, beforePay })

Parameters

beforePay receives the same fields as onValidateSession, plus two express checkout fields:

ParameterTypeDescription
paymentMethodTypestringPayment method identifier (for example, card, click_to_pay, applepay, or paypal).
fundingTypestring | nullCard funding type when available. In Universal checkout, included only when you listen for card_funding_type_update.
walletResponseobjectRaw payment response from Apple Pay or Google Pay.
expresstruePresent when the payment originates from checkout.express().
payerExpressPayerCustomer details from the wallet sheet (name, email, phone, billingAddress, shippingAddress, and shippingOption), present for express checkout only.

Returns

Return or resolve with:

  • true or { proceed: true } to let the payment go ahead.
  • false or { proceed: false, message } to stop the payment before the SDK submits it. Express checkout sheets show message to the customer.

Any other value, including undefined when a callback doesn't return a value, stops the payment. A callback that throws stops the payment too, and if it throws an Error, the error's message is passed on.

While a wallet sheet waits on it, beforePay has 15 seconds. The limit applies to every Apple Pay sheet, in Universal checkout, Headless checkout, and express checkout, and to Google Pay in express checkout. Google Pay elsewhere has no limit, because its sheet has closed by the time beforePay runs.

If beforePay takes longer than 15 seconds, Pay.com.js stops the payment as if beforePay returned false. Nothing is charged, onFailure isn't called, and the sheet asks the customer to try again. A call such as checkout.headless('applepay') rejects with an error whose data.stoppedBy is 'beforePay'. Make beforePay idempotent, so it's safe to run twice for the same order. See How a payment runs.

Errors

Stopping a payment in beforePay doesn't decline it. Pay.com.js doesn't emit payment_failure or call onFailure. checkout.headless() and checkout.pay({ source }) reject with an error whose data.stoppedBy is 'beforePay'. checkout.submit() and checkout.pay({ method: 'card' }) resolve with undefined instead, with secure card fields and with the Universal checkout form, whatever throwOnSubmitFailure is.

Approval applies only to the session beforePay was asked about. If pay.checkout() moves to another session while beforePay runs, or after it and before the request that pays, nothing is paid. checkout.headless() and checkout.pay() then reject with the message "The transaction was replaced by a new checkout session". A refreshed client secret for the same session still pays.

If the customer closes the Apple Pay sheet in Universal checkout while beforePay runs, nothing is paid, even if beforePay then lets the payment go ahead. A sheet closed after the payment started reports the payment's real outcome.

With beforePay set, checkout.pay() and checkout.headless() for cards, alternative payment methods, and Click to Pay emit payment_processing only after beforePay approves the payment. checkout.headless('applepay') and checkout.headless('googlepay') are the exceptions. They emit it before the wallet sheet opens.

With onValidateSession

In Universal checkout, an element's own onValidateSession runs first, and beforePay runs after it. Both must approve for the payment to go ahead. onValidateSession approves by returning true, and beforePay by returning true or { proceed: true }.

Example

const checkout = pay.checkout({
  clientSecret,
  beforePay: async (data) => {
    const ok = await reserveStock(data.paymentMethodType)
    return ok ? true : { proceed: false, message: 'Some items are no longer available.' }
  },
})
Was this page helpful?