Last Updated: 22 January 2024 | Change Log

Card Validation

Validate your customer's card details before processing them.

Warning

The validation does not check if your customer's card details are correct. The validator only checks the formatting of the entered details.

Get started

To integrate the validation feature you must create an implementation of a AccessCheckoutCardValidationListener. Initialize the validation using the AccessCheckoutValidationInitialiser by passing in the android UI elements in a CardValidationConfig.

Full sample integration

You can see an example of the card validation integration here.

Implementing AccessCheckoutCardValidationListener

To receive validation results of your customer's card details you are required to create your own implementation of the AccessCheckoutCardValidationListener

  1. Kotlin
  2. Java
package com.worldpay.access.checkout.sample.code

import com.worldpay.access.checkout.client.validation.listener.AccessCheckoutCardValidationListener
import com.worldpay.access.checkout.client.validation.model.CardBrand

class CardValidationListener : AccessCheckoutCardValidationListener {

    override fun onCvcValidated(isValid: Boolean) {
        //TODO: handle the cvc validation result
    }

    override fun onPanValidated(isValid: Boolean) {
        //TODO: handle the pan validation result
    }

    override fun onBrandChange(cardBrand : CardBrand?) {
        //TODO: handle the card brand change
    }

    override fun onExpiryDateValidated(isValid: Boolean) {
        //TODO: handle the expiry date validation result
    }

    override fun onValidationSuccess() {
        //TODO: handle the form when the validation is complete i.e. all fields are valid
    }

}

Function and parameter descriptions

MethodDescription
onCvcValidatedThis method is called with the validity of the CVC field. isValid indicates whether the field is in a valid or invalid state.
onPanValidatedThis method is called with the validity of the pan field. isValid indicates whether the field is in a valid or invalid state.
onBrandChangeThis method is called with a card brand based on the details that your customer is entering. You can use this method to determine the logo of the card, which can be applied as a UI effect. If the card brand can't be identified, a null response is returned.
The card brand contains a list of images which you may then use to display an icon for the identified card brand. Access Worldpay hosts both PNG and SVG versions of the supported card brands. You can use the icons right away and apply them to your views.
onExpiryDateValidatedThis method is called with the validity of the expiry date field. isValid indicates whether the field is in a valid or invalid state.
onValidationSuccessThis method is called when all fields are in a valid state. You typically use this to enable the submit button.

Initializing card validation

After implementing the AccessCheckoutCardValidationListener, you must initialize the validation for your views. To do this, first create a CardValidationConfig using the builder as shown below. Then use this to initialize the validation.

  1. Kotlin
  2. Java
package com.worldpay.access.checkout.sample.code

// android library imports omitted

import com.worldpay.access.checkout.client.validation.AccessCheckoutValidationInitialiser
import com.worldpay.access.checkout.client.validation.config.CardValidationConfig

class MainActivity : AppCompatActivity() {

    private val baseUrl = "TARGET_BASE_URL"

    // other fields omitted

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

        val panAccessCheckoutView: AccessCheckoutEditText= findViewById<AccessCheckoutEditText>(R.id.card_flow_text_pan)
        val cvcAccessCheckoutView: AccessCheckoutEditText = findViewById<AccessCheckoutEditText>(R.id.card_flow_text_cvc)
        val expiryDateAccessCheckoutView: AccessCheckoutEditText = findViewById<AccessCheckoutEditText>(R.id.card_flow_text_expiryDate)

        // other view references omitted

        val cardValidationListener = CardValidationListener()

        val cardValidationConfig = CardValidationConfig.Builder()
                .baseUrl(baseUrl)
                .pan(panAccessCheckoutView)
                .expiryDate(expiryDateAccessCheckoutView)
                .cvc(cvcAccessCheckoutView)
                .validationListener(cardValidationListener)
                .lifecycleOwner(this)
                .build()

        AccessCheckoutValidationInitialiser.initialise(cardValidationConfig)

        //TODO: generate session here
    }

}

Card validation rules

The validation logic in the SDK, is based off a set of default rules for any card type. Specific brand rules are fetched from a CardTypes JSON file, which holds the validation rules and card brand logos for each brand. The icons are available in both SVG and PNG.

Validation rules

The table below shows the rules that our SDK uses to validate your customer's card details.

Card NameBIN RangePAN LengthCVC Length
Amex34, 37154
Diners300-305, 3095, 36, 38, 3914, 16, 193
Discover6011, 644 - 649, 6516, 193
JCB2131, 1800, 3088 - 3094, 3096 - 3102, 3112 - 3120, 3158 - 3159, 3337 - 3349, 3528 - 358916, 17, 18, 193
Maestro493698, 500000 - 506698, 506779 - 508999, 56 - 59, 63, 67, 612, 13, 14, 15, 16, 17, 18, 193
MasterCard51 - 55, 2221 - 2229, 223 - 229, 23 - 26, 270 - 271, 2720: optimized using 22 - 27163
Visa413, 16, 18, 193

Card brand restriction

The SDK enables you to optionally provide a list of card brands that you support. This means that if you do not support a certain card brand the SDK notifies your code of an invalid PAN if an unsupported brand is recognized.

By default, the SDK allows cards from any brand. If you do not wish to restrict the card brands that you accept then you do not need to pass any configuration.

Example configuration

To restrict the card brands that you accept, simply pass in an array of the brands that you do wish to accept when initializing the SDK.

The following validation configuration restricts the SDK to accept only American Express, Visa or Mastercard BIN ranges.

  1. Kotlin
  2. Java
val cardValidationConfig = CardValidationConfig.Builder()
    ...
    .acceptedCardBrands(arrayOf("visa", "mastercard", "amex"))
    ...
    .build()

Currently supported card brands

The SDK is able to recognize the following card brands:

BrandCode
American Express"amex"
Diners"diners"
Discover"discover"
JCB"jcb"
Maestro"maestro"
Mastercard"mastercard"
Visa"visa"
Note

If the SDK does not recognize a PAN as one of the above brands, it will be permitted as long as it meets the usual criteria for a valid PAN.

PAN formatting

The SDK allows PAN formatting as the customer types. This feature is disabled by default.

The PAN is formatted in the following way:

Card TypeFormatting
Visa, Mastercard, JCB, Discover, Diners, MaestroXXXX XXXX XXXX XXXX
AmexXXXX XXXXXX XXXXX

Enabling PAN formatting

To enable the PAN formatting behavior, simply call the enablePanFormatting method on the builder.

  1. Kotlin
  2. Java
val cardValidationConfig = CardValidationConfig.Builder()
    ...
    .enablePanFormatting()
    ...
    .build()

Next steps


Create a session to pay with a card or
Create sessions to pay with a card and cvc