| 状态 | 草稿 |
|---|---|
| Todo | Needs updating from library to module |
| 官方最后更新时间 | 2009/02/08 14:58 |
支付模块可以让用户很容易的处理电子商务交易,而不需要担心背后的细节。它有很多特性:
设置 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 扩展包含一系列默认的属性。这些属性都还比较简单易懂,在下面列出:
特定的驱动可能需要这些属性的一部分或者全部。某些驱动也可能需要自己特定的属性,一般可以在驱动的说明里看到。
Payment 扩展有几个方法可以使用:
这个方法允许你使用一个array来指定支付的属性。
$payment = new Payment(); $attributes = array ( 'card_num' = '1234567890123456', 'exp_date' = '0510' }; $payment->set_fields($attributes);
这个方法完成支付。设置好属性之后,只要在一个if语句中调用这个方法,检查返回值是否是 TRUE,即可知道支付交易是否成功,如果失败,则会返回一个错误描述的字符串,该字符串可以告诉你如何处理这个错误。
Payment 扩展的驱动库接口很像数据库 API,这样可以在不同的支付网关之间保持 API 的一致性。它目前支持以下几种支付网关:
如果你有 Authorize.net 网站开发者的测试账户请在配置中开启 test_mode。如果你在测试模式下有一个正规账户(在你的 Authorize.net 页面设置)那么请关闭 test_mode。
The amount above is the subtotal. Tax and Shipping get added to the amount to form the grand total inside the 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 没有包含的支付网关驱动。方法如下: