Skip to Content
Docs

Start a checkout

Check a session's client_secret, load the session from Pay.com, and get the checkout every other call goes through.

PayCheckout.create() checks the client_secret your server sent the app, loads the session from Pay.com, and returns a PayCheckout for it. Every other call goes through that checkout, including listing payment methods, paying, and presenting the drop-in.

Create one checkout per session. Your server creates the session first, with Create a payment session to take a payment, or with Create a setup session to save a payment method or let a customer choose a payout method, and sends the app only the session's client_secret.

create is a suspend function, so call it from a coroutine.

import com.pay.sdk.core.PayCheckout

val checkout = PayCheckout.create(clientSecret, PayCheckout.Configuration(returnUrl = "myshop://pay"))

Parameters

ParameterTypeRequiredDescription
clientSecretStringYesThe session's client_secret, from your server. Whitespace around it is trimmed. The SDK never prints it.
configurationPayCheckout.ConfigurationYesHow the checkout behaves. See PayCheckout.Configuration.

PayCheckout.Configuration

Pass its properties by name.

ParameterTypeRequiredDescription
returnUrlStringYesThe URL where redirect payment methods return the customer to your app (a custom scheme such as myshop://pay or an App Link). Your server creates the session with return_url set to the same URL, and your app passes incoming URLs to the SDK.
environmentPayEnvironmentNoThe Pay.com environment the SDK connects to. Keep the default for both live and sandbox sessions. Default: PayEnvironment.Production. See PayEnvironment.
challengeTimeoutMillisLongNoHow long the 3DS challenge screen may stay open, in milliseconds, before the SDK closes it and reads the payment's status instead. Default: 600_000 (10 minutes).
threeDsAppReturnModeBooleanNoAsks the 3DS challenge page to end with a redirect to returnUrl. Default: false.
clientSideEncryptionBooleanNoSends the card encrypted with PayCSE instead of the card fields, and a saved card's CVV encrypted as well. Leave it off until Pay.com turns client-side encryption on for your account. When it's on, create starts fetching the encryption keys from /cse/keys on the API host (https://js.pay.com/cse/keys) with the client_secret while it loads the session, and a card or a CVV that can't be encrypted fails with PayError.EncryptionUnavailable. The SDK never falls back to sending the card fields. See Client-side encryption on mobile. Default: false.

Returns

A PayCheckout for the session. Its session property exposes the session's kind (sale, authorization, or setup), amount, currency, and whether it's a payout session. See PayCheckout for every member.

The members that describe the session, such as paymentMethods(), read what create loaded, so they don't call Pay.com again. After your server changes the amount, refreshSession() reads it again.

Errors

create throws a PayError when it can't start the checkout:

ErrorWhen
PayError.InvalidClientSecretThe client_secret is empty, longer than 8192 characters, or contains a character that isn't printable ASCII, such as a space. Its format isn't otherwise checked. Nothing was sent.
PayError.SessionExpiredPay.com rejected the client_secret (HTTP 401). The session expired, or the secret isn't a session's.
PayError.RequestRejected(error)Pay.com returned another error status below 500.
PayError.Server(error)Pay.com failed (HTTP 5xx) or timed out (HTTP 408).
PayError.Network(detail)The request didn't reach Pay.com, or the response didn't arrive.
PayError.UnexpectedResponse(detail)Pay.com returned a session this SDK version can't read.

Example

class CheckoutViewModel(private val shopApi: ShopApi) : ViewModel() {
    private val _checkout = MutableStateFlow<PayCheckout?>(null)
    val checkout: StateFlow<PayCheckout?> = _checkout.asStateFlow()

    private val _message = MutableStateFlow<String?>(null)
    val message: StateFlow<String?> = _message.asStateFlow()

    fun start(orderId: String) {
        viewModelScope.launch {
            // Your server creates the payment session and returns only its client_secret.
            val clientSecret = shopApi.createPaymentSession(orderId)
            try {
                _checkout.value = PayCheckout.create(
                    clientSecret,
                    PayCheckout.Configuration(returnUrl = "myshop://pay"),
                )
            } catch (e: PayError.SessionExpired) {
                _message.value = "This checkout has expired. Please start again."
            } catch (e: PayError) {
                _message.value = "We couldn't start the checkout. Please try again."
            }
        }
    }
}

Next steps

Was this page helpful?