<?php

/**
 * 快钱插件
 */


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

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

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

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

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

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

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

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

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

	$_LANG['kuaiqian']   = '快钱';
	$_LANG['kq_desc']    = '上海快钱信息服务有限公司是国内第一家提供基于EMAIL和手机号码的网上收付款服务的互联网企业。' .
							'快钱已同中国建设银行、中国银联、中国农业银行、中国银行、招商银行、交通银行、华夏银行、中国光大银行、' .
							'中信银行、深圳发展银行、广东发展银行等结成战略合作并推出网上交易的收付款服务，服务覆盖国内外27亿张银行卡。' .
							'<a href="http://agent.99bill.com/agent/jsp/agent/onlineprotocol/agentProtocol.do?act=check&agentno=bjcx13" style="font-weight:bold; color:#CC0000" >点击这里注册即可获得 1500 条免费短信。</a>';
	$_LANG['kq_account'] = '商户编号';
	$_LANG['kq_key']     = '商户密钥';
	$_LANG['pay_button'] = '立即使用快钱支付';

    return;
}

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

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

    /**
     * 生成支付代码
     * @param   array   $order  订单信息
     * @param   array   $payment    支付方式信息
     */
    function get_code($order, $payment)
    {
        $data_merchant_id   = $payment['kq_account'];
        $data_order_id      = "kq".date("Ymd").$order['log_id'];
        $data_amount        = $order['order_amount'] * 100;
        $data_paymoney_type = '1';
        $data_body          = '';
        $data_pname         = !empty($order['user_name']) ? $order['user_name'] : '';
        $data_return_url    = return_url(basename(__FILE__, '.php'));
		$data_order_time	= date('YmdHis');
		$payerContact		= "admin@nicenic.net";

        $data_pay_key       = $payment['kq_key'];
        $data_pay_account   = $payment['kq_account'];

		$signMsgVal = "inputCharset=2&pageUrl=".$data_return_url."&version=v2.0&".
					"language=1&signType=1&merchantAcctId=".$data_merchant_id."&payerName=".$_SESSION['User_S']."&".
					"payerContactType=1&payerContact=".$payerContact."&".
					"orderId=".$data_order_id."&orderAmount=".$data_amount."&orderTime=".$data_order_time."&productName=网上支付&".
					"productNum=1&payType=00&key=".$data_pay_key;
        $MD5KEY = strtoupper(md5($signMsgVal));
		

		$def_url  = '<body onload="document.payform.submit()">';
        $def_url .= '<form name="payform" id="payform" action="https://www.99bill.com/gateway/recvMerchantInfoAction.htm" method="post">';
		$def_url .= "<input type='hidden' name='inputCharset' value='2'>";
		$def_url .= "<input type='hidden' name='pageUrl' value='".$data_return_url."'>";
		$def_url .= "<input type='hidden' name='version' value='v2.0'>";
		$def_url .= "<input type='hidden' name='language' value='1'>";
		$def_url .= "<input type='hidden' name='signType' value='1'>";
		$def_url .= "<input type='hidden' name='merchantAcctId' value='".$data_merchant_id."'>";
		$def_url .= "<input type='hidden' name='payerName' value='".$_SESSION['User_S']."'>";
		$def_url .= "<input type='hidden' name='payerContactType' value='1'>";
		$def_url .= "<input type='hidden' name='payerContact' value='".$payerContact."'>";
		$def_url .= "<input type='hidden' name='orderId' value='".$data_order_id."'>";
		$def_url .= "<input type='hidden' name='orderAmount' value='".$data_amount."'>";
		$def_url .= "<input type='hidden' name='orderTime' value='".$data_order_time."'>";
		$def_url .= "<input type='hidden' name='productName' value='网上支付'>";
		$def_url .= "<input type='hidden' name='productNum' value='1'>";
		$def_url .= "<input type='hidden' name='payType' value='00'>";
		$def_url .= "<input type='hidden' name='signMsg' value='".$MD5KEY."'>";
		$def_url .= "</form>";
		

        return $def_url;
    }

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

        $merchant_id    = $payment['kq_account'];               ///获取商户编号
        $merchant_key   = $payment['kq_key'];                   ///获取秘钥

        $order_sn       = substr($_REQUEST[orderId],10);
        $amount         = trim(@$_REQUEST['orderAmount'])/100;   ///获取订单金额

		$mac			= $_REQUEST[signMsg];					//获取返回的加密串


		$ScrtStr="merchantAcctId=".$merchant_id;
		if($_REQUEST[version]) $ScrtStr.="&version=".$_REQUEST[version];
		if($_REQUEST[language]) $ScrtStr.="&language=".$_REQUEST[language];
		if($_REQUEST[signType]) $ScrtStr.="&signType=".$_REQUEST[signType];
		if($_REQUEST[payType]) $ScrtStr.="&payType=".$_REQUEST[payType];
		if($_REQUEST[bankId]) $ScrtStr.="&bankId=".$_REQUEST[bankId];
		if($_REQUEST[orderId]) $ScrtStr.="&orderId=".$_REQUEST[orderId];
		if($_REQUEST[orderTime]) $ScrtStr.="&orderTime=".$_REQUEST[orderTime];
		if($_REQUEST[orderAmount]) $ScrtStr.="&orderAmount=".$_REQUEST[orderAmount];
		if($_REQUEST[dealId]) $ScrtStr.="&dealId=".$_REQUEST[dealId];
		if($_REQUEST[bankDealId]) $ScrtStr.="&bankDealId=".$_REQUEST[bankDealId];
		if($_REQUEST[dealTime]) $ScrtStr.="&dealTime=".$_REQUEST[dealTime];
		if($_REQUEST[payAmount]) $ScrtStr.="&payAmount=".$_REQUEST[payAmount];
		if($_REQUEST[fee]!='') $ScrtStr.="&fee=".$_REQUEST[fee];
		if($_REQUEST[ext1]) $ScrtStr.="&ext1=".$_REQUEST[ext1];
		if($_REQUEST[ext2]) $ScrtStr.="&ext2=".$_REQUEST[ext2];
		if($_REQUEST[payResult]) $ScrtStr.="&payResult=".$_REQUEST[payResult];
		if($_REQUEST[errCode]) $ScrtStr.="&errCode=".$_REQUEST[errCode];
		$ScrtStr.="&key=".$merchant_key;

        $mymac    = md5($ScrtStr);

        $v_result = false;

        if (strtoupper($mac) == strtoupper($mymac))
        {
            if ($_REQUEST[payResult] == "10" )
            {
				if (!check_money($order_sn, $amount))
                {
                   return false;
                }
                ///支付成功
                $v_result = true;

                online_pay_return($order_sn);
            }
        }

        return $v_result;
    }

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

?>