Tokenizing a card in a mobile app

Once you have an invoice for a payment, the next step is usually to generate a payment source token to
represent the customer's payment details. The most common example of a payment source is a card token. (See the page about accepting card payments) to learn more about card tokens and their place in the payment flow.)

We provide client-side SDKs for iOS and Android. These let you generate a card token directly from a customer's details as described below. You can also use our SDKs to accept payment with Apple Pay and Google Pay, which are described separately.

Tokenizing a card

First, you should obtain and set up the SDK in your code using the instructions for iOS or Android, as appropriate.

The code sample below shows how to tokenize a set of card details that you have already received from the customer (you can use any suitable UI to do this). The token itself is just a string value that represents the card for this particular payment.

// First, create a tokenization request with card details. Check `init` for
// additional parameters that could be passed when creating a request.
let tokenizationRequest = POCardTokenizationRequest(
    number: "4242424242424242", expMonth: 04, expYear: 30, cvc: "737"
)

// Send request.
ProcessOut.shared.cards.tokenize(request: tokenizationRequest) { result in
    switch result {
    case .success(let card):
        // Send card.id to your backend to charge a customer
    case .failure(let failure):
        // Inspect `failure.code` for detailed error reason
    }
}

// Alternatively, use an async method.
let card = try await ProcessOut.shared.cards.tokenize(request: tokenizationRequest)
val tokenizationRequest = POCardTokenizationRequest(
    number = "4242424242424242",
    expMonth = 4,
    expYear = 30,
    cvc = "737"
)

// Coroutine function
ProcessOut.instance.cards.tokenize(tokenizationRequest).let { result ->
    when (result) {
        // Send the card ID to your backend to charge a customer.
        is ProcessOutResult.Success -> result.value.id
        // Inspect POFailure.Code for the detailed error reason.
        is ProcessOutResult.Failure -> result.code
    }
}

// Callback function
ProcessOut.instance.cards.tokenize(
    request = tokenizationRequest,
    callback = object : ProcessOutCallback<POCard> {
        override fun onSuccess(result: POCard) {
            // Send the card ID to your backend to charge a customer.
        }

        override fun onFailure(
            code: POFailure.Code,
            message: String?,
            invalidFields: List<POFailure.InvalidField>?,
            cause: Exception?
        ) {
            // Inspect POFailure.Code for the detailed error reason.
        }
    }
)

What’s Next

Once you have generated the card token successfully, you can use it to process a payment. For Cardholder Initiated Transactions, you will first need to authorize the payment on the client before capturing it on the server. You may also be interested in saving the token to capture future payments.