状态 草稿
Todo Needs updating from library to module
官方最后更新时间 2009/02/08 14:58

Payment 扩展

概述

支付模块可以让用户很容易的处理电子商务交易,而不需要担心背后的细节。它有很多特性:

  1. 一个及其简单的 API 接口(处理一个交易只需要一个方法!)
  2. 详细的错误报告
  3. 可以通过扩展后端驱动来连接到各种不同的支付网关
  4. 支持类似于 PayPal 的信用卡支付网关

加载 payment 扩展

设置 application/config/config.php 文件中的'modules'选项

$config['modules'] => array
(
    'modules/auth',
    'modules/payment'
)

如果需要直接调用,可以在代码中这样实例化一个该扩展:

$payment = new Payment();

使用示例

使用非常简单,下面的代码演示了如何使用 payment 扩展,只需要设置驱动需要的参数,然后调用process().

$payment = new Payment();
$payment->card_num = '1234567890123456';
$payment->exp_date = '0510';
 
if ($status = $payment->process() === TRUE)
{
     // 通过
}
else
{
    // $status 包含错误信息,所以可以在错误页面显示它
}

配置

system/config/payment.php 中有我们支持的每一个支付网关的简单配置。只需要将这个文件拷贝到你的 application 文件夹中,然后删除你不需要的驱动即可。这个扩展支持一个 application 中使用多个驱动,只许在构造器中传入不同的名字。

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

删除不需要的配置之后,按自己的需要修改自己的驱动配置。通常这些配置包含各种类型的 username/password 的组合,而且每个驱动都不一样。

属性

Payment 扩展包含一系列默认的属性。这些属性都还比较简单易懂,在下面列出:

  • 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

特定的驱动可能需要这些属性的一部分或者全部。某些驱动也可能需要自己特定的属性,一般可以在驱动的说明里看到。

方法

Payment 扩展有几个方法可以使用:

set_fields()

这个方法允许你使用一个array来指定支付的属性。

$payment = new Payment();
 
$attributes = array
(
    'card_num' = '1234567890123456',
    'exp_date' = '0510'
};
 
$payment->set_fields($attributes);

process()

这个方法完成支付。设置好属性之后,只要在一个if语句中调用这个方法,检查返回值是否是 TRUE,即可知道支付交易是否成功,如果失败,则会返回一个错误描述的字符串,该字符串可以告诉你如何处理这个错误。

驱动

Payment 扩展的驱动库接口很像数据库 API,这样可以在不同的支付网关之间保持 API 的一致性。它目前支持以下几种支付网关:

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

Authorize.net

需要的参数

  1. card_num
  2. exp_date
  3. amount

注意

如果你有 Authorize.net 网站开发者的测试账户请在配置中开启 test_mode。如果你在测试模式下有一个正规账户(在你的 Authorize.net 页面设置)那么请关闭 test_mode。

Trident Gateway

需要的参数

  1. card_num
  2. exp_date
  3. amount

TrustCommerce

需要的参数

  1. card_num
  2. exp_date
  3. amount

YourPay.net

需要的参数

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

驱动特殊细节

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

PayPal

需要的参数

  1. amount
  2. payerid (after paypal authentication)

Driver特殊细节

使用 PayPal 驱动跟其他的信用卡交易稍有不同。它需要两个 process() 方法调用。第一个将发送用户数据到 PayPal 的认证中心,第二个才实际处理了交易。下面是例子:

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 '<pre>'.print_r($this->session->get(), true);
	}
}

撰写你自己的驱动

还有很多 Kohana 没有包含的支付网关。我们鼓励用户撰写 module 没有包含的支付网关驱动。方法如下:

  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)
addons/payment.txt · 最后更改: 2009/02/08 17:43 由 icyleaf