/** * Validates a credit card number using the Luhn (mod10) formula. * @see http://en.wikipedia.org/wiki/Luhn_algorithm * * @param integer credit card number * @param string|array card type, or an array of card types * @return boolean */ public static function credit_card($number, $type = NULL) { // Remove all non-digit characters from the number if (($number = preg_replace('/\\D+/', '', $number)) === '') { return FALSE; } if ($type == NULL) { // Use the default type $type = 'default'; } elseif (is_array($type)) { foreach ($type as $t) { // Test each type for validity if (valid::credit_card($number, $t)) { return TRUE; } } return FALSE; } $cards = Kohana::config('credit_cards'); // Check card type $type = strtolower($type); if (!isset($cards[$type])) { return FALSE; } // Check card number length $length = strlen($number); // Validate the card length by the card type if (!in_array($length, preg_split('/\\D+/', $cards[$type]['length']))) { return FALSE; } // Check card number prefix if (!preg_match('/^' . $cards[$type]['prefix'] . '/', $number)) { return FALSE; } // No Luhn check required if ($cards[$type]['luhn'] == FALSE) { return TRUE; } // Checksum of the card number $checksum = 0; for ($i = $length - 1; $i >= 0; $i -= 2) { // Add up every 2nd digit, starting from the right $checksum += substr($number, $i, 1); } for ($i = $length - 2; $i >= 0; $i -= 2) { // Add up every 2nd digit doubled, starting from the right $double = substr($number, $i, 1) * 2; // Subtract 9 from the double where value is greater than 10 $checksum += $double >= 10 ? $double - 9 : $double; } // If the checksum is a multiple of 10, the number is valid return $checksum % 10 === 0; }
public function pay($method) { $payment = new Payment($method); if ($method == 'Paypal') { Session::instance()->delete('paypal_token'); $order = new Order_Model($this->order->id); //$inv = uniqid('payPal_'); $inv = $this->getTransID(); $order->trans_id = $inv; $order->save(); $attributes = array('AMT' => $this->total(), 'INVNUM' => $inv, 'SHIPTONAME' => $this->order->shipping->first_name . ' ' . $this->order->shipping->last_name, 'SHIPTOSTREET' => $this->order->shipping->address1, 'SHIPTOCITY' => $this->order->shipping->city, 'SHIPTOCOUNTRYCODE' => $this->order->shipping->country, 'SHIPTOSTATE' => $this->order->shipping->state, 'SHIPTOZIP' => $this->order->shipping->zip); //$this->payment->GETDETAILS = FALSE; } else { if (!valid::credit_card($this->order->payment->card->card_num)) { throw new Exception('Invalid credit card data'); } $attributes = array('card_num' => $this->order->payment->card->card_num, 'exp_date' => $this->order->payment->card->exp_date, 'amount' => $this->total(), 'ship_to_first_name' => $this->order->shipping->first_name, 'ship_to_last_name' => $this->order->shipping->last_name, 'ship_to_address' => $this->order->shipping->address1, 'ship_to_city' => $this->order->shipping->city, 'ship_to_state' => $this->order->shipping->state, 'ship_to_zip' => $this->order->shipping->zip, 'ship_to_country' => $this->order->shipping->country, 'first_name' => $this->order->billing->first_name, 'last_name' => $this->order->billing->last_name, 'address' => $this->order->billing->address1, 'city' => $this->order->billing->city, 'state' => $this->order->billing->state, 'zip' => $this->order->billing->zip, 'country' => $this->order->billing->country); } $payment->set_fields($attributes); $this->order->finance_state = 'CHARGING'; if ($payment->process()) { $this->order->finance_state = 'CHARGED'; $this->order->status = 2; //$this->order->save(); return true; } else { $this->order->finance_state = 'DENIED'; $this->order->status = 3; //$this->order->save(); throw new Exception('Payment operation failed (' . $payment->getLastError() . ')'); } }
public function valid_credit_card_test() { $this->assert_true_strict(valid::credit_card('4222222222222'))->assert_true_strict(valid::credit_card('4012888888881881'))->assert_true_strict(valid::credit_card('5105105105105100'))->assert_true_strict(valid::credit_card('6011111111111117'))->assert_false_strict(valid::credit_card('6011111111111117', 'visa')); // invalid visa test nr }
/** * Validates a credit card number using the Luhn (mod10) formula. * @see http://en.wikipedia.org/wiki/Luhn_algorithm * * @param integer credit card number * @param string|array card type, or an array of card types * @return boolean */ public static function credit_card($number, $type = NULL) { // Remove all non-digit characters from the number if (($number = preg_replace('/\\D+/', '', $number)) === '') { return FALSE; } if ($type == NULL) { // Use the default type $type = 'default'; } elseif (is_array($type)) { foreach ($type as $t) { // Test each type for validity if (valid::credit_card($number, $t)) { return TRUE; } } return FALSE; } $cards = array('default' => array('length' => '13,14,15,16,17,18,19', 'prefix' => '', 'luhn' => TRUE), 'american express' => array('length' => '15', 'prefix' => '3[47]', 'luhn' => TRUE), 'diners club' => array('length' => '14,16', 'prefix' => '36|55|30[0-5]', 'luhn' => TRUE), 'discover' => array('length' => '16', 'prefix' => '6(?:5|011)', 'luhn' => TRUE), 'jcb' => array('length' => '15,16', 'prefix' => '3|1800|2131', 'luhn' => TRUE), 'maestro' => array('length' => '16,18', 'prefix' => '50(?:20|38)|6(?:304|759)', 'luhn' => TRUE), 'mastercard' => array('length' => '16', 'prefix' => '5[1-5]', 'luhn' => TRUE), 'visa' => array('length' => '13,16', 'prefix' => '4', 'luhn' => TRUE)); // Check card type $type = strtolower($type); if (!isset($cards[$type])) { return FALSE; } // Check card number length $length = strlen($number); // Validate the card length by the card type if (!in_array($length, preg_split('/\\D+/', $cards[$type]['length']))) { return FALSE; } // Check card number prefix if (!preg_match('/^' . $cards[$type]['prefix'] . '/', $number)) { return FALSE; } // No Luhn check required if ($cards[$type]['luhn'] == FALSE) { return TRUE; } // Checksum of the card number $checksum = 0; for ($i = $length - 1; $i >= 0; $i -= 2) { // Add up every 2nd digit, starting from the right $checksum += $number[$i]; } for ($i = $length - 2; $i >= 0; $i -= 2) { // Add up every 2nd digit doubled, starting from the right $double = $number[$i] * 2; // Subtract 9 from the double where value is greater than 10 $checksum += $double >= 10 ? $double - 9 : $double; } // If the checksum is a multiple of 10, the number is valid return $checksum % 10 === 0; }
/** * Tests the valid::credit_card() function. * @dataProvider credit_card_provider * @group core.helpers.valid.credit_card * @test */ public function credit_card($input_number, $card_type, $expected_result) { $result = valid::credit_card($input_number, $card_type); $this->assertEquals($expected_result, $result); }