<?php

/**
 * 网银在线插件
 */

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

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

    /* 描述对应的语言项 */
    $modules[$i]['desc']    = 'chinabank_desc';

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

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

    /* 支付费用 */
    $modules[$i]['pay_fee'] = '2.5%';

    /* 作者 */
    $modules[$i]['author']  = 'ECSHOP TEAM';

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

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

    /* 配置信息 */
    $modules[$i]['config'] = array(
        array('name' => 'chinabank_account', 'type' => 'text', 'value' => ''),
        array('name' => 'chinabank_key',     'type' => 'text', 'value' => ''),
    );

	$_LANG['chinabank']         = '网银在线';
	$_LANG['chinabank_desc']    = '网银在线与中国工商银行、招商银行、中国建设银行、农业银行、民生银行等数十' .
			'家金融机构达成协议。全面支持全国19家银行的信用卡及借记卡实现网上支付。（网址：http://www.chinabank.com.cn）';
	$_LANG['chinabank_account'] = '商户编号';
	$_LANG['chinabank_key']     = 'MD5 密钥';
	$_LANG['pay_button']        = '立即使用网银在线支付';

    return;
}

/**
 * 类
 */
class chinabank
{
    /**
     * 构造函数
     *
     * @access  public
     * @param
     *
     * @return void
     */
    function chinabank()
    {
    }

    function __construct()
    {
        $this->chinabank();
    }

    /**
     * 生成支付代码
     * @param   array   $order      订单信息
     * @param   array   $payment    支付方式信息
     */
    function get_code($order, $payment)
    {
        $data_vid           = trim($payment['chinabank_account']);
        $data_orderid       = date("Ymd")."-".$data_vid."-".substr(1000000+$order['log_id'],-6);
        $data_vamount       = $order['order_amount'];
        $data_vmoneytype    = 'CNY';
        $data_vpaykey       = trim($payment['chinabank_key']);
        $data_vreturnurl    = return_url(basename(__FILE__, '.php'));

        $MD5KEY =$data_vamount.$data_vmoneytype.$data_orderid.$data_vid.$data_vreturnurl.$data_vpaykey;
        $MD5KEY = strtoupper(md5($MD5KEY));

        $def_url  = '<br /><form id="payform" style="text-align:center;" method=post action="https://pay3.chinabank.com.cn/PayGate">';
        $def_url .= "<input type=HIDDEN name='v_mid' value='".$data_vid."'>";
        $def_url .= "<input type=HIDDEN name='v_oid' value='".$data_orderid."'>";
        $def_url .= "<input type=HIDDEN name='v_amount' value='".$data_vamount."'>";
        $def_url .= "<input type=HIDDEN name='v_moneytype'  value='".$data_vmoneytype."'>";
        $def_url .= "<input type=HIDDEN name='v_url'  value='".$data_vreturnurl."'>";
        $def_url .= "<input type=HIDDEN name='v_md5info' value='".$MD5KEY."'>";
        $def_url .= "</form><script>document.all.payform.submit();</script>";

        return $def_url;
    }

    /**
     * 响应操作
     */
    function respond()
    {
        $payment        = get_payment('chinabank');

        $v_oid          = trim($_REQUEST['v_oid']);
        $v_pmode        = trim($_REQUEST['v_pmode']);
        $v_pstatus      = trim($_REQUEST['v_pstatus']);
        $v_pstring      = trim($_REQUEST['v_pstring']);
        $v_amount       = trim($_REQUEST['v_amount']);
        $v_moneytype    = trim($_REQUEST['v_moneytype']);
        $remark1        = trim($_REQUEST['remark1']);
        $remark2        = trim($_REQUEST['remark2']);
        $v_md5str       = trim($_REQUEST['v_md5str']);

		$log_id			= intval(substr($v_oid,strrpos($v_oid,"-")+1));

        /**
         * 重新计算md5的值
         */
        $key            = $payment['chinabank_key'];

        $md5string=strtoupper(md5($v_oid.$v_pstatus.$v_amount.$v_moneytype.$key));

        /* 检查秘钥是否正确 */
        if ($v_md5str==$md5string)
        {
            if ($v_pstatus == '20')
            {
				if (!check_money($log_id, $v_amount))
                {
                   return false;
                }
                /* 改变订单状态 */
                online_pay_return($log_id);

                return true;
            }
        }
        else
        {
            return false;
        }
    }

	function get_order_id(){
		return $_GET['v_oid'];
	}
}

?>