Skip to Content
Docs

Pay with a saved payment method

Submit a Headless checkout payment or select a payout destination using a customer's previously saved payment method.

The checkout.pay() method submits a payment using a saved payment method. Use it in a Headless checkout integration with checkout.getExistingSources() to let returning customers pay with a payment method they've saved from a previous purchase.

For payouts, checkout.pay() doesn't send money. It registers the payout source the customer selected from checkout.getExistingSources() through the setup session. Your server confirms the choice on the session, then uses it as the destination when calling the Create a payout API. See Payout flow.

Headless checkout only. Universal checkout handles saved payment methods through its built-in UI. You need this method only when you build your own checkout UI with Headless checkout.

const result = await checkout.pay({ source, cvv })

Parameters

ParameterTypeRequiredDescription
sourcestringYesThe id from a saved source returned by checkout.getExistingSources().
cvvstringNoCard security code. Required when the source's cvvExists is false. Can be omitted when cvvExists is true.

Returns

checkout.pay() returns a promise that resolves with the payment result. Unlike checkout.headless(), it does not call the onSuccess, onPending, or onFailure callbacks registered on pay.checkout(). Use the resolved value, or listen for checkout.EVENT_TYPES.PAYMENT_SUCCESS and checkout.EVENT_TYPES.PAYMENT_PENDING with checkout.on().

If the payment fails, the promise rejects with a { message, data } object, so wrap the call in try/catch.

FieldTypeDescription
transactionstringThe charge ID (for example, "chrg_742353659387446272").
customerstringThe Pay.com customer ID.
statusstringPayment status, for example "succeeded", or "pending" when the payment isn't final yet.
paymentMethodstringThe stored payment method ID used for this charge.
amountnumberThe charged amount in minor units.

checkout.pay() returns the result in the browser. Always confirm payments server-side using webhooks. Don't rely on client-side results for order fulfillment.

Example

Pay with a card the customer saved earlier.

Payment example

// 1. Retrieve saved payment methods
const existingSources = await checkout.getExistingSources()

// 2. Customer selects a saved card (your UI logic)
const selected = existingSources[0]

// 3. Pay with the saved card. Pass the CVV when selected.card.cvvExists is false.
try {
  const result = await checkout.pay({
    source: selected.id,
    cvv: '123',
  })

  console.log(result.status)      // 'succeeded'
  console.log(result.transaction) // 'chrg_742353659387446272'
} catch (error) {
  console.log('Payment failed:', error.message)
}

// Or listen for the result as an event
checkout.on(checkout.EVENT_TYPES.PAYMENT_SUCCESS, (payment) => {
  console.log('Paid:', payment.transaction)
})

Payout flow

When the checkout is initialized with a payout setup session (payout: true), checkout.getExistingSources() returns only payout-capable sources. In this case, checkout.pay() does not send money. It registers the customer's selection and resolves with the result, whose paymentMethod is the selected payment method ID. It doesn't call onSuccess. Tell your server the recipient is done. Your server confirms the choice on the setup session, then uses that method as the destination when calling the Create a payout API. For the full flow, including creating a payout setup session and handling webhooks, see Let recipients choose a payout method.

Payout example

// 1. Start checkout with the payout setup session's client secret
const checkout = pay.checkout({ clientSecret })

// 2. Retrieve saved payout-capable sources
const existingSources = await checkout.getExistingSources()

// 3. Customer selects a saved source: register the selection
const selected = existingSources[0]

try {
  const result = await checkout.pay({ source: selected.id })

  // 4. Tell your server the recipient is done. It retrieves the setup
  // session, checks that it's completed for this recipient, and pays out
  // to its payment_method (amount in minor units: 10000 is $100.00):
  // POST /v1/payouts
  // {
  //   "amount": 10000,
  //   "currency": "usd",
  //   "customer_reference_id": "user_10",
  //   "destination": "<the setup session's payment_method>"
  // }
  await tellServerRecipientIsDone()
} catch (error) {
  console.error('Selection failed:', error.message)
}

Next steps

Was this page helpful?