Client-side encryption on mobile
Encrypt cards on iOS and Android, through the checkout or with the standalone PayCSE modules.
The iOS and Android SDKs encrypt cards with PayCSE in two ways:
- Through the checkout. Turn on
clientSideEncryptionand the checkout sends an encrypted card instead of the card fields. - With the standalone module.
PayCSEon iOS andpay-cseon Android encrypt cards from your own form, for your server to send with its API key. They don't depend on the rest of the SDK, so you can use and audit them on their own.
Through the checkout
Set clientSideEncryption to true in the checkout's configuration:
let checkout = try await PayCheckout(
clientSecret: secret,
configuration: .init(
returnURL: URL(string: "myshop://pay")!,
clientSideEncryption: true
)
)// PayCheckout.create is a suspend function: call it from a coroutine.
lifecycleScope.launch {
val checkout = PayCheckout.create(
secret,
PayCheckout.Configuration(returnUrl = "myshop://pay", clientSideEncryption = true),
)
}With it on:
- The SDK fetches the key set from
/cse/keyson its API host (https://js.pay.com/cse/keys) with the session'sclient_secret, while it loads the session. Pay.com picks the key set from theclient_secretand returns the audience to encrypt for. The SDK binds each token to the session. paysendsencrypted_cardinstead of the card fields, and a saved card's CVV (iOS, Android) asencrypted_cvv. The SDK never falls back to the raw card or CVV. If it can't encrypt them (no usable key, or no merchant ID on the session),payreturns.failed(.encryptionUnavailable)on iOS orPayResult.Failed(PayError.EncryptionUnavailable)on Android. It doesn't throw.- If Pay.com returns
cse_unknown_key, the SDK fetches the keys again once and retries.
With the standalone module
Use the module when your app collects the card in its own form and your server sends the token to the API. The module fetches the keys with a client_secret. Your server creates a tokenization session and sends its client_secret to the app. Bind the token to your merchant and to the key set's audience, and leave the session ID out.
import PayCSE
// Start fetching when the checkout screen loads, so the key is ready at submit.
// clientSecret is a tokenization session's client_secret, from your server.
let keys = CSEKeyProvider(clientSecret: clientSecret)
keys.prefetch()
// At submit:
let keySet = try await keys.keySet()
let key = try keySet.encryptionKey(at: Date())
let token = try CSEEncryptor().encryptCard(
CSECardDetails(number: number, expiryMonth: month, expiryYear: year, cvc: cvc, name: name),
claims: CSEClaims(
merchantID: "YOUR_MERCHANT_ID",
audience: keySet.audience,
sessionID: nil, // Your server sends the token with its API key
sdk: "ios/1.0.0"
),
key: key
)
// Send `token` to your server, which sends it as encrypted_card.data.import com.pay.sdk.cse.*
// Start fetching when the checkout screen loads, so the key is ready at submit.
// clientSecret is a tokenization session's client_secret, from your server.
val keys = CseKeyProvider(PayCse.KEYS_URL, clientSecret)
keys.prefetch()
// At submit, from a coroutine:
val keySet = keys.keySet()
val key = keySet.encryptionKey(System.currentTimeMillis() / 1000)
val token = CseEncryptor().encryptCard(
CseCardDetails(number, expiryMonth, expiryYear, cvc, name),
CseClaims(
merchantId = "YOUR_MERCHANT_ID",
audience = keySet.audience,
sessionId = null, // Your server sends the token with its API key
sdk = "android/1.0.0",
),
key,
)
// Send `token` to your server, which sends it as encrypted_card.data.Pay.com picks the key set, and the audience to encrypt for, from the client_secret you fetch the keys with. Your server then sends the token as described in Send encrypted cards from your server.
API
Each iOS member has an Android equivalent:
| iOS | Android | Description |
|---|---|---|
PayCSE.keysURL | PayCse.KEYS_URL | https://js.pay.com/cse/keys, the key endpoint. |
PayCSE.formatVersion | PayCse.FORMAT_VERSION | The token format the module makes: 1. |
CSEKeyProvider(keysURL:clientSecret:session:) | CseKeyProvider(keysUrl, clientSecret, http) | Fetches the key set for a client_secret, sent as Authorization: Bearer and never in the URL, and keeps it in memory for at most 24 hours. On iOS, keysURL defaults to PayCSE.keysURL. |
prefetch() | prefetch() | Starts fetching in the background. Errors surface on the next keySet. |
keySet(refresh:) | keySet(refresh) | The key set. refresh: true fetches again past every cache. Call it once when Pay.com returns cse_unknown_key. |
CSEKeySet.encryptionKey(at:) | CseKeySet.encryptionKey(nowSeconds) | The key to encrypt with, by the key rule. Throws when no key is usable. |
CSEKeySet.audience | CseKeySet.audience | The aud to put in tokens encrypted with the set's keys, for example api.pay.com. |
CSEKeySet(response:) | CseKeySet.parse(response) | Parses a /cse/keys response you fetched yourself: its audience and its usable keys. Throws when it has no audience. |
CSEPublicKey | CsePublicKey | A usable key: keyID / keyId and activeFrom. |
CSEEncryptor().encryptCard(_:claims:key:) | CseEncryptor().encryptCard(card, claims, key) | A token for encrypted_card. |
CSEEncryptor().encryptCVC(_:claims:key:) | CseEncryptor().encryptCvc(cvc, claims, key) | A token for encrypted_cvv, to re-collect a saved card's CVV. |
CSECardDetails / CseCardDetails take number, expiryMonth, expiryYear, cvc, and name. Values are normalized first. Spaces and dashes are removed from the number, a one-digit month gets its leading zero, and a two-digit year becomes 20yy.
CSEClaims / CseClaims set who the token is for:
| iOS | Android | Claim | Description |
|---|---|---|---|
merchantID | merchantId | mid | Your Pay.com merchant ID: the value a checkout session carries as account_settings.id. |
audience | audience | aud | The environment the token is for: the key set's audience, for example api.pay.com. |
sessionID | sessionId | sid | nil or null for a token your server sends with its API key. |
sdk | sdk | sdk | <platform>/<version>, for example ios/1.0.0. Informational. |
Errors
The module throws CSEError on iOS and CseException on Android. Neither ever contains card data.
| iOS | Android | When |
|---|---|---|
keysUnavailable(_:) | KeysUnavailable(reason) | The client_secret isn't usable, or the key set couldn't be fetched, isn't a key set, or has no audience. |
noUsableKey | NoUsableKey | The key set has no usable key. There is never a bundled fallback. |
invalidCard(field:) | InvalidCard(field) | A card value is outside the format after normalization. field names the payload member, never its value. |
invalidClaims(field:) | InvalidClaims(field) | A claim is outside the format. |
encryptionFailed | EncryptionFailed | The platform couldn't encrypt. |