Pay with a saved card
Let returning customers pay using a previously saved card with Headless checkout.
Returning customers who have saved a card from a previous purchase can
pay without re-entering their card details. You fetch their saved cards
with checkout.getExistingSources(), display them in your UI, and
complete the payment with checkout.pay().
To charge a saved card from your server instead, pass its payment method ID
(pm_card_…) in the source field of a
POST /v1/charges request.
See Create recurring charges for
examples of each API approach to charging stored cards.
Requirements
Before you begin:
- Complete the
Headless checkout guide.
You'll need a
client_secretand an initializedpayinstance. - The customer must have at least one saved payment method. See Save a payment method for how to store a card.
- The payment session must include the same
customer_reference_idused when the card was saved.
Charge a saved card
Follow these steps to show a returning customer their saved cards and charge the one they pick.
- 1
Create a payment session
Create a payment session on your server with the customer's
customer_reference_id. This links the session to the customer's saved cards.amountis in minor units (2000is $20.00).POST https://api.sandbox.pay.com/v1/sessions/payment{ "amount": 2000, "currency": "usd", "customer_reference_id": "cust-jane-smith-001", "payment_method_types": ["card"] }Pass the
client_secretto your frontend. - 2
Initialize Pay.com.js and checkout
Set up Pay.com.js with your merchant ID (from the Developers tab of the Pay.com dashboard), and create a checkout instance with the session's
client_secret.import Pay from '@pay-com/js' // identifier is your Pay.com merchant ID, not the session ID const pay = await Pay.com({ identifier: 'YOUR_MERCHANT_ID' }) const checkout = pay.checkout({ clientSecret: session.client_secret, // Called by checkout.headless(). checkout.pay() doesn't call these: // use its resolved value and catch its rejection instead. onSuccess: (payment) => { console.log('Payment succeeded:', payment.transaction) }, onFailure: (error) => { console.log('Payment failed:', error.message) }, }) - 3
Fetch saved payment methods
Call
checkout.getExistingSources()to get the customer's saved cards. Each source includes card details, such asbrand,last4, and the expiration date, that you can use in your UI.const existingSources = await checkout.getExistingSources()The method returns an array of saved source objects. If the customer has no saved cards, the array is empty.
[ { "type": "card", "id": "pm_card_742346737426893824", "token": "tok_card_685104577720095744", "card": { "brand": "visa", "last4": "4242", "expMonth": "12", "expYear": "2028", "funding": "debit", "cvvExists": false } } ] - 4
Display saved cards
Render each saved source as a selectable option. Use the
cardobject to show the brand, last four digits, and expiration date.existingSources.forEach(source => { if (source.card) { const label = `${source.card.brand} ending in ${source.card.last4}` const expiry = `Expires ${source.card.expMonth}/${source.card.expYear}` console.log(label, expiry) } })If
cvvExistsisfalse, show a CVV input, and pass the CVV the customer enters tocheckout.pay(). If it'strue, you can skip the CVV. - 5
Submit the payment
When the customer selects a saved card, call
checkout.pay()with the sourceid. Include thecvvwhen the card'scvvExistsisfalse.try { const result = await checkout.pay({ source: selectedSource.id, cvv: '123', }) console.log(result.status) // 'succeeded' console.log(result.transaction) // 'chrg_742353659387446272' } catch (error) { // The payment was declined or failed console.log('Payment failed:', error.message) }checkout.pay()returns the payment result directly as a resolved promise. The result includes thetransaction(charge ID),status,customer,paymentMethod, andamount. It doesn't call theonSuccessoronFailurecallbacks. It rejects when the payment fails, so wrap it intry/catch. You can also listen withcheckout.on(checkout.EVENT_TYPES.PAYMENT_SUCCESS, …).
Complete example
This example fetches the customer's saved cards and pays with the first one. When the customer has no saved cards, it falls back to a new card.
import Pay from '@pay-com/js'
// identifier is your Pay.com merchant ID, not the session ID
const pay = await Pay.com({ identifier: 'YOUR_MERCHANT_ID' })
const checkout = pay.checkout({
clientSecret: session.client_secret,
// Called by checkout.headless(). checkout.pay() doesn't call these:
// use its resolved value and catch its rejection instead.
onSuccess: (payment) => {
console.log('Payment succeeded:', payment.transaction)
},
onFailure: (error) => {
console.log('Payment failed:', error.message)
},
})
// Fetch saved cards
const existingSources = await checkout.getExistingSources()
if (existingSources.length > 0) {
// Display saved cards in your UI
existingSources.forEach(source => {
console.log(`${source.card.brand} ending in ${source.card.last4}`)
})
// Customer selects a card and pays
try {
const result = await checkout.pay({
source: existingSources[0].id,
cvv: '123',
})
console.log('Charge ID:', result.transaction)
} catch (error) {
console.log('Payment failed:', error.message)
}
} else {
// No saved cards, fall back to new card form.
// checkout.headless() calls onSuccess or onFailure, and also rejects on failure.
try {
await checkout.headless('card', {
number: '4242424242424242',
cvv: '123',
expirationMonth: '12',
expirationYear: '28',
name: 'Jane Smith',
})
} catch {
// Already handled in onFailure
}
}Next steps
Related saved card guides:
- Display available payment methods: Build a dynamic payment method selector for new payments.
- Save a payment method: Store a card for future use.
- checkout.getExistingSources(): Full method reference with card object details.
- checkout.pay(): Full method reference with parameters and return value.