Start a checkout
Check a session's client_secret, load the session from Pay.com, and get the checkout that pays it.
PayCheckout.create() checks the client_secret of a session your server created, loads the session from Pay.com, and resolves with a PayCheckout for it. Call it first. The checkout then lists the session's methods with checkout.paymentMethods(), shows the drop-in with checkout.presentDropIn(), or pays with checkout.pay(). One checkout pays one session. Release it when you're done with it.
Your server creates the session with Create a payment session, or with Create a setup session to save a payment method without paying. It sets the session's return_url to the app's returnUrl, and sends only the session's client_secret to the app, never your API key.
const checkout = await PayCheckout.create(clientSecret, { returnUrl: 'myshop://pay' })Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
clientSecret | string | Yes | The session's client_secret, from your server. |
configuration | PayCheckoutConfiguration | Yes | How the checkout runs. |
configuration.returnUrl | string | Yes | The URL where redirect payment methods return to the app (a custom scheme such as myshop://pay, a universal link on iOS, or an App Link on Android). Create the session with return_url set to the same URL, and set up a deep link for it. |
configuration.environment | PayEnvironment | No | The Pay.com environment the SDK connects to. Keep the default, 'production', for both live and sandbox sessions. See Environments. |
configuration.challengeTimeoutSeconds | number | No | How long the 3DS challenge screen may stay open, in seconds, before the SDK reads the outcome from the payment's status instead. Default: 600. |
configuration.threeDSAppReturnMode | boolean | No | Asks the 3DS challenge to end with a redirect to returnUrl. Default: false. |
configuration.clientSideEncryption | boolean | No | Encrypts the card on the device and sends it encrypted, instead of the raw card fields. Leave it off until Pay.com turns on client-side encryption for your account. With it on, a card the SDK can't encrypt fails with encryption_unavailable. The SDK never falls back to sending the raw fields. Default: false. |
Environments
PayEnvironment is 'production', 'development', or { apiBaseUrl, scaBaseUrl }. Use 'production', the default, for both live and sandbox sessions.
The other two are for other deployments. In { apiBaseUrl, scaBaseUrl }, apiBaseUrl is the prefix of the client API's paths and scaBaseUrl serves 3DS. Both must be http or https URLs with a host.
Returns
PayCheckout.create() returns a promise that resolves with a PayCheckout:
| Member | Type | Description |
|---|---|---|
session | PaySession | The session the checkout pays. See The session. |
acceptedCardBrands | readonly string[] | The card brands the session accepts. See Accepted card brands. |
paymentMethods() | () => PayPaymentMethod[] | Lists the methods the session accepts. See List payment methods. |
savedPaymentMethods() | () => PaySavedPaymentMethod[] | Lists the customer's saved methods the session can use. See Let customers choose a payout method and Pay with a saved payment method. |
pay(request) | (request: PayPaymentRequest) => Promise<PayResult> | Pays from your own screen. See Pay. |
presentDropIn(options) | (options?: PayDropInOptions) => Promise<PayResult> | Shows the drop-in. See Present the drop-in. |
release() | () => void | Frees the native checkout. See Release a checkout. |
PayCheckout also has a static PayCheckout.handleReturn(). Its constructor is private, so PayCheckout.create() is the only way to get one.
The session
checkout.session is a PaySession:
| Field | Type | Description |
|---|---|---|
id | string | The session's ID. |
kind | 'sale' | 'authorization' | 'setup' | 'unknown' | 'sale' charges now. 'authorization' authorizes now, for your server to capture later. 'setup' saves a payment method without paying. 'unknown' is a flow the SDK doesn't recognize. It may charge, so its total is checked as a sale's is. |
amount | string | null | The amount in major units, as the client API sends it, for example "10.00". Your server created the session in minor units (1000). pay() and presentDropIn() update it when they find the total changed. |
currency | string | null | The ISO 4217 alpha-3 currency code. |
isLive | boolean | Whether the session is live. |
expiresAt | number | null | When the client_secret stops working, in milliseconds since the epoch, as for new Date(expiresAt). |
setupFutureUsage | string | null | 'on_session' or 'off_session' when the session saves the card whatever the customer picks. |
isRecurring | boolean | true for a recurring session, which always keeps the card for later charges. |
isPayout | boolean | true for a setup session your server created with payout: true, where a recipient chooses where their payouts go. See Let customers choose a payout method. |
Errors
PayCheckout.create() rejects with a PayError when it can't start the checkout:
| Error code | When |
|---|---|
invalid_client_secret | The client_secret is empty or isn't a token. |
session_expired | Pay.com no longer accepts the session's token, because the session expired or its last attempt retired it. Your server needs a new session. |
request_rejected | The API rejected the request. error.apiError has the details. |
server | The API failed with a 5xx, or returned a 408. |
network | The request didn't reach Pay.com, or the response didn't arrive. |
unsupported | The native SDK rejected the configuration, for example a returnUrl without a scheme, or an environment whose URLs aren't http or https URLs with a host. |
unexpected_response | Pay.com or the native SDK returned a response this SDK version can't read. |
PayCheckout.create() rejects with a TypeError instead, before anything is sent, when the configuration has an invalid type, such as a returnUrl that isn't a non-empty string, an environment that isn't 'production', 'development', or { apiBaseUrl, scaBaseUrl }, or a challengeTimeoutSeconds that isn't a positive number.
Example
import { PayCheckout, PayError } from '@pay-com/react-native'
export async function startCheckout(orderId: string): Promise<PayCheckout | null> {
// Your own server creates the session, with return_url myshop://pay,
// and returns only its client_secret.
const clientSecret = await fetchClientSecret(orderId)
try {
const checkout = await PayCheckout.create(clientSecret, { returnUrl: 'myshop://pay' })
console.log(checkout.session.kind, checkout.session.amount) // 'sale' '10.00'
return checkout // release it once the customer is done
} catch (error) {
if (error instanceof PayError && error.code === 'session_expired') {
return null // this session can't be paid: ask your server for a new one
}
throw error
}
}Next steps
- Present the drop-in: Take the payment in Pay.com's ready-made sheet.
- Card fields: Take it on your own screen instead.
- Handle the return URL: Set up the deep link for
returnUrl. - Create a payment session: The server call that gives you the
client_secret.