P. 1
Kohana Doc

Kohana Doc

|Views: 161|Likes:
Published by jeva_s

More info:

Published by: jeva_s on Jun 15, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/09/2013

pdf

text

original

Overview

The payment library allows you to easily process e­commerce transactions without having to worry
about all the backend details of connecting and setting up the cURL options. It has many features:

1. An extremely easy API (only one method required to process a transaction!)
2. Detailed error reporting
3. Extensible with backend drivers to connect to many different payment gateways
4. Support for credit card gateways as well as PayPal­like gateways

Using the library is very simple. Just set the fields required by your driver, and process().

$payment = new Payment();
$payment->card_num = '1234567890123456';
$payment->exp_date = '0510';

if ($status = $payment->process() === TRUE)
{

// Report successful transaction

}
else
{

// $status has the error message, so display an error page based on it

}

Configuration

In system/config/payment.php there is a sample config for each payment gateway we support.
Simply copy this file to your application directory, and remove the drivers you are not using. The
library can support using more than one driver per application by passing the driver name to the
constructor:

$paypal_payment = new Payment('Paypal');
$authorize_payment = new Payment('Authorize');

After you remove the non­required config lines, modify your driver config as needed. Usually this
will include some sort of API username/password combination, but it differs for each driver.

Attributes

The payment library has a set of default attributes it uses to send payments to the gateway. These
attributes are fairly self explanatory and are listed below.

• card_num
• exp_date
• cvv
• description

• amount
• tax
• shipping
• first_name
• last_name
• company
• address
• city
• state
• zip
• email
• phone
• fax
• ship_to_first_name
• ship_to_last_name
• ship_to_company
• ship_to_address
• ship_to_city
• ship_to_state
• ship_to_zip
Specific drivers may require some or all of these fields. They may also have driver specific fields
that are noted in the Driver section.

Methods

The payment library has a minimal set of methods to use:

set_fields()

This method allows you to set bulk payment attributes with an array.

$payment = new Payment();

$attributes = array
(

'card_num' = '1234567890123456',
'exp_date' = '0510'

};

$payment->set_fields($attributes);

process()

This method does the magic. After you set your attributes, simply call this method in an if test. The
method returns TRUE on successful payment transaction or an error string on failure. It is up to you
how to handle the failure.

Drivers

The payment library uses drivers much like the Database library does in order to keep the API
consistent between payment gateways. It currently supports the following gateways:

1. Authorize.net
2. Trident Gateway
3. TrustCommerce
4. YourPay.net
5. PayPal

Authorize.net

Required Fields

1. card_num
2. exp_date
3. amount

Trident Gateway

Required Fields

1. card_num
2. exp_date
3. amount

TrustCommerce

Required Fields

1. card_num
2. exp_date
3. amount

YourPay.net

Required Fields

1. card_num
2. exp_date
3. amount
4. tax
5. shipping
6. cvv

Driver Specific Details

The amount above is the subtotal. Tax and Shipping get added to the amount to form the grand total
inside the driver.

PayPal

Required Fields

1. amount
2. payerid (after paypal authentication)

Driver Specific Details

Using the PayPal driver is a little different than a normal credit card transaction. It requires two
process() commands to be run. The first one will send the user to PayPal to authenticate, and the
second one will actually run the transaction. Below is an example of this.

class Paypal_Controller extends Controller {

// This will demo a simple paypal transaction. It really only comes down

to two steps.

function __construct()
{

parent::__construct();

$this->paypal = new Payment();

}

// This will set up the transaction and redirect the user to paypal to

login

function index()
{

$this->paypal->amount = 5;
$this->paypal->process();

}

// Once the user logs in, paypal redirects them here (set in the config
file), which processes the payment
function return_test()
{

$this->paypal->amount = 5;
$this->paypal->payerid = $this->input->get('payerid');
echo ($this->paypal->process()) ? "WORKED" : "FAILED";

}

// This is the cancel URL (set from the config file)
function cancel_test()
{

echo 'cancelled';

}

// Just some utility functions.
function reset_session()
{

$this->session->destroy();
url::redirect('paypal/index');

}

function session_status()
{

echo '

'.print_r($this->session->get(), true);

}

}

Writing Your Own Driver

There are many more payment gateways than there are drivers provided by Kohana. We encourage
you to write your own when you encounter a gateway not supported by the library. An easy way to
do it is like so:

1. Add a new entry in config/payment.php with your driver details. Use the previous entries as

an example.
2. Copy the Trident Gateway driver and rename it to *Gateway Name*.php
3. Alter the required fields array as instructed in the gateway's API manual (You have this,

right? ;)

4. Modify the fields array to include all the available relevant fields for the gateway
5. Modify the constructor to set default values from the config file (API username/password for

example)

6. Modify the set_fields() method to do variable translation (for example if your gateway names
cc_num something different. Look in your API manual for details.)
7. Modify the $post_url ternary statement to include the correct test and live mode API URLs
8. Modify how the return statement handles success and error based on what the specific
gateway status message is (Look in you API manual)

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->