Pay with a saved payment method
In a sale or authorization session, list the customer's saved methods, ask for a saved card's CVV when needed, and charge the one they pick.
In a sale or authorization session, the customer can pay with one of their saved payment methods, such as a card, a PayPal account, or a bank account. checkout.savedPaymentMethods() lists the saved methods of the customer the session was created for (its customer_reference_id), and checkout.pay() charges the one they pick. A saved method's requiresCvc tells you when to ask for a card's CVV.
const method = checkout.savedPaymentMethods()[0]
const { cvc } = usePaySavedCardCvc(method)
// Show <PaySavedCardCvcField cvc={cvc} /> when method.requiresCvc is true.
const result = await checkout.pay({ type: 'savedPaymentMethod', id: method.id, cvc })The drop-in shows the customer's saved methods for you, in a carousel. To show them on your own screen instead, use checkout.savedPaymentMethods(), requiresCvc, and checkout.pay().
List the saved methods
In a sale or authorization session, checkout.savedPaymentMethods() returns the customer's saved cards that haven't expired and their other saved methods the session can pay, such as PayPal accounts and bank accounts, as PaySavedPaymentMethod objects, in the order the API sends them. Passkeys, Click to Pay, Apple Pay, and Google Pay aren't listed. Some saved methods can be paid only in a sale session, so an authorization session doesn't list them.
A method's type indicates the payment method type, for example card or paypal. A card's card has the brand, the last 4 digits, and the expiration date. A bank account's bankAccount has the bank's name and the last 4 digits, and a PayPal account has its email.
Ask for the CVV
A method's requiresCvc is true when the session checks CVVs and Pay.com doesn't hold one for the card. It's always false in a setup session, and for a saved method that isn't a card.
When it's true, show a PaySavedCardCvcField for the customer to type the CVV into, with a cvc from usePaySavedCardCvc(method). The hook follows the card's brand, so the input expects the right length (4 digits for American Express, 3 for other brands). Its state.status is 'valid' once the CVV is complete.
The CVV never reaches JavaScript. It stays in the native input, and goes from there only to Pay.com's vault.
Pay
Call checkout.pay({ type: 'savedPaymentMethod', id: method.id, cvc }). It resolves with a PayResult.
- A card. The SDK sends the saved card to Pay.com's vault, with the CVV when there is one. The payment then runs as it does for a card typed into the card fields, including 3DS, any challenge over the app's current screen, and the amount check.
- When
requiresCvcisfalse,cvcis optional. An empty input sends nothing, and a CVV typed into it must be valid. - With client-side encryption on, the native SDK encrypts the CVV on the device first.
- When
- Another saved method, such as a PayPal account or a bank account. Pay.com charges it the same way as in Pay.com.js. It may open the provider's page, as a redirect method does, which comes back to the app at its return URL. The result can be
'processing', and your server learns the outcome from Pay.com. Leavecvcout.
Errors
pay() rejects with a PayError only when a call fails before sending a request:
| Error code | When |
|---|---|
invalid_card_input | For a card, requiresCvc is true and no cvc was passed, or the CVV isn't valid for the card's brand. error.fields is ['cvc']. |
unsupported | The id isn't in this checkout's savedPaymentMethods(). |
released | The checkout, or the cvc in the request, was released. |
payment_in_progress | Another pay() on this checkout is still running. |
no_screen | There's no screen to show 3DS or the provider's page on (no foreground activity on Android, or no view controller on iOS). |
Declines and the other failures come back as a 'failed' result, as for a new card.
Example
import { useState } from 'react'
import { Button, Text, View } from 'react-native'
import {
PayError,
PaySavedCardCvcField,
usePaySavedCardCvc,
type PayCheckout,
type PaySavedPaymentMethod,
} from '@pay-com/react-native'
function label(method: PaySavedPaymentMethod): string {
if (method.card) return `${method.card.brand ?? 'Card'} ending ${method.card.last4 ?? ''}`
if (method.bankAccount) return `${method.bankAccount.bankName ?? 'Bank account'} ending ${method.bankAccount.last4 ?? ''}`
return method.email ? `${method.type} (${method.email})` : method.type
}
export function SavedMethods({ checkout, onPaid }: { checkout: PayCheckout; onPaid: () => void }) {
const methods = checkout.savedPaymentMethods()
const [chosen, setChosen] = useState<PaySavedPaymentMethod | null>(methods[0] ?? null)
const { cvc, state } = usePaySavedCardCvc(chosen)
const [busy, setBusy] = useState(false)
const [message, setMessage] = useState<string | null>(null)
async function pay() {
if (!chosen) return
setBusy(true)
setMessage(null)
try {
const result = await checkout.pay({ type: 'savedPaymentMethod', id: chosen.id, cvc })
if (result.status === 'succeeded' || result.status === 'processing') {
onPaid() // A hint for your UI: your server confirms the payment before you fulfill the order.
} else if (result.status === 'failed') {
setMessage(result.error.code === 'declined' ? 'This payment method was declined.' : "The payment didn't go through.")
}
// 'canceled': the customer closed the 3DS screen or the provider's page, and can pay again.
} catch (error) {
// Nothing was sent.
if (error instanceof PayError && error.code === 'invalid_card_input') setMessage('Check the CVV.')
else setMessage('Something went wrong. Try again.')
} finally {
setBusy(false)
}
}
return (
<View>
{methods.map((method) => (
<Button key={method.id} title={label(method)} onPress={() => setChosen(method)} />
))}
{chosen?.requiresCvc ? <PaySavedCardCvcField cvc={cvc} placeholder="CVV" /> : null}
{message ? <Text>{message}</Text> : null}
<Button
title="Pay"
disabled={busy || !chosen || (chosen.requiresCvc && state.status !== 'valid')}
onPress={pay}
/>
</View>
)
}Next steps
- Saved card CVV: The CVV input, its state, and its props.
- checkout.pay(): The other ways to pay, and what
pay()rejects with. - Handle the return URL: How the provider's page comes back to the app.