最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > WordPress > WordPress教程 >

创建WooCommerce第三方支付模块

时间:2014-09-03 16:46来源:未知 作者:最模板zuimoban 点击:
WooCommerce二次开发 创建WooCommerce第三方支付模块 创建WooCommerce第三方支付模块和开发一个普通的WooCommerce插件是一样的,它们都遵循wordpress的插件标准和开发规范。 WooCommerce把支付模块划

WooCommerce二次开发 — 创建WooCommerce第三方支付模块

创建WooCommerce第三方支付模块和开发一个普通的WooCommerce插件是一样的,它们都遵循wordpress的插件标准和开发规范。

WooCommerce把支付模块划分成四类,分别是

基于表单(FORM)的支付模块
用户单击表单的一个提交按钮后会被带到(跳转到)第三方支付的网关支付页面,如PayPal standard, Authorize.net DPM

基于iframe的支付模块
这个是相对于“基于表单(FORM)的支付模块 ”,用户不会被跳转到第三方支付的网站,而是在商户的网站内直接通过iframe加载第三方支付的网关支付页面,就好像是在站内一样。

直接支付(Direct)的支付模块
用户在下单前就已经在支付流程内填好了支付的信息(如信用卡),在单击下单(或提交)按钮后,即完成付款。没有跳转或内嵌。

线下支付(Offline)的支付模块
即用户通过支票,银行线下转账,到付等。

表单和iframe内嵌这两类支付模块都要将数据post给第三方支付网站,所以安全性有点欠缺。而直接支付支付模块相对来说安全一些。因为它通常会要求商户的网站要足够安全,比如必须支持SSL做为接入的条件,通常是要符合PCI标准的。

了解了之后,我们知道我们国内大部分的第三方支付应该都是属于第一类,第二类的开发,就是跳转支付或内嵌支付。下面就以开发一个基于表单(FORM)的支付模块为例来说明创建一个WooCommerce第三方支付模块。

第一步,像其它的WooCommerce插件一样,你要检查WooCommerce是否已经安装并激活。

/**
 * Check if WooCommerce is active
 **/
if ( in_array( ‘woocommerce/woocommerce.php’, apply_filters( ‘active_plugins’, get_option( ‘active_plugins’ ) ) ) ) {
    // Put your plugin code here
}

第二步,定义你插件的plugins_loaded hook(加载插件钩或事件吧),通过hook实现在插件被加载的时候,加载你的支付网关类(就是你写的支付网关代码了)。

function init_pagbrasil_gateway_class() {
    include_once(dirname(__FILE__) . ‘/includes/Acc.php’);    
    include_once(dirname(__FILE__) . ‘/includes/Bb.php’);    
    include_once(dirname(__FILE__) . ‘/includes/Obt.php’);    
}
add_action( ‘plugins_loaded’, ‘init_pagbrasil_gateway_class’ );

在上面的代码,我定义plugins_loaded的处理函数init_pagbrasil_gateway_class,在函数我加载了三个支付网关类。

如果你有去阅读WooCommerce的代码,你会发现WooCommerce的所有支付模块是保存在wp-content/plugins/woocommerce/includes/gateways目录下,一个支付网关对应一个目录,每个目录下一个文件定义了一个支付网关类,比如paypal目录下的class-wc-gateway-paypal.php定义了paypal标准支付网关类 WC_Gateway_Paypal。当然每个支付网关可能还会有一些模板文件。

但是我们开发的支付网关是作为一个插件的形式,不要直接放到这个目录下,即使放进去了,它也不会识别到。所以我们要在插件加载的时候自己加载我们定义的类文件。

第三,告诉WooCommerce我们定义了新的支付网关类。
正如我上面所说,即使我们把文件放到wp-content/plugins/woocommerce/includes/gateways目录下,WooCommerce也不会知道并自动加载它,所以我们要通过上面定义的hook来加载我们定义的支付网关类。但是很快你会发现,光加载了还不够,WooCommerce对你定义的类文件视若无睹。什么办?我们要添加一个filter来向WooCommerce声明我们定义了新的支付网关类,而不是没用的类。

function add_pagbrasil_gateway_class( $methods ) {
    $methods[] = ‘WC_Gateway_Pagbrasil_Acc’;
    $methods[] = ‘WC_Gateway_Pagbrasil_Bb’;
    $methods[] = ‘WC_Gateway_Pagbrasil_Obt’;
    return $methods;
}
add_filter( ‘woocommerce_payment_gateways’, ‘add_pagbrasil_gateway_class’ );

上面代码需要注意的是,你定义了几个类就向$methods数组声明几个元素,我之所以声明了三个,是因为我上一个项目是开发了三个支付模块。

第四,现在我们来关注我们应该怎样写我们的支付网关类。
通过对wp-content/plugins/woocommerce/includes/gateways目录下WooCommerce自带的支付网关类的阅读分析,

4.1 首先我们的支付网关类要继承父类WC_Payment_Gateway,如

