Last Updated: 15 May 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:

  • Initialize and configure the useAccessCheckout hook.
  • Create an implementation of a CardValidationEventListener to be notified of validation events.
  • Get an instance of the initaliseValidation function exposed by the useAccessCheckout hook.
  • Call that function once your UI components are present in the DOM in order to effectively initialize the validation
Full Sample Integration

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

Initialize the useAccessCheckout hook

First step to implement the validation is to configureuseAccessCheckout, if you haven't already.

To do this, you must provide your baseUrl, checkoutId and the PAN, Expiry Date and CVC identifiers which you defined as the nativeID property within the AccessCheckoutTextInput component.

Here's an example of how to initialize it.

import {useAccessCheckout, useCardConfig} from '@worldpay/access-worldpay-checkout-react-native-sdk';

export default function CardFlow() {

    const {initialiseValidation} = useAccessCheckout({
        baseUrl: 'https://try.access.worldpay.com',
        checkoutId: 'YOUR_CHECKOUT_ID',
        config: useCardConfig({
            panId: 'panInput',
            expiryDateId: 'expiryDateInput',
            cvcId: 'cvcInput'
        }),
    });
}

Parameters

ParameterDescription
baseUrl
  • For testing use: https://try.access.worldpay.com/
  • For live use: https://access.worldpay.com/
checkoutIdYour unique checkout ID.
panIdThe nativeID assigned to your Pan AccessCheckoutTextInput component.
expiryDateIdThe nativeID assigned to your Expiry date AccessCheckoutTextInput component.
cvcIdThe nativeID assigned to your CVC AccessCheckoutTextInput component.

Implement your CardValidationEventListener

To receive validation results of your customer's card details, you are required to create your own implementation of the CardValidationEventListener interface. Each of the function of this interface is optional to give you flexibility to listen to the events that you care about.

const validationEventListener = {
  onCardBrandChanged(brand) {
    // TODO: handle the card brand change
    // brand is an object properties name and images
    // name contains the name of one of the brands supported and detected by the SDK
    // images is an array of objects where each object has a type (image/png or image/svg+xml) and a url property
    // brand will be undefined in the case where the PAN changes from a detected to an undetected card brand
  },

  onPanValidChanged(isValid) {
    // TODO: handle the pan validation result
  },

  onExpiryDateValidChanged(isValid) {
    // TODO: handle the expiry date validation result
  },

  onCvcValidChanged(isValid) {
    // TODO: handle the cvc validation result
  },

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

Functions

FunctionDescription
onCardBrandChangedThis function is called with a card brand based on the details that your customer is entering. The card brand contains a name as well as an array of images. If the card brand can't be identified, an undefined brand is passed to the function.
You may want to use the array of images 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 UI.
onPanValidChangedThis function is called with the validity of the PAN field. isValid indicates whether the field is in a valid or invalid state.
onExpiryDateValidChangedThis function is called with the validity of the Expiry Date field. isValid indicates whether the field is in a valid or invalid state.
onCvcValidChangedThis function is called with the validity of the CVC field. isValid indicates whether the field is in a valid or invalid state.
onValidationSuccessThis function is called when all fields are in a valid state. You typically use this to enable the submit button.

Configure useAccessCheckout to enable validation

To receive validation results of your customer's card details, you are required to create your own implementation of the CardValidationEventListener interface. Each of the function of this interface is optional to give you flexibility to listen to the events that you care about.

Here is an example detailing how to do this.

import {
  useAccessCheckout,
  useCardConfig
} from '@worldpay/access-worldpay-checkout-react-native-sdk';

export default function CardFow() {
  const validationEventListener = {
    //...
  }
  const {initialiseValidation, ...} = useAccessCheckout({
    ...,
    config: useCardConfig({
      panId: 'panInput',
      expiryDateId: 'expiryDateInput',
      cvcId: 'cvcInput',
      validationConfig: {
        validationListener: validationEventListener,
      },
    }),
  });
}

Initialize the validation

You can now initialize the validation using the initialiseCardValidation() function. Before doing so, you need to make sure that your UI components for PAN, Expiry Date and CVC are available in the DOM.

Validation initialization is an asynchronous process so make sure to handle the promise returned by the function.

import React from 'react';
import {View} from 'react-native';
// ...

export default function CardFlow() {
  // ...

  const onLayout = () => {
    initialiseValidation()
      .then(() => {
        // You may want to optionally perform some actions once validation has been successfully initialized.
      })
      .catch(e => {
        // do something in case of error
      });
  };

  return (
    <View onLayout={onLayout}>
      ...
    </View>
  );
}

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 dowish to accept when initializing the SDK.

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

const {initialiseValidation, ...} = useAccessCheckout({
  ...
  config: useCardConfig({
    ...,
    validationConfig: {
      validationListener: validationEventListener,
      acceptedCardBrands: ['visa', 'mastercard', 'amex'],
    }
  }),
});

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 pass true in the enablePanFormatting property of your CardValidationConfig.

const {initialiseValidation, ...} = useAccessCheckout({
  ...
  config: useCardConfig({
    ...,
    validationConfig: {
      validationListener: validationEventListener,
      enablePanFormatting: true,
    }
  }),
});

Full code sample

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

import React from 'react';
import {View} from 'react-native';
import {
  AccessCheckoutTextInput,
  useAccessCheckout,
  useCardConfig,
} from '@worldpay/access-worldpay-checkout-react-native-sdk';

export default function CardFlow() {

  const validationEventListener = {
    onCardBrandChanged(brand?: Brand): void {
      // TODO: handle the card brand change
      // brand is an object properties name and images
      // name contains the name of one of the brands supported and detected by the SDK
      // images is an array of objects where each object has a type (image/png or image/svg+xml) and a url property
      // brand will be undefined in the case where the PAN changes from a detected to an undetected card brand
    },
      onPanValidChanged(isValid: boolean): void {
      // TODO: handle the cvc validation result
    },
      onExpiryDateValidChanged(isValid: boolean): void {
      // TODO: handle the cvc validation result
    },
      onCvcValidChanged(isValid: boolean): void {
      // TODO: handle the cvc validation result
    },
      onValidationSuccess(): void {
      // TODO: handle the form when the validation is complete i.e. all fields are valid
    },
  };

  const {initialiseValidation, ...} = useAccessCheckout({
    baseUrl: 'https://try.access.worldpay.com',
    checkoutId: 'YOUR_CHECKOUT_ID',
    config: useCardConfig({
      panId: 'panInput',
      expiryDateId: 'expiryDateInput',
      cvcId: 'cvcInput',
      validationConfig: {
        validationListener: validationEventListener,
      },
    }),
  });

  const onLayout = () => {
    initialiseValidation()
      .then(() => {
        // You may want to optionally perform some actions once validation has been successfully initialized.
      })
      .catch(e => {
        // do something in case of error
      });
  };

  return (
    <View onLayout={onLayout}>
      <AccessCheckoutTextInput
          nativeID="panInput"
          placeholder="Pan"
      />
      <AccessCheckoutTextInput
          nativeID="expiryDateInput"
          placeholder="MM/YY"
      />
      <AccessCheckoutTextInput
          nativeID="cvcInput"
          placeholder="CVC"
      />
    </View>
  );
}

Next steps


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