Kohana comes with a payment module with various payment drivers. Here is a payment driver for credit card pre authorisation…
config/payment.php
$config['Creditcard'] = array ( 'driver' => 'Creditcard', );
and the payment driver…
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Creditcard Payment Driver
*
*
* @author Sakib Farid
*/
class Payment_Creditcard_Driver implements Payment_Driver
{
// Fields required to do a transaction
private $required_fields = array
(
'card_num' => false,
//format mmyy
'exp_date' => false,
);
private $fields = array(
'card_num'=>'',
//format mmyy
'exp_date'=>'',
'cvv'=>'',
//format mmyy
'start_date',
'issue_num',
);
private $cc_expiry_month;
private $cc_expiry_year;
/**
* Sets the config for the class.
*
* @param array config passed from the library
*/
public function __construct($config)
{
Kohana::log('debug', 'Creditcard Payment Driver Initialized');
}
public function set_fields($fields)
{
foreach ((array) $fields as $key => $value)
{
$this->fields[$key] = $value;
if (array_key_exists($key, $this->required_fields) and !empty($value)) $this->required_fields[$key] = true;
}
}
public function process()
{
// Check for required fields
if (in_array(false, $this->required_fields))
{
$fields = array();
foreach ($this->required_fields as $key => $field)
{
if (!$field) $fields[] = $key;
}
return 'required_fields';
}
$this->fields['card_num'] = ereg_replace('[^0-9]', '', $this->fields['card_num']);
if (ereg('^4[0-9]{12}([0-9]{3})?$', $this->fields['card_num'])) {
$_SESSION['cc_type'] = 'Visa';
} elseif (ereg('^5[1-5][0-9]{14}$', $this->fields['card_num'])) {
$_SESSION['cc_type'] = 'Master Card';
//} elseif (ereg('^3[47][0-9]{13}$', $this->card_num)) {
// $_SESSION['cc_type'] = 'American Express';
} elseif (ereg('^3(0[0-5]|[68][0-9])[0-9]{11}$', $this->fields['card_num'])) {
$_SESSION['cc_type'] = 'Diners Club';
} elseif (ereg('^6011[0-9]{12}$', $this->fields['card_num'])) {
$_SESSION['cc_type'] = 'Discover';
} elseif (ereg('^(3[0-9]{4}|2131|1800)[0-9]{11}$', $this->fields['card_num'])) {
$_SESSION['cc_type'] = 'JCB';
} elseif (ereg('^5610[0-9]{12}$', $this->fields['card_num'])) {
$_SESSION['cc_type'] = 'Australian BankCard';
} elseif (ereg('((^(4903|4905|4911|4936|6333|6759)([0-9]{12}|[0-9]{14}|[0-9]{15}))|(^(564182|633110)([0-9]{10}|[0-9]{12}|[0-9]{13})))$', $this->fields['card_num'])) {
$_SESSION['cc_type'] = 'Switch Debit Card';
} else {
//unknown card number
return -1;
}
$expiry_m = substr($this->fields['exp_date'], 0, 2);
if (is_numeric($expiry_m) && ($expiry_m > 0) && ($expiry_m < 13)) {
$this->cc_expiry_month = $expiry_m;
} else {
//expiry month is invalid
return -2;
}
$current_year = date('Y');
$expiry_y = substr($this->fields['exp_date'], 2);
$expiry_y = substr($current_year, 0, 2) . $expiry_y;
if (is_numeric($expiry_y) && ($expiry_y >= $current_year) && ($expiry_y <= ($current_year + 10))) {
$this->cc_expiry_year = $expiry_y;
} else {
//invalid expiry year
return -3;
}
if ($expiry_y == $current_year) {
if ($expiry_m < date('n')) {
//invalid expiry date, not in future
return -4;
}
}
$cardNumber = strrev($this->fields['card_num']);
$numSum = 0;
for ($i=0; $i<strlen($cardNumber); $i++) {
$currentNum = substr($cardNumber, $i, 1);
// Double every second digit
if ($i % 2 == 1) {
$currentNum *= 2;
}
// Add digits of 2-digit numbers together
if ($currentNum > 9) {
$firstNum = $currentNum % 10;
$secondNum = ($currentNum - $firstNum) / 10;
$currentNum = $firstNum + $secondNum;
}
$numSum += $currentNum;
}
// If the total has no remainder it's OK
if ($numSum % 10 == 0) {
return true;
} else {
//hasnt passed pre-authorisation
return -5;
}
}
}
You must ofcourse create any forms you require.
Visit the Kohana website for more information on the payment module.
Tags: credit card, Kohana, payment driver, payment module, php, PHP Framework, pre-authorisation