What is the best way to validate a credit card in PHP?
There are three parts to the validation of the card number:
- PATTERN - does it match an issuers pattern (e.g. VISA/Mastercard/etc.)
- CHECKSUM - does it actually check-sum (e.g. not just 13 random numbers after "34" to make it an AMEX card number)
- REALLY EXISTS - does it actually have an associated account (you are unlikely to get this without a merchant account)
Pattern
- MASTERCARD Prefix=51-55, Length=16 (Mod10 checksummed)
- VISA Prefix=4, Length=13 or 16 (Mod10)
- AMEX Prefix=34 or 37, Length=15 (Mod10)
- Diners Club/Carte Prefix=300-305, 36 or 38, Length=14 (Mod10)
- Discover Prefix=6011,622126-622925,644-649,65, Length=16, (Mod10)
- etc. (detailed list of prefixes)
Checksum
Most cards use the Luhn algorithm for checksums:
Luhn Algorithm described on Wikipedia
There are links to many implementations on the Wikipedia link, including PHP:
<?
/* Luhn algorithm number checker - (c) 2005-2008 shaman - www.planzero.org *
* This code has been released into the public domain, however please *
* give credit to the original author where possible. */
function luhn_check($number) {
// Strip any non-digits (useful for credit card numbers with spaces and hyphens)
$number=preg_replace('/\D/', '', $number);
// Set the string length and parity
$number_length=strlen($number);
$parity=$number_length % 2;
// Loop through each digit and do the maths
$total=0;
for ($i=0; $i<$number_length; $i++) {
$digit=$number[$i];
// Multiply alternate digits by two
if ($i % 2 == $parity) {
$digit*=2;
// If the sum is two digits, add them together (in effect)
if ($digit > 9) {
$digit-=9;
}
}
// Total up the digits
$total+=$digit;
}
// If the total mod 10 equals 0, the number is valid
return ($total % 10 == 0) ? TRUE : FALSE;
}
?>
I need to validate credit-card details
Magento comes with Really Easy Field Validation in place so you only need to add some classes to the fields in question.
validate-cc-type
orvalidate-cc-type-select
validate-cc-number
validate-cc-cvn
validate-cc-exp
However the normal payment modules should already be using these so what are you doing that breaks it or otherwise needs CC details?
What is the best way to validate a credit card in codeigniter
As people already told you, CodeIgniter is a php framework, coded using php, works in a php environment and makes use of..,php classes and functions :).
What's more, the file you linked to is a simple function. One function. You know what you can do? Take the file as it is, name it creditcard_helper.php
, put it inside the helpers folder, open it and place the whole code inside this snippet (ugly but necessary, as whenever you'll load the helper a second time it would give you error otherwise):
if(!function_exists('checkCreditCard')
{
//the whole content goes here untouched;
}
And you're set. Just use:
$this->load->helper('creditcard');
if(checkCreditCard($cardnumber, $cardname, &$errornumber, &$errortext))
{
echo 'card OK';
}
else
{
echo 'wrong card type/number';
}
how to validate credit card number and Card Code Verification (ccv)
Right, so after some initial fail on understanding what was being asked for, I think the following should help.
http://community.developer.authorize.net/t5/The-Authorize-Net-Developer-Blog/Validating-Credit-Card-Information-Part-3-of-3-CVV-Numbers/ba-p/7657
The post provides some code that validates a CVV number. It obviously can't tell you if it's correct or not though.
Hopefully that helps.
Credit Card Validation using PHP
You want to look at the Luhn algorithm: http://en.wikipedia.org/wiki/Luhn_algorithm
This is the algorithm that basically looks at a credit card number and decides if it's a valid number. This doesn't mean that it's an actual, real credit card. You would need a credit card processor/gateway for that (see: Authorize.NET, Paypal, etc).
This algorithm has been implemented in PHP many times, and I'm sure with a simple Google search you can find some code in PHP that representes the Luhn algorithm.
Good luck.
Validating credit card details in Codeigniter
In CodeIgniter, you can add custom validators. Symfony2 offers a bunch of validators (including credit card details) and you can just use those classes in CI.
For example, the 'card scheme' validator - documentation and code.
how to validate credit card info(billing, card number etc) through authorize.net?
No. The only way to know a credit card is valid is to process a transaction. Only then is the bank contacted to validate the credit card.
FYI, I am the author of the article you linked to.
Related Topics
Utf-8 Safe Equivalent of Ord or Charcodeat() in PHP
Create New Variables from Array Keys in PHP
In PHP, Is There a Short Way to Compare a Variable to Multiple Values
How to Schedule the Execution of a PHP Script on the Server Side
Calculate the Time When the Sun Is X Degrees Below/Above the Horizon
Phpmyadmin - Count(): Parameter Must Be an Array or an Object That Implements Countable
MVC (Laravel) Where to Add Logic
"Connection for Controluser as Defined in Your Configuration Failed" with PHPmyadmin in Xampp
Is It Necessary to Declare PHP Array Before Adding Values with []
Why Are "Echo" Short Tags Permanently Enabled as of PHP 5.4
PHP Send E-Mail with Attachment
Automatic Image Format Detection in PHP
Composer Installing: the JSON Extension Is Missing
How to Send Email from Localhost Wamp Server to Send Email Gmail Hotmail or So Forth