Skip to content

Tap-Payments/TapCardValidator-Android

Repository files navigation

TapCardValidator

A robust Android library for validating payment card information in real-time with support for multiple card types and comprehensive error handling.

Features

  • ✅ Real-time card number validation
  • ✅ Card type detection (Visa, Mastercard, American Express, etc.)
  • ✅ Expiry date validation
  • ✅ CVV/CVC validation
  • ✅ Cardholder name validation
  • ✅ Lightweight and efficient
  • ✅ Easy-to-integrate API

Installation

Gradle

Add the dependency to your module's build.gradle:

dependencies {
    implementation 'com.tap:cardvalidator:1.0.0'
}

Maven

<dependency>
    <groupId>com.tap</groupId>
    <artifactId>cardvalidator</artifactId>
    <version>1.0.0</version>
</dependency>

Integration Guide

Step 1: Initialize the Validator

import com.tap.cardvalidator.CardValidator

val validator = CardValidator()

Step 2: Validate Card Number

val cardNumber = "4532015112830366"
val isValid = validator.validateCardNumber(cardNumber)

if (isValid) {
    val cardType = validator.detectCardType(cardNumber)
    println("Card Type: $cardType")
} else {
    println("Invalid card number")
}

Step 3: Validate Expiry Date

val month = 12
val year = 25

val isExpiryValid = validator.validateExpiryDate(month, year)
if (isExpiryValid) {
    println("Expiry date is valid")
} else {
    println("Card has expired or invalid format")
}

Step 4: Validate CVV

val cvv = "123"
val cardType = "VISA"

val isCVVValid = validator.validateCVV(cvv, cardType)
if (isCVVValid) {
    println("CVV is valid")
} else {
    println("Invalid CVV")
}

Step 5: Validate Cardholder Name

val cardholderName = "John Doe"

val isNameValid = validator.validateCardholderName(cardholderName)
if (isNameValid) {
    println("Cardholder name is valid")
} else {
    println("Invalid cardholder name")
}

Complete Integration Example

import com.tap.cardvalidator.CardValidator

class PaymentActivity : AppCompatActivity() {
    
    private val validator = CardValidator()
    
    fun processPayment(
        cardNumber: String,
        expiryMonth: Int,
        expiryYear: Int,
        cvv: String,
        cardholderName: String
    ): Boolean {
        // Validate all fields
        if (!validator.validateCardNumber(cardNumber)) {
            showError("Invalid card number")
            return false
        }
        
        if (!validator.validateExpiryDate(expiryMonth, expiryYear)) {
            showError("Invalid expiry date")
            return false
        }
        
        val cardType = validator.detectCardType(cardNumber)
        if (!validator.validateCVV(cvv, cardType)) {
            showError("Invalid CVV")
            return false
        }
        
        if (!validator.validateCardholderName(cardholderName)) {
            showError("Invalid cardholder name")
            return false
        }
        
        // All validations passed
        submitPayment(cardNumber, expiryMonth, expiryYear, cvv, cardholderName)
        return true
    }
    
    private fun showError(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
    
    private fun submitPayment(vararg args: Any) {
        // Process payment
    }
}

Supported Card Types

  • Visa
  • Mastercard
  • American Express
  • Discover
  • Diners Club
  • JCB
  • Maestro

API Reference

CardValidator

Method Parameters Returns Description
validateCardNumber() cardNumber: String Boolean Validates card number using Luhn algorithm
detectCardType() cardNumber: String String Detects and returns card type
validateExpiryDate() month: Int, year: Int Boolean Validates expiry date
validateCVV() cvv: String, cardType: String Boolean Validates CVV based on card type
validateCardholderName() name: String Boolean Validates cardholder name

Best Practices

  1. Always validate on the client side - Provides immediate user feedback
  2. Never store card details - Use tokenization for production
  3. Validate expiry before processing - Check for expired cards
  4. Use appropriate card type validation - CVV length differs by card type
  5. Handle validation errors gracefully - Show clear error messages to users
  6. Test with multiple card numbers - Use test cards for each type

Testing

Use these test card numbers for development:

Visa:              45xxxxxxxx2830366
Mastercard:        54xxxxxxxxxxxx0000
American Express:  37xxxxxxxxxxxxxx126
Discover:          6xxxxxxxxxxxx7

Troubleshooting

Issue: Card validation always fails

  • Solution: Ensure card number is stripped of spaces and hyphens
  • Solution: Verify the card number length matches the card type

Issue: CVV validation fails for American Express

  • Solution: AmEx CVV is 4 digits, not 3
  • Solution: Use the correct card type when validating

Issue: Expiry date validation not working

  • Solution: Ensure month is 1-12 and year is valid
  • Solution: Use 2-digit year format (e.g., 25 for 2025)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues, questions, or contributions, please visit our GitHub repository.

About

Card Validation library for Android

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages