Pixel Envision Ltd.
Indie Game Development Studio
  • facebook
  • twitter
  • Home
  • About Us
    • Privacy Policy
    • Press Kit
  • Our Games
    • Casual Games
      • Hordes of Enemies
      • noded
      • kubic
      • Whip Swing
      • Witches’ Brew
    • Kids Apps
      • Cars & Trucks Puzzle
      • Train Puzzles for Kids
      • Animal Puzzle
      • Fairy Tale Puzzles for Kids
      • Coloring Book
      • Find the Differences
  • Blog
  • Support
Select Page ...

Blog

PHP Credit Card Validation class using Mod 10 (Luhn) + more!

February 17, 2012 PHP, Programming

Here is another PHP code snippet from my library. This PHP class allows you to validate credit card information before processing it further. Doing all possible off-line checks before sending it to he credit card gateway will help you save time & money…

If the card is valid it will return cleaned & formatted card number along with the card type (Visa, Mastercard, Amex, Dinners, Discover, JCB & Maestro).

Besides regular checks, validates the card number against Mod 10 (Luhn) as well as know test card numbers…

Features

  • Checks if the card number, expiration date & validation numbers are valid.
  • Checks if the card is expired
  • Finds & returns the card type.
  • Checks against the known test card numbers.
  • Validates the card number using Mod 10 algorithm.

Usage Sample

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
require('mod10.php');
$CARDTYPE=$ErrArr=NULL;
 
$cc = new CCVal($_POST["card_number"], $_POST["card_expire_month"], $_POST["card_expire_year"], $_POST["card_cvv2"]);
 
$cstatus=$cc->IsValid();
 
if($cstatus[0]=="valid") {
$CARDTYPE=$cstatus[1];
$_POST["card_number"]=$cstatus[2];
$_POST["card_cvv2"]=$cstatus[3];
}else {
$ErrArr=$cstatus;
}
 
?>

Download

  • Download PHP code snippet *UPDATED FOR AMEX CVV FIX
  • Code is PHP5 compatible but it should also work on PHP4 (Untested)

License

This code is free to use, distribute, modify and study. When referencing please link back to this website / post in any way e.g. direct link, credits etc. If you find this useful, please leave a comment and share using the buttons below!

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class CCVal
{
var $__ccType = '';
var $__ccNum = '';
var $__ccExpM = 0;
var $__ccExpY = 0;
var $__ccCVV = '';
 
function CCVal($num, $expm, $expy, $cvv)
{
 
if(!empty($num))
{
  $cardNumber = ereg_replace("[^0-9]", "", $num);
  if(!empty($cardNumber))
  {
$this->__ccNum = $cardNumber;
  }
}
 
if(!empty($cvv))
{
  $cardCVV = ereg_replace("[^0-9]", "", $cvv);
  if(!empty($cardCVV))
  {
$this->__ccCVV = $cardCVV;
  }
}
 
if(!is_numeric($expm) || $expm < 1 || $expm > 12)
{
  $this->__ccExpM =NULL;
}
else
{
  $this->__ccExpM = $expm;
}
 
$currentYear = date('Y');
settype($currentYear, 'integer');
$expy=$expy+2000;
if(!is_numeric($expy) || $expy < $currentYear || $expy > $currentYear + 15)
{
  $this->__ccExpY = NULL;
}
else
{
  $this->__ccExpY = $expy;
}
 
if (ereg("^5[1-5][0-9]{14}$", $this->__ccNum)) {
$this->__ccType = MASTERCARD;}
elseif (ereg("^4[0-9]{12}([0-9]{3})?$", $this->__ccNum)) {
$this->__ccType = VISA;}
elseif (ereg("^3[47][0-9]{13}$", $this->__ccNum)) {
$this->__ccType = AMEX;}
elseif (ereg("^3(0[0-5]|[68][0-9])[0-9]{11}$", $this->__ccNum)) {
$this->__ccType = DINNERS;}
elseif (ereg("^6011[0-9]{12}$", $this->__ccNum)) {
$this->__ccType = DISCOVER;}
elseif (ereg("^(3[0-9]{4}|2131|1800)[0-9]{11}$", $this->__ccNum)) {
$this->__ccType = JCB;}
elseif (ereg("^(5[06-8]|6)[0-9]{10,17}$", $this->__ccNum)) {
$this->__ccType = MAESTRO;}
else {
$this->__ccType = UNKNOWN;}
}
 
function IsValid()
{
 
  $validFormat = false;
  $validExp = false;
  $passCheck = false;
  $validCVV = false;
  $testCard = false;
 
switch($this->__ccType)
{
  case MASTERCARD:
    $validFormat = true;
    break;
case VISA:
    $validFormat = true;
    break;
case AMEX:
    $validFormat = true;
    break;
case DISCOVER:
    $validFormat = true;
    break;
case DINNERS:
    $validFormat = true;
    break;
case JCB:
    $validFormat = true;
    break;
case MAESTRO:
    $validFormat = true;
    break;
  default:
 
  $validFormat = false;
}
 
$cardExpM = $this->__ccExpM;
$cardExpY = $this->__ccExpY;
 
if($cardExpM && $cardExpY && date('Ym')<=$cardExpY.$cardExpM) {
$validExp = true;
}
 
$cardCVV = $this->__ccCVV;
 
if($cardCVV && (strlen($cardCVV)==3 && $this->__ccType!=AMEX)) {
$validCVV = true;
} elseif($cardCVV && (strlen($cardCVV)==4 && $this->__ccType==AMEX)) {
$validCVV = true;
}
 
$cardNumber = strrev($this->__ccNum);
$numSum = 0;
 
for($i = 0; $i < strlen($cardNumber); $i++)
{
  $currentNum = substr($cardNumber, $i, 1);
 
if($i % 2 == 1)
{
  $currentNum *= 2;
}
 
if($currentNum > 9)
{
  $firstNum = $currentNum % 10;
  $secondNum = ($currentNum - $firstNum) / 10;
  $currentNum = $firstNum + $secondNum;
}
 
$numSum += $currentNum;
}
 
$passCheck = ($numSum % 10 == 0);
 
$testCard = in_array($this->__ccNum,array('340000000000009','341111111111111','343434343434343','346827630435344','370000000000002','370000200000000','370407269909809','370556019309221','371449635398431','374200000000004','376462280921451','377752749896404','378282246310005','378734493671000','30000000000004','30569309025904','5019717010103742','30204169322643','30218047196557','30221511563252','36000000000008','36148900647913','36700102000000','38000000000006','38520000023237','6011000000000004','6011000000000012','6011000400000000','6011000990139424','6011111111111117','6011153216371980','6011601160116611','6011687482564166','6011814836905651','201400000000009','201481123699422','214925980592653','214983972181233','180001638277392','180040153546898','180058601526635','3528000700000000','3528723740022896','3530111333300000','3566002020360505','3569990000000009','630495060000000000','6304900017740292441','6333333333333333336','5100080000000000','5105105105105100','5111111111111118','5123619745395853','5138495125550554','5274576394259961','5301745529138831','5311531286000465','5364587011785834','5404000000000001','5424000000000015','5431111111111111','5454545454545454','5459886265631843','5460506048039935','5500000000000004','5500939178004613','5555555555554444','5565552064481449','5597507644910558','6334580500000000','6334900000000005','633473060000000000','6767622222222222222','6767676767676767671','5641820000000005','6331101999990016','6759649826438453','4007000000027','4012888818888','4024007127653','4222222222222','4556069275201','4556381812806','4911830000000','4916183935082','4916603452528','4929000000006','4005550000000019','4012888888881881','4111111111111111','4444333322221111','4539105011539664','4544182174537267','4716914706534228','4916541713757159','4916615639346972','4917610000000000','4406080400000000','4462000000000003','4462030000000000','4917300000000008','4917300800000000','4484070000000000','4485680502719433'));
 
  if($validFormat && $validExp && $validCVV && $passCheck && !$testCard) {
  return array("valid",$this->__ccType,$this->__ccNum,$this->__ccCVV);
  } else {
  $ERR=NULL;
  if(!$validFormat || !$passCheck) $ERR[]="Invalid card number";
  if(!$validCVV) $ERR[]="Invalid CVV2/CVC2 Number";
  if(!$validExp) $ERR[]="Credit card has been expired";  
  if($testCard) $ERR[]="Reserved card number";
  return $ERR;
  }
}
}
?>

