您的位置:首页 > 其它

WooCommerce接入支付宝支付功能(二)——WooCommerce中添加新的支付网关

2017-11-24 15:48 726 查看
前面一篇博客介绍了支付宝开放平台上如何创建应用,以及相应的SDK中提供的接口。

这篇博客介绍一下WooCommerce中如何添加新的支付方式。

创建一个简单的支付网关

WooCommerce中的支付网关是基于类的,可以通过插件的方式添加。在插件里面,需要在插件加载后创建一个类,比如:

add_action( 'plugins_loaded', 'init_your_gateway_class' );


并且你的类需要继承自WooCommerce网关基类,然后你才可以使用设置API以及其他一些WooCommerce提供的方法:

function init_your_gateway_class() {

class WC_Gateway_Your_Gateway extends WC_Payment_Gateway {}

}


除了定义自己的类,还需要告诉WooCommerce这个类的存在。这是通过添加过滤器实现的:

function add_your_gateway_class( $methods ) {

$methods[] = 'WC_Gateway_Your_Gateway';

return $methods;

}

add_filter( 'woocommerce_payment_gateways', 'add_your_gateway_class' );


类中需要实现的方法

大多数的方法继承自WC_Payment_Gateway类,但是在自己的网关中要求实现一些方法。

__construct()

在构造方法中,应该定义以下的变量:

$this->id:自己的网关的唯一ID,比如’your_gateway’

$this->icon:如果你想在前端网关名称的旁边显示一张图片,输入图片的URL

$this->has_fields:布尔值,如果你想要支付域在结算页面显示的话,设置成true

$this->method_title:在admin页面显示的支付方法的标题

$this->method_description:在admin页面显示的支付方法的描述

构造方法中应该定义并加载设置域:

$this->init_form_fields();
$this->init_settings();


在调用了
init_settings()
之后,可以获取设置并且保存在变量中,比如:

$this->title = $this->get_option( 'title' );


最后,需要为你的设置添加一个保存钩子:

add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );


init_form_fields()

这个方法用于设置
$this->form_fields
,这是你在自己的网关设置页面中显示的选项,这个方法中需要使用WC Settings API([2])。

自己网关的基本设置应该包含enabled,title和description:

$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Cheque Payment', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Cheque Payment', 'woocommerce' ),
'desc_tip'      => true,
),
'description' => array(
'title' => __( 'Customer Message', 'woocommerce' ),
'type' => 'textarea',
'default' => ''
)
);


process_payment( $order_id )

这是网关最重要的部分——处理支付以及订单。这个方法也告诉WC重定向用户到哪,这是通过返回的array实现的。

下面以Cheque网关的process_function方法为例:

function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );

// Mark as on-hold (we're awaiting the cheque)
$order->update_status('on-hold', __( 'Awaiting cheque payment', 'woocommerce' ));

// Reduce stock levels
$order->reduce_order_stock();

// Remove cart
$woocommerce->cart->empty_cart();

// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}


这个方法实现了:

获取并更新处理中的订单

减少库存并清空购物车

返回success和重定向URL(这个例子中是感谢页面)

Cheque给订单一个On-Hold状态,因为支付不能被自动验证。如果你在构建一个direct网关,那么你应该在这里完成订单。在订单被支付之后,应该使用payment_complete而不是update_status:

$order->payment_complete();


这可以确保库存确实减少并且状态是正确的值。

如果支付失败,你应该抛出一个错误并且返回null:

wc_add_notice( __('Payment error:', 'woothemes') . $error_message, 'error' );
return;


WooCommerce会捕获这个错误并在结算页面中显示。

更新订单状态

更新订单状态可以通过使用订单类中的方法实现。更新到一个定制状态的例子如下:

$order = new WC_Order( $order_id );
$order->update_status('on-hold', __('Awaiting cheque payment', 'woothemes'));


上面的例子把订单状态更新为On-Hold,并且添加一个注意消息通知顾客正在等待一个Cheque。你也可以不再更新订单状态的时候添加注意消息:

$order->add_order_note( __('IPN payment completed', 'woothemes') );


订单状态实践建议

如果订单完成但是admin需要人工验证支付,使用On-Hold

如果订单失败并且已经创建,设置为Failed

如果订单完成,让WooCommerce处理状态并且使用
$order->payment_complete()
. WooCommerce会使用Completed或者Processing状态并处理库存。

参考

[1] Payment Gateway API

[2] Settings API
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: