Skip to Content
Docs

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 clientSideEncryption and the checkout sends an encrypted card instead of the card fields.
  • With the standalone module. PayCSE on iOS and pay-cse on 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
    )
)

With it on:

  • The SDK fetches the key set from /cse/keys on its API host (https://js.pay.com/cse/keys) with the session's client_secret, while it loads the session. Pay.com picks the key set from the client_secret and returns the audience to encrypt for. The SDK binds each token to the session.
  • pay sends encrypted_card instead of the card fields, and a saved card's CVV (iOS, Android) as encrypted_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), pay returns .failed(.encryptionUnavailable) on iOS or PayResult.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.

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:

iOSAndroidDescription
PayCSE.keysURLPayCse.KEYS_URLhttps://js.pay.com/cse/keys, the key endpoint.
PayCSE.formatVersionPayCse.FORMAT_VERSIONThe 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.audienceCseKeySet.audienceThe 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.
CSEPublicKeyCsePublicKeyA 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:

iOSAndroidClaimDescription
merchantIDmerchantIdmidYour Pay.com merchant ID: the value a checkout session carries as account_settings.id.
audienceaudienceaudThe environment the token is for: the key set's audience, for example api.pay.com.
sessionIDsessionIdsidnil or null for a token your server sends with its API key.
sdksdksdk<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.

iOSAndroidWhen
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.
noUsableKeyNoUsableKeyThe 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.
encryptionFailedEncryptionFailedThe platform couldn't encrypt.
Was this page helpful?