Skip to Content
Docs

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().

This applies to Headless checkout integrations, where you build your own checkout UI. If you're using Universal checkout, saved cards are displayed automatically. See Saved payment methods styling to customize their appearance.

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_secret and an initialized pay instance.
  • 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_id used 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. 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. amount is in minor units (2000 is $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_secret to your frontend.

  2. 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. 3

    Fetch saved payment methods

    Call checkout.getExistingSources() to get the customer's saved cards. Each source includes card details, such as brand, 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. 4

    Display saved cards

    Render each saved source as a selectable option. Use the card object 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 cvvExists is false, show a CVV input, and pass the CVV the customer enters to checkout.pay(). If it's true, you can skip the CVV.

  5. 5

    Submit the payment

    When the customer selects a saved card, call checkout.pay() with the source id. Include the cvv when the card's cvvExists is false.

    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 the transaction (charge ID), status, customer, paymentMethod, and amount. It doesn't call the onSuccess or onFailure callbacks. It rejects when the payment fails, so wrap it in try/catch. You can also listen with checkout.on(checkout.EVENT_TYPES.PAYMENT_SUCCESS, …).

    Always confirm payments server-side using webhooks. Don't rely on client-side results for order fulfillment.

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:

Was this page helpful?