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.
const result = await checkout.pay({ source, cvv })Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
source | string | Yes | The id from a saved source returned by checkout.getExistingSources(). |
cvv | string | No | Card 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.
| Field | Type | Description |
|---|---|---|
transaction | string | The charge ID (for example, "chrg_742353659387446272"). |
customer | string | The Pay.com customer ID. |
status | string | Payment status, for example "succeeded", or "pending" when the payment isn't final yet. |
paymentMethod | string | The stored payment method ID used for this charge. |
amount | number | The charged amount in minor units. |
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
- Pay with a saved card: Full guide for building a saved card checkout flow.
- Let recipients choose a payout method: Full guide for the payout method selection flow.
- checkout.getExistingSources(): Retrieve the customer's saved payment methods or payout sources.
- Save a payment method: Store a customer's card for future use.