ECSHOP payment plug-in development (Yin-sheng Alipay)

Brief

        Bowen developed on a silver Shengbao pay DEMO, this post is raw silver treasure in the payment integrated into ECSHOP. ECSHOP in payment plug-in development is not very complicated, mainly to provide basic information about a plug-in module, a plug-in class. This plug-in module into the information database and a corresponding display is mainly used for this plug-in installation information in the background; principal payment code generated to provide this third party payment and callbacks. Here is the code, welcome to learn from each other. (The following codes are merely simple realization of payment function, the code is not optimized [OCD forgive me])

Code

<?php
/**
 * ECSHOP 银生宝支付
 * ===============================================
 * Copy: Created by WEI ZHI HUA
 * Author: ZHIHUA·WEI<[email protected]>
 * Date: 2018/5/28
 * Time: 08:59
 * Project: ECSHOP 银生宝支付插件
 * Power: 银生宝支付插件
 * ===============================================
 */

if (!defined('IN_ECS')) {
    die('Hacking attempt');
}

/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE) {
    $i = isset($modules) ? count($modules) : 0;

    /* 代码 */
    $modules[$i]['code'] = basename(__FILE__, '.php');

    /* 描述对应的语言项 */
    $modules[$i]['desc'] = 'unspay_desc(银生宝支付描述)';

    /* 是否支持货到付款 */
    $modules[$i]['is_cod'] = '0';

    /* 是否支持在线支付 */
    $modules[$i]['is_online'] = '1';

    /* 作者 */
    $modules[$i]['author'] = 'ZHIHUA·WEI';

    /* 网址 */
    $modules[$i]['website'] = 'http://www.unspay.com';

    /* 版本号 */
    $modules[$i]['version'] = '3.0.0';

    /* 配置信息 */
    $modules[$i]['config'] = array(
        array('name' => 'merchantId', 'type' => 'text', 'value' => ''),
        array('name' => 'merchantUrl', 'type' => 'text', 'value' => ''),
        array('name' => 'merchantKey', 'type' => 'text', 'value' => ''),
    );
    return;
}

/**
 * Class unspay 银生宝支付类
 */
class unspay
{
    /**
     * 生成支付代码
     * @param $order array 订单信息
     * @param $payment array 字符信息
     * @return string
     */
    function get_code($order, $payment)
    {
        $unspay_url = "http://xxxxx(银生宝支付地址)/unspay/page/linkbank/payRequest.do";
        $version = '3.0.0';
        $merchantId = 'xxxx(银生宝商户号)';
        $merchantUrl = return_url(basename(__FILE__, '.php'));
        $responseMode = '2';
        $orderId = $order['order_sn'] . "A" . $order['log_id'];
        $currencyType = 'CNY';
        $amount = $order['order_amount'];
        $assuredPay = '';
        $time = date("YmdHis");
        $remark = 'payTest';
        $merchantKey = 'xxxxxxxx(银生宝商户密钥)';

        $s = "merchantId=";
        $s .= $merchantId;
        $s .= "&merchantUrl=";
        $s .= $merchantUrl;
        $s .= "&responseMode=";
        $s .= $responseMode;
        $s .= "&orderId=";
        $s .= $orderId;
        $s .= "¤cyType=";
        $s .= $currencyType;
        $s .= "&amount=";
        $s .= $amount;
        $s .= "&assuredPay=";
        $s .= $assuredPay;
        $s .= "&time=";
        $s .= $time;
        $s .= "&remark=";
        $s .= $remark;
        $s .= "&merchantKey=";
        $s .= $merchantKey;
        $mac = strtoupper(md5($s));

        $def_url = '<br /><form style="text-align:center;" method=post action="' . $unspay_url . '" target="_blank">';
        $def_url .= '<input type="hidden" name="version" value="' . $version . '">';
        $def_url .= '<input type="hidden" name="merchantId" value="' . $merchantId . '">';
        $def_url .= '<input type="hidden" name="merchantUrl" value="' . $merchantUrl . '">';
        $def_url .= '<input type="hidden" name="responseMode" value="' . $responseMode . '">';
        $def_url .= '<input type="hidden" name="orderId" value="' . $orderId . '">';
        $def_url .= '<input type="hidden" name="currencyType" value="' . $currencyType . '">';
        $def_url .= '<input type="hidden" name="amount" value="' . $amount . '">';
        $def_url .= '<input type="hidden" name="assuredPay" value="' . $assuredPay . '">';
        $def_url .= '<input type="hidden" name="time" value="' . $time . '">';
        $def_url .= '<input type="hidden" name="remark" value="' . $remark . '">';
        $def_url .= '<input type="hidden" name="mac" value="' . $mac . '">';
        //$def_url .= '<input type="hidden" name="bankCode" value="'.$bankCode.'">';
        $def_url .= '<input type="hidden" name="merchantKey" value="' . $merchantKey . '">';
        $def_url .= '<input type="submit" value="立即使用银生宝支付">';
        $def_url .= "</form>";
        return $def_url;
    }

    /**
     * 响应操作
     */
    function respond()
    {
        $merchantId = $_REQUEST["merchantId"];
        $merchantKey = "xxxxx(银生宝商户密钥)";
        $responseMode = $_REQUEST["responseMode"];
        $orderId = $_REQUEST["orderId"];
        $currencyType = $_REQUEST["currencyType"];
        $amount = $_REQUEST["amount"];
        $returnCode = $_REQUEST["returnCode"];
        $returnMessage = $_REQUEST["returnMessage"];
        $mac = $_REQUEST["mac"];
        $success = $returnCode == "0000";
        $paid = $returnCode == "0001";
        $s = "merchantId=";
        $s .= $merchantId;
        $s .= "&responseMode=";
        $s .= $responseMode;
        $s .= "&orderId=";
        $s .= $orderId;
        $s .= "¤cyType=";
        $s .= $currencyType;
        $s .= "&amount=";
        $s .= $amount;
        $s .= "&returnCode=";
        $s .= $returnCode;
        $s .= "&returnMessage=";
        $s .= $returnMessage;
        $s .= "&merchantKey=";
        $s .= $merchantKey;
        $nowMac = strtoupper(md5($s));

        //进行密文验证
        if ($nowMac == $mac) {
            //说明支付成功,在此处进行你的业务逻辑修改开发
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            //商户订单号
            $order_sn_arr = explode('A', $orderId);
            //$order_sn = $orderId;
            order_paid($order_sn_arr[1]);

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        } else {
            //此处支付未成功,进行支付未成功逻辑开发
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            if ($success || $paid) {
                $success = false;
                $paid = false;
                $returnCode = "0401";
                $returnMessage = "mac值校验错误!";
            }
            return false;
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        }
    }

}

 

Published 154 original articles · won praise 404 · Views 650,000 +

Guess you like

Origin blog.csdn.net/Zhihua_W/article/details/80475899