Client-side encryption on the web
Pay with your own card form through checkout.cse(), or encrypt cards for your server with PayCSE.create().
Pay.com.js encrypts cards with PayCSE in two ways:
checkout.cse()pays from your own card form. The card is encrypted on your page, and only the token goes to Pay.com, through the checkout's frame.PayCSE.create()encrypts without a checkout, so your server can send the token with its API key.
The encryption code ships as its own script, cse.js, next to the Pay.com.js script. Pages that don't use it never load it.
Pay with your own card form
Set up the checkout as usual with the session's client_secret (see Install and initialize), then call checkout.cse():
const checkout = pay.checkout({ clientSecret })
// Starts loading cse.js and the encryption keys while the customer types.
const cse = checkout.cse()
form.addEventListener('submit', async (event) => {
event.preventDefault()
try {
const payment = await cse.pay({
number: form.number.value,
expMonth: form.expMonth.value,
expYear: form.expYear.value,
cvc: form.cvc.value,
holderName: form.name.value,
})
// A hint for your UI: confirm the payment on your server.
} catch (error) {
// error.code says what went wrong; error.data.field names a bad field, never its value.
}
})checkout.cse() returns an object with two methods. Both encrypt on your page, bound to the checkout's session, and resolve with the payment the same way checkout.pay() does. One payment runs at a time per page. A second pay() or paySource() call while one runs resolves with undefined and does nothing.
If you set a beforePay on pay.checkout(), pay() and paySource() call it once, after Pay.com.js has checked that the card or CVV can be encrypted and before anything is sent. If it stops the payment, they reject with { message, data: { stoppedBy: 'beforePay' } } and emit nothing. They also reject and emit nothing if the checkout moved to another session in the meantime, with the message "The transaction was replaced by a new checkout session". The card is encrypted again after beforePay returns.
| Method | Description |
|---|---|
pay(card, { saveSourceForFutureUse }) | Pays with a card from your form. saveSourceForFutureUse asks to save the card for the customer. Default: false. |
paySource({ source, cvc }) | Pays with a saved card and the CVC the customer typed again for it. Only the CVC is encrypted. source is a saved card, as checkout.pay({ source }) takes it. Any other kind of source is refused. |
The SDK emits payment_processing when a payment starts, and payment_success or payment_pending when it ends. A refusal on your page emits payment_failure with the error's message and data.
If Pay.com rotated its key after the page fetched the keys (decline_code: "cse_unknown_key"), the SDK fetches the keys again, encrypts again, and retries once.
Card details
pay() and encryptCard() take the card in this shape:
| Field | Type | Rules |
|---|---|---|
number | string | 12 to 19 digits. Spaces and dashes are removed. |
expMonth | string | number | 1 to 12, for example 1, "1", or "01". |
expYear | string | number | Four digits, or two ("30" is 2030). |
cvc | string | Optional. 3 or 4 digits. An empty string counts as absent. |
holderName | string | Optional. The name on the card, up to 255 characters, no control characters. An empty string counts as absent. |
The SDK checks the format only. Luhn and expiration checks happen at Pay.com after decryption, as for any card.
Encrypt without a checkout
PayCSE.create() encrypts cards and CVVs into tokens your server sends to the API with its API key. Load cse.js from Pay.com, which exposes the global PayCSE:
<script src="https://js.pay.com/v1/cse.js"></script>const encryptor = PayCSE.create({
merchantId: 'YOUR_MERCHANT_ID',
environment: 'sandbox',
})
const encryptedCard = await encryptor.encryptCard({
number: '4242 4242 4242 4242',
expMonth: '12',
expYear: '2030',
cvc: '737',
})
// Send encryptedCard to your server, which sends it to Pay.com.create() checks its options and throws synchronously when one is unusable (invalid_options). It also starts fetching the environment's keys right away. A failed fetch doesn't throw there. It shows up as keys_unavailable from the first encryptCard() or encryptCvc() call.
Options
PayCSE.create() takes these options:
type CseOptions = {
// Your Pay.com merchant ID, as a string: 1 to 64 characters of A-Z a-z 0-9 _ -.
// Tokens are accepted only for this merchant.
merchantId: string
// 'live': keys from https://api.pay.com/cse/keys, aud api.pay.com.
// 'sandbox': the sandbox, with keys from https://api.sandbox.pay.com/cse/keys
// and aud api.sandbox.pay.com. Allow that host in your page's CSP connect-src.
environment: 'live' | 'sandbox'
// Binds tokens to one payment session: 'ps_' followed by 18 to 20 digits,
// or 'ss_' for a setup session. Leave it out for tokens your server sends with its API key.
sessionId?: string
}The merchant ID is the value a checkout session carries as account_settings.id. checkout.cse() reads it from the session itself.
Set sessionId only when the token is sent with that session's client_secret. A token your server sends with its API key must have none, or Pay.com refuses it with cse_session_mismatch. checkout.cse() sets it for you.
Encryptor
create() returns an encryptor with these methods:
| Method | Returns | Description |
|---|---|---|
encryptCard(card) | Promise<string> | Encrypts a card (see Card details) into a token for encrypted_card. |
encryptCvc(cvc) | Promise<string> | Encrypts the CVC of a saved card into a token for encrypted_cvv. |
refreshKeys() | Promise<void> | Fetches the keys again, past every cache. Call it once when Pay.com returns decline_code: "cse_unknown_key", then encrypt again and retry once. |
PayCSE.version is the script's version, and PayCSE.CseError is the error class.
Pay.com refuses a token as expired once it's more than 1200 seconds old, and accepts each token once, so encrypt when the customer submits, not earlier. To send the token from your server, see Send encrypted cards from your server.
Errors
Errors are Errors whose name is 'CseError', with a code and data: { code, field? }. Messages and data never contain card data.
PayCSE.create()throws synchronously forinvalid_options. The encryptor's methods reject with aPayCSE.CseError.checkout.cse()'s methods reject with the same shape.
code | Meaning |
|---|---|
invalid_card | A card field is missing or malformed. data.field names it: number, expMonth, expYear, cvc, or holderName. |
invalid_options | create() got an unusable merchantId, sessionId, or environment. data.field names it. |
keys_unavailable | The keys couldn't be fetched, or none is usable. Nothing was encrypted. |
unsupported_browser | The browser has no WebCrypto (an insecure context or a very old browser). |
encryption_failed | WebCrypto refused the key or the operation. |
session_unavailable | The checkout session failed to load (checkout.cse() only). |
module_unavailable | cse.js failed to load, or the checkout frame doesn't support encrypted cards (checkout.cse() only). |
Refusals from Pay.com after the token was sent, such as an expired token, come back as payment errors whose data.declineCode is a cse_* code. See Errors.