Overview

The Credit Card model extracts and structures information from credit cards, handling various card types, validation rules, and security considerations. The model includes comprehensive utilities for card number formatting, validation, and secure display.

Model Details

  • Model ID: CREDIT_CARD
  • Version: GA
  • Release Date: November 30, 2024

Payment Networks

The model supports these major payment networks:

  • VISA
  • MASTERCARD
  • AMERICAN EXPRESS
  • DISCOVER
  • OTHER

Schema Structure

Root Level Fields

FieldTypeDescription
CardNumberstring?A unique identifier for the card (Luhn validated)
IssuingBankstring?The name of the bank that issued the card
PaymentNetworkstring?The payment network that processes transactions
CardHolderNamestring?The name of the person who owns the card
CardHolderCompanyNamestring?The company name associated with the card
ValidDatestring?Valid from date (MM/YY format)
ExpirationDatestring?Expiration date (MM/YY format)
CardVerificationValuestring?Card verification value (CVV)
CustomerServicePhoneNumbersstring[]Array of customer service numbers

Utility Functions

The model provides comprehensive utility functions:

maskCardNumber

Securely masks the card number:

const maskCardNumber = (cardNumber: string): string;
// Returns: "****-****-****-1234"

isExpired

Checks if the card is expired:

const isExpired = (expirationDate: string): boolean;
// Returns: true/false based on current date

formatCardNumber

Formats card number with spaces:

const formatCardNumber = (cardNumber: string): string;
// Returns: "4275 0000 0000 0000"

getCardType

Determines payment network from card number:

const getCardType = (cardNumber: string): PaymentNetwork;
// Returns: "VISA" | "MASTERCARD" | "AMERICAN EXPRESS" | "DISCOVER" | "OTHER"

formatPhoneNumber

Formats phone numbers consistently:

const formatPhoneNumber = (phoneNumber: string): string;
// Returns: "+1 (987) 123-4567"

Example Usage

import { validateCreditCard, creditCardUtils } from '@your-package/models';

// Example credit card data
const cardData = {
  CardNumber: "4275000000000000",
  IssuingBank: "Woodgrove Bank",
  PaymentNetwork: "VISA",
  CardHolderName: "JOHN SMITH",
  CardHolderCompanyName: "CONTOSO SOFTWARE",
  ExpirationDate: "01/29",
  CustomerServicePhoneNumbers: ["+1 (987) 123-4567"]
};

// Validate the card
const validatedCard = validateCreditCard(cardData);

// Check expiration
if (creditCardUtils.isExpired(validatedCard.result.ExpirationDate)) {
  console.log("Card is expired");
}

// Get masked number for display
const maskedNumber = creditCardUtils.maskCardNumber(validatedCard.result.CardNumber);
console.log(maskedNumber); // "****-****-****-0000"

// Detect card type
const cardType = creditCardUtils.getCardType(validatedCard.result.CardNumber);
console.log(cardType); // "VISA"

Date Handling

The model handles dates in MM/YY format:

  • Valid format example: “01/29”
  • Months must be 01-12
  • Years are assumed to be 20XX
  • Expiration is calculated using end of month

Type Definitions

You can import the following TypeScript types:

import type {
  CreditCard,
  CreditCardResponse,
  StrictCreditCardResponse
} from '@your-package/models';

Validation Rules

Basic Validation

  • All fields are optional by default
  • CustomerServicePhoneNumbers defaults to an empty array if not specified

Strict Validation

The following fields are required in strict mode:

  • CardNumber
  • CardHolderName
  • IssuingBank
  • PaymentNetwork
  • ExpirationDate

Example Usage

import { validateCreditCard, creditCardUtils } from '@your-package/models';

// Example credit card data
const cardData = {
  CardNumber: "4275000000000000",
  IssuingBank: "Woodgrove Bank",
  PaymentNetwork: "VISA",
  CardHolderName: "JOHN SMITH",
  CardHolderCompanyName: "CONTOSO SOFTWARE",
  ExpirationDate: "01/29",
  CustomerServicePhoneNumbers: ["+1 (987) 123-4567"]
};

// Validate the card
const validatedCard = validateCreditCard(cardData);

// Check expiration
if (creditCardUtils.isExpired(validatedCard.result.ExpirationDate)) {
  console.log("Card is expired");
}

// Get masked number for display
const maskedNumber = creditCardUtils.maskCardNumber(validatedCard.result.CardNumber);
console.log(maskedNumber); // "****-****-****-0000"

Error Handling

Common validation errors:

try {
  const validatedCard = validateCreditCard(cardData);
} catch (error) {
  if (error.errors?.[0]?.path.includes('CardNumber')) {
    // Handle invalid card number
  }
  if (error.errors?.[0]?.path.includes('ExpirationDate')) {
    // Handle invalid expiration date
  }
}