Last Updated: 23 August 2024 | Change Log

Create a session to pay with a card

Use our Android SDK to secure your customer's card details by creating a session.

Full sample integration

You can see an example of the session generation here.

Set your views

When you create your checkout form, you must set the views that your customers use to enter their card details. You must use unique identifiers for your view configurations.

Here's an example of how you would set your view configurations.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >

    <!-- layout style attributes omitted -->
    <EditText android:id="@+id/pan" />

    <!-- layout style attributes omitted -->
    <EditText android:id="@+id/expiryDate" />

    <!-- layout style attributes omitted -->
    <EditText android:id="@+id/cvc" />

    <!-- layout style attributes omitted -->
    <Button android:id="@+id/submit_button" />

</LinearLayout>

Reference your views

You must now reference your view configurations. Use the same unique view identifiers as above.

Here's an example of how you would reference your view configurations.

package com.worldpay.access.checkout.sample.code

// android library imports omitted

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val pan = findViewById<EditText>(R.id.pan)
        val cvc = findViewById<EditText>(R.id.cvc)
        val expiryDate = findViewById<EditText>(R.id.expiryDate)
        val submit = findViewById<Button>(R.id.submit_button)
    }

}

Validate card details

You can optionally validate your customer's card details. You can find instructions here.

Create a session

Implementing callbacks

The SDK converts the customer's card details. The SDK then retrieves the session by invoking a callback to the implementation class of the SessionResponseListener interface.

You must create a class that implements our SessionResponseListener interface.

Your implementation receives notifications once the session request is complete.

You get the session as a Map<SessionType, String> or the exception as a AccessCheckoutException in case of an error.

Here is an example implementation of the SessionResponseListener interface.

package com.worldpay.access.checkout.sample.code

import com.worldpay.access.checkout.client.api.exception.AccessCheckoutException
import com.worldpay.access.checkout.client.session.listener.SessionResponseListener
import com.worldpay.access.checkout.client.session.model.SessionType

class MySessionResponseListener: SessionResponseListener {

    override fun onSuccess(sessionResponseMap: Map<SessionType, String>) {
        //TODO: handle the session response here
    }

    override fun onError(error: AccessCheckoutException) {
        //TODO: handle the exception here
    }

}

Initialize the AccessCheckoutClient

You must now initialize the AccessCheckoutClient using the AccessCheckoutClientBuilder.

To do this, you must provide your baseUrl, merchantId (checkout ID) and other parameters. See the table below for more information.

Here's an example of how you would initialize the SDK with the mandatory parameters and configurations.

package com.worldpay.access.checkout.sample.code

// android library imports omitted

import com.worldpay.access.checkout.client.session.AccessCheckoutClientBuilder
import com.worldpay.access.checkout.client.session.AccessCheckoutClient
import com.worldpay.access.checkout.client.session.model.SessionType

class MainActivity : AppCompatActivity() {

    private val baseUrl = "TARGET_BASE_URL"
    private val checkoutId = "YOUR_CHECKOUT_ID"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // reference views code ommited

        val sessionResponseListener = MySessionResponseListener()

        val accessCheckoutClient = AccessCheckoutClientBuilder()
                .baseUrl(baseUrl)
                .merchantId(checkoutId)
                .context(this)
                .sessionResponseListener(sessionResponseListener)
                .lifecycleOwner(this)
                .build()
    }

}
Important

When using the SDK with AndroidX Lifecycle 2.3.0 and above, the call to build() must be done on the main UI thread. This is due to changes to the LifecycleRegistry class used by the SDK which now enforces this constraint.

Mandatory parameters

ParameterDescription
baseUrl
  • For testing use: https://try.access.worldpay.com/
  • For live use: https://access.worldpay.com/
merchantIdYour unique checkout ID.
sessionResponseListenerThe callback listener that returns your customer's session.
contextAndroid Context
lifecycleAndroid LifecycleOwner

Generate CARD session

You must pass the customer's card details and the SessionType.CARD enum to the generateSessions method on the AccessCheckoutClient instance.

Here's an example of what you should do when your customer clicks the submit button.

package com.worldpay.access.checkout.sample.code

// android library imports omitted

import com.worldpay.access.checkout.client.session.AccessCheckoutClient
import com.worldpay.access.checkout.client.session.model.CardDetails
import com.worldpay.access.checkout.client.session.model.SessionType

class MainActivity : AppCompatActivity() {

    // fields ommitted

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val pan = findViewById<EditText>(R.id.pan)
        val cvc = findViewById<EditText>(R.id.cvc)
        val expiryDate = findViewById<EditText>(R.id.expiryDate)
        val submit = findViewById<Button>(R.id.submit_button)

        // AccessCheckoutClient creation code ommitted

        submit.setOnClickListener {
            val cardDetails = CardDetails.Builder()
                    .pan(pan.getInsertedText())
                    .expiryDate(expiryDate.getInsertedText())
                    .cvc(cvc.getInsertedText())
                    .build()

            accessCheckoutClient.generateSessions(cardDetails, listOf(SessionType.CARD))
        }
    }

}

Full code sample

Here's the full code sample of the steps above.

package com.worldpay.access.checkout.sample.code

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
import com.worldpay.access.checkout.sample.code.R

import com.worldpay.access.checkout.client.session.AccessCheckoutClient
import com.worldpay.access.checkout.client.session.model.CardDetails
import com.worldpay.access.checkout.client.session.model.SessionType

class MainActivity : AppCompatActivity() {

    private val baseUrl = "TARGET_BASE_URL"
    private val checkoutId = "YOUR_CHECKOUT_ID"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val pan = findViewById<EditText>(R.id.pan)
        val cvc = findViewById<EditText>(R.id.cvc)
        val expiryDate = findViewById<EditText>(R.id.expiryDate)
        val submit = findViewById<Button>(R.id.submit_button)

        val sessionResponseListener = MySessionResponseListener()

        val accessCheckoutClient = AccessCheckoutClientBuilder()
                .baseUrl(baseUrl)
                .merchantId(checkoutId)
                .context(this)
                .sessionResponseListener(sessionResponseListener)
                .lifecycleOwner(this)
                .build()

        submit.setOnClickListener {
            val cardDetails = CardDetails.Builder()
                    .pan(pan.getInsertedText())
                    .expiryDate(expiryDate.getInsertedText())
                    .cvc(cvc.getInsertedText())
                    .build()

            accessCheckoutClient.generateSessions(cardDetails, listOf(SessionType.CARD))
        }
    }

}
Caution

Do not validate the structure or length of the session resources. We follow HATEOS standard to allow us the flexibility to extend our APIs with non-breaking changes.

Create a verified token

Once you've received a CARD session you must create a verified token to take a payment.

Important

The CARD session has a lifespan of one minute and you can use it only once. If you do not create a token within that time, you must create a new CARD session value.


Next steps


Create a verified token