Archive for the ‘Programming’ Category

Kohana Credit Card Pre-Authorisation Payment Driver

August 31, 2009

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.

400 Bad Request After SSL Install on Ubuntu 6.06 Dapper Drake Using Mod SSL / Open SSL

June 28, 2008

If you have a 400 bad request when installing SSL on Ubuntu Dapper Drake (virtual host configuration) then maybe the following may help you.

Check your virtual host apache file located at
/etc/apache2/sites-enabled/your-virtualhost-file

If it has the following syntax it may not work as you intended…

<VirtualHost your.site.ip.address:80 your.site.ip.address:443>
any other configuration in here
SSLCertificateFile path-to-your-cert-file.cert
SSLCertificateKeyFile path-to-your-cert-keyfile.key
</VirtualHost>

The above syntax may give you a 400 bad request and only lets you access your website via the SSL port 443 and not the normal http port 80. You will porbably see the following error when accessing your website.

Your browser sent a request that this server could not understand.
Reason: You’re speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.

Obviously you want requests to both ports to work. Try the following syntax to correct this problem.

<VirtualHost your.site.ip.address:80>
any other configuration in here
</VirtualHost>

<VirtualHost your.site.ip.address:443>
any other configuration in here
SSLCertificateFile path-to-your-cert-file.cert
SSLCertificateKeyFile path-to-your-cert-keyfile.key
</VirtualHost>

Hopefully, with abit of luck, this should correct your problem.

Apache ReWrite Module Setup on Ubuntu 6.06 Dapper Drake

June 27, 2008

To setup the Apache ReWrite Module on Ubuntu 6.06 Dapper Drake (virtual host setup) all you need to do is create a symbolic link for the rewrite.load in /etc/apache2/mods-enabled/

sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/

Restart Apache webserver

sudo /etc/init.d/apache2 restart

Now in your .htaccess do

RewriteEngine On
RewriteRule.....

Thats all. It should work.
If its still not working for you then open /etc/apache2/sites-enabled/your-virtualhost-conf-file
and add in between your <Directory> tag

AllowOverride All

Restart Apache and thats it, your all done.

Lookahead and Lookbehind Zero Width Assertions

June 3, 2008

LOOKAHEAD

When using regular expressions, sometimes you may want to match a string NOT followed by some particular word (negative lookahead) or followed by some particular word (positive lookahead) . The construct to do this is

search string (?!some word)
search string (?=some word)

The first example will match `search string` not followed by `some word`, this is a negative lookahead. The second example will match `search string` followed by `some word` which is a poitive lookahead. When using lookaheads we can use any regular expression within the brackets and even capture the match e.g.

search string (?=(regex))

In the above example we can match `search string` followed by any regular expression and also capture the regular expression.

LOOKBEHIND

Lookbehind on the other hand works in a slightly different way. You cannot just apply any regular expression for a lookbehind assertion, it has to be fixed width. The construct for lookbehind is

(?<!some word )search string
(?<=some word )search string

The first example is a negative lookbehind which allows you to match `search string` not preceded by `some word `. The second will match `search string` which is preceded by `some word `.

Lookbehinds do not allow you to have just any regular expressions inside the lookbehind. You can only use a regular expression where the length of the match is predetermined, is a fixed width. This applies to perl, python and php. Java on the other hand may allow some extra options such as the ?

All in all the lookarounds in regular expressions are a powerful and indespinsible tool when matching strings.