Last active
January 19, 2017 16:22
-
-
Save yokada/0d2f19b3829404b339401bc85adc2689 to your computer and use it in GitHub Desktop.
payment cards php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyPayment | |
{ | |
protected static cards_config = [ | |
[ | |
'type' => 'visaelectron', | |
'patterns' => [4026, 417500, 4405, 4508, 4844, 4913, 4917], | |
'format' => '/(\d{1,4})/', | |
'length' => [16], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => false, | |
], | |
[ | |
'type' => 'maestro', | |
'patterns' => [5018, 502, 503, 506, 56, 58, 639, 6220, 67], | |
'format' => '/(\d{1,4})/', | |
'length' => [12, 13, 14, 15, 16, 17, 18, 19], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => false, | |
], | |
[ | |
'type' => 'forbrugsforeningen', | |
'patterns' => [600], | |
'format' => '/(\d{1,4})/', | |
'length' => [16], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => false, | |
], | |
[ | |
'type' => 'dankort', | |
'patterns' => [5019], | |
'format' => '/(\d{1,4})/', | |
'length' => [16], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => false, | |
], | |
[ | |
'type' => 'elo', | |
'patterns' => [4011, 4312, 4389, 4514, 4573, 4576, 5041, 5066, 5067, 509, 6277, 6362, 6363, 650, 6516, 6550], | |
'format' => '/(\d{1,4})/', | |
'length' => [16], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => false, | |
], | |
[ | |
'type' => 'visa', | |
'patterns' => [4], | |
'format' => '/(\d{1,4})/', | |
'length' => [13, 16], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => true, // 本人認証可能か? | |
], | |
[ | |
'type' => 'mastercard', | |
'patterns' => [51, 52, 53, 54, 55, 22, 23, 24, 25, 26, 27], | |
'format' => '/(\d{1,4})/', | |
'length' => [16], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => true, // 本人認証可能か? | |
], | |
[ | |
'type' => 'amex', | |
'patterns' => [34, 37], | |
'format' => '/(\d{1,4})(\d{1,6})?(\d{1,5})?/', | |
'length' => [15], | |
'cvcLength' => [3, 4], | |
'luhn' => true, | |
'auth' => false, | |
], | |
[ | |
'type' => 'dinersclub', | |
'patterns' => [30, 36, 38, 39], | |
'format' => '/(\d{1,4})(\d{1,6})?(\d{1,4})?/', | |
'length' => [14], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => false, | |
], | |
[ | |
'type' => 'discover', | |
'patterns' => [60, 64, 65, 622], | |
'format' => '/(\d{1,4})/', | |
'length' => [16], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => false, | |
], | |
[ | |
'type' => 'unionpay', | |
'patterns' => [62, 88], | |
'format' => '/(\d{1,4})/', | |
'length' => [16, 17, 18, 19], | |
'cvcLength' => [3], | |
'luhn' => false, | |
'auth' => false, | |
], | |
[ | |
'type' => 'jcb', | |
'patterns' => [35], | |
'format' => '/(\d{1,4})/', | |
'length' => [16], | |
'cvcLength' => [3], | |
'luhn' => true, | |
'auth' => true, | |
] | |
]; | |
public static function card_from_num($num) | |
{ | |
$config = self::$cards_config; | |
$card; | |
$p; | |
$pattern; | |
$_i; | |
$_j; | |
$_len; | |
$_len1; | |
$_ref; | |
$num = preg_replace('#\D#', '', $num); | |
for ($_i = 0, $_len = count($config); $_i < $_len; $_i++ ) { | |
$card = $config[$_i]; | |
$_ref = $card['patterns']; | |
for ($_j = 0, $_len1 = count($_ref); $_j < $_len1; $_j++) { | |
$pattern = $_ref[$_j]; | |
$p = strval($pattern); | |
if (substr($num, 0, strlen($p)) === $p) { | |
return $card; | |
} | |
} | |
} | |
} | |
public static function card_luhn_check($num) | |
{ | |
$digit; | |
$digits; | |
$odd; | |
$sum; | |
$_i; | |
$_len; | |
$odd = true; | |
$sum = 0; | |
$num = preg_replace('#\D#', '', $num); | |
$digits = strrev(strval($num)); | |
//$digits = (num + '').split('').reverse(); | |
for ($_i = 0, $_len = strlen($digits); $_i < $_len; $_i++) { | |
$digit = $digits[$_i]; | |
$digit = intval($digit); | |
if (($odd = !$odd)) { | |
$digit *= 2; | |
} | |
if ($digit > 9) { | |
$digit -= 9; | |
} | |
$sum += $digit; | |
} | |
return $sum % 10 === 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
php > include 'MyPayment.php';
php > var_dump( MyPayment::card_from_num(4111111111111111) );
array(7) {
["type"]=>
string(4) "visa"
["patterns"]=>
array(1) {
[0]=>
int(4)
}
["format"]=>
string(11) "/(\d{1,4})/"
["length"]=>
array(2) {
[0]=>
int(13)
[1]=>
int(16)
}
["cvcLength"]=>
array(1) {
[0]=>
int(3)
}
["luhn"]=>
bool(true)
["auth"]=>
bool(true)
}