class WC_Gateway_COD extends WC_Payment_Gateway {

继承父类WC_Payment_Gateway作用是明显的,第一,你可以轻松的给你的支付模块在wordpress后台添加一些配置选项,比如你在第三方支付那边开的商户,加密资料等等。第二,大部分的函数直接从父类继承,仅有部分做下修改就可以了。

4.2 构造函数__construct()

    $this->id – 支付网关的唯一标识符,如‘your_gateway’
    $this->icon – 如果你想在前台支付网关的标题旁显示图片,那么请给这个指定一个图片的URL。
    $this->has_fields – 这个是给直接支付类网关使用的,决定是否在支付流程中直接显示用来收集支付信息的表单控件。
    $this->method_title – 在wordpress后台显示的支付网关标题
    $this->method_description – 在wordpress后台显示的支付网关的描述。

另外,构造函数还要调用 下面这两个函数,init_form_fields函数用来定义在wordpress后台显示的用来配置支付网关的表单。
init_settings则是将保存在数据库中关于支付网关的配置参数读取出来,之后我们可以通过$this->get_option函数来获取。

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

4.3 init_form_fields()函数

我们已经知道init_form_fields函数用来定义在wordpress后台显示的用来配置支付网关的表单。它决定了你的支付网关,有哪些参数,每个参数要使用什么控件,比如input,还是select,参数的默认值等。

    /**
     * Initialise Gateway Settings Form Fields
     *
     * @access public
     * @return void
     */
    function init_form_fields() {

        $this->form_fields = array(
            ’enabled’ => array(
                ’title’   => __( ‘Enable/Disable’, ‘pagbrasil’ ),
                ’type’    => ‘checkbox’,
                ’label’   => __( ‘Enable PagBrasil Credit Card Payment’, ‘pagbrasil’ ),
                ’default’ => ‘yes’
            ),
            ’title’ => array(
                ’title’       => __( ‘Title’, ‘pagbrasil’ ),
                ’type’        => ‘text’,
                ’description’ => __( ‘This controls the title which the user sees during checkout.’, ‘pagbrasil’ ),
                ’default’     => __( ‘PagBrasil Credit Card Payment’, ‘pagbrasil’ ),
                ’desc_tip’    => true,
            ),
            ’description’ => array(
                ’title’       => __( ‘Description’, ‘pagbrasil’ ),
                ’type’        => ‘textarea’,
                ’description’ => __( ‘This controls the description which the user sees during checkout.’, ‘pagbrasil’ ),
                ’default’     => __( ‘Pay via PagBrasil; you can pay with Visa,Mastero,AME … ‘, ‘pagbrasil’ )
            ),
            ’email’ => array(
                ’title’       => __( ‘Email’, ‘pagbrasil’ ),
                ’type’        => ‘email’,
                ’description’ => __( ‘Please enter your email address; ‘, ‘pagbrasil’ ),
                ’default’     => ”,
                ’desc_tip’    => true,
                ’placeholder’ => ‘you@youremail.com’
            ),
            ’identity_token’ => array(
                ’title’       => __( ‘PagBrasil Identity Token’, ‘pagbrasil’ ),
                ’type’        => ‘text’,
                ’description’ => __( ‘Token assigned to your merchant account.If you have thisfeature enabled in your account, your token will be displayed at the PagBrasil Merchant Interface, tab “Update Information”.’, ‘pagbrasil’ ),
                ’default’     => ”,
                ’desc_tip’    => true,
                ’placeholder’ => __( ‘PagBrasil Identity Token’, ‘pagbrasil’ )
            ),
            ’identity_secret’ => array(
                ’title’       => __( ‘PagBrasil Secret Phrase’, ‘pagbrasil’ ),
                ’type’        => ‘text’,
                ’description’ => __( ‘Pre-shared secret phrase to secure the transaction.Secret phrase as defined in the PagBrasil\’s Merchant Interface’, ‘pagbrasil’ ),
                ’default’     => ”,
                ’desc_tip’    => true,
                ’placeholder’ => __( ‘PagBrasil Secret Phrase’, ‘pagbrasil’ )
            ),            
            ’invoice_prefix’ => array(
                ’title’       => __( ‘Invoice Prefix’, ‘pagbrasil’ ),
                ’type’        => ‘text’,
                ’description’ => __( ‘Please enter a prefix for your invoice numbers. If you use your PagBrasil account for multiple stores ensure this prefix is unique as PagBrasil will not allow orders with the same invoice number.’, ‘pagbrasil’ ),
                ’default’     => ‘WC’,
                ’desc_tip’    => true,
            ),
            ’debug’ => array(
                ’title’       => __( ‘Debug Log’, ‘pagbrasil’ ),
                ’type’        => ‘checkbox’,
                ’label’       => __( ‘Enable logging’, ‘pagbrasil’ ),
                ’default’     => ‘no’,
                ’description’ => sprintf( __( ‘Log PagBrasil events, such as IPN requests, inside <code>woocommerce/logs/pagbrasil_acc-%s.txt</code>’, ‘pagbrasil’ ), sanitize_file_name( wp_hash( ‘pagbrasil_acc’ ) ) ),
            )
        );
    }
    
   

(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容