← We have stopped developing with the Corona SDK
Corona Project Manager license for sale! →

5 Responses to PHP Credit Card Validation class using Mod 10 (Luhn) + more!

  • Aina
    11 / 29 / 2015

    hye, could you help me with these error?
    if (preg_match(‘/^5[1-5][0-9]{14}$/’, $this->__ccNum)) {
    but THIS line, and the next 3 elseif’s do not pass:
    elseif (preg_match(“/^3[47][0-9]{13}$/”, $this->__ccNum)) {

    Aina 11 / 29 / 2015
    Reply
    • Erdener Gonenc
      12 / 2 / 2015

      Hard to tell but from the sample it might be syntax error. I would check quotation marks & closing brackets. I hope this helps.

      Erdener Gonenc 12 / 2 / 2015
  • ervasti
    12 / 29 / 2013

    Hi! never mind – I fixed it. Thanks a bunch!~ =]

    ervasti 12 / 29 / 2013
    Reply
    • Erdener
      12 / 30 / 2013

      Ah, ok. You’re welcome… :)

      Erdener 12 / 30 / 2013
  • ervasti
    12 / 29 / 2013

    Hi! ereg is throwing deprecation errors. I’ve tried for 3 hours to edit your code using preg_match and it is say “unexpected ‘^’ on L74.” Here is an example:

    THIS line seems to pass the error test:
    if (preg_match(‘/^5[1-5][0-9]{14}$/’, $this->__ccNum)) {
    but THIS line, and the next 3 elseif’s do not pass:
    elseif (preg_match(“/^3[47][0-9]{13}$/”, $this->__ccNum)) {

    Any thoughts? Thanks!

    ervasti 12 / 29 / 2013
    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Tweets by PixelEnvision

  • Recent Comments

    • Yogesh Singh on ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries
    • Admin on Maxscript – Vray Cubemap Generator for Unity
    • charlie on Maxscript – Vray Cubemap Generator for Unity
    • Mastan on PHP Currency Converter
    • Rakesh Vishnoi on ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries
  • Tags

    Coming Soon Featured PHP Programming Reviews Tips & Tricks Unity 3D Windows Phone
    • Find us on

      amazonandroidapplefacebooklinkedinrsstwitterwindowsyoutube
    • Company Information

      Pixel Envision Limited is a company registered in England, company number: 09558675. Registered Office: Palmeira Avenue Mansions, 19 Church Road, Hove, East Sussex, BN3 2FA, UK

    • Press Kit
    • Privacy Policy
    Copyright © 2011-2018 Pixel Envision Ltd, all rights reserved.
    We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkNoPrivacy policy
    Revoke cookies