Zencart支付插件开发笔记
v1.5.1

payment_gateway_apiplus

支付插件文件创建

1、includes/modules/payment目录下创建对应的文件名payment_gateway_apiplus.php,后续文章中以支付插件代表此文件。

支付插件页面中写入

<?php
    class payment_gateway_apiplus {
        var $code, $title, $description;

        function payment_gateway_apiplus() {
            $this->code = 'payment_gateway_apiplus';
            $this->title = MODULE_PAYMENT_HOSTED_PAYMENT_GATEWAY_API_TEXT_TITLE;
            $this->description = MODULE_PAYMENT_HOSTED_PAYMENT_GATEWAY_API_TEXT_DESCRIPTION;
            
        }

        function check() {}

        function install() {}

        function keys() {}
    }
?>

2、includes/languages/english/modules/payment目录下创建对应的文件名payment_gateway_apiplus.php,后续文章中以支付翻译代表此文件。

支付翻译页面中写入

<?php
  define('MODULE_PAYMENT_HOSTED_PAYMENT_GATEWAY_API_TEXT_TITLE', 'Payment Gateway');
  define('MODULE_PAYMENT_HOSTED_PAYMENT_GATEWAY_API_TEXT_DESCRIPTION', 'Payment Gateway for API+');
?>

支付插件安装

1、点击列表中的Payment Gateway支付插件后,右侧出现install按钮,此时点击将调用支付插件页面的install方法,我们将改方法改成如下:

function install() {
    global $db, $messageStack, $language;

    // 校验支付插件是否已经安装
    // MODULE_PAYMENT_PAYMENT_GATEWAY_APIPLUS_ENVIRONMENT 为我们定义的支付插件状态校验的关键字段
    if(defined('MODULE_PAYMENT_PAYMENT_GATEWAY_APIPLUS_ENVIRONMENT')) {
        $messageStack->add_session('payments module already installed.', 'error');
        zen_redirect(zen_href_link(FILENAME_MODULES, 'set=payment&module='.$this->code, 'NONSSL'));
        return 'failed';
    } 

    include(DIR_FS_CATALOG_LANGUAGES . $_SESSION['language'] . '/modules/payment/' . $this->code . '.php');
    
    // 执行安装SQL,插入对应所需填写的值
    $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_PAYMENT_GATEWAY_APIPLUS_ENVIRONMENT."', 'MODULE_PAYMENT_PAYMENT_GATEWAY_APIPLUS_ENVIRONMENT', 'Live', '6', '2', 'zen_cfg_select_option(array(\'Test\', \'Live\'), ', now())");
}

并且在keys方法中插入我们所定义的参数们

function keys() {
    return array('MODULE_PAYMENT_PAYMENT_GATEWAY_APIPLUS_ENVIRONMENT');
}

同时在支付翻译页面中插入

define('MODULE_PAYMENT_PAYMENT_GATEWAY_APIPLUS_ENVIRONMENT', 'Environment');

这样我们点击安装时页面上就出现了一条Environment的选项

但是这时候会有个问题,当我们选择其他支付插件在选择回来之后会发现停留在install按钮界面,当再次点击会提示已安装的错误信息,这是因为我们在install方法中校验了是否已经安装插件,不允许重复安装,所以需要再同页面的check方法中加入是否已经安装插件的校验:

function check() {
    global $db;
    if (!isset($this->_check)) {
        $check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PAYMENT_GATEWAY_APIPLUS_ENVIRONMENT'");
        $this->_check = $check_query->RecordCount();
    }
    return $this->_check;
}

MODULE_PAYMENT_PAYMENT_GATEWAY_APIPLUS_ENVIRONMENT作为了我们校验状态的关键KEY,这时刷新一下页面会发现按钮变成了edit和remove。

2、我们这时候在支付插件页面中写一个remove方法来作为卸载方法

function remove() {
    global $db;
    $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
}