<?php

/**
 * ips支付系统插件
 */



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

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

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

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

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

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

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

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

    /* 配置信息 */
    $modules[$i]['config']  = array(
        array('name' => 'ips_account',  'type' => 'text',   'value' => ''),
        array('name' => 'ips_key',      'type' => 'text',   'value' => ''),
        array('name' => 'ips_currency', 'type' => 'select', 'value' => '01'),
        array('name' => 'ips_lang',     'type' => 'select', 'value' => 'GB')
    );

$_LANG['ips']            = '环迅IPS';
$_LANG['ips_desc']       = 'IPS账户是上海环迅于2005年推出的新一代基于电子邮件的互联网多币种收付款工具。截止到目前，IPS账户具备在线充值、在线收付款、在线转账、网上退款和网上提款等多种功能，并支持多种账户充值方式。';
$_LANG['ips_account']    = 'IPS商户号';
$_LANG['ips_key']        = 'IPS密码';
$_LANG['ips_currency']   = '支付方式';
$_LANG['ips_currency_range']['01'] = '借记卡';
$_LANG['ips_currency_range']['02'] = '信用卡';
$_LANG['ips_currency_range']['04'] = 'IPS账户支付';
$_LANG['ips_currency_range']['08'] = 'I币支付';
$_LANG['ips_currency_range']['16'] = '电话支付';
$_LANG['ips_lang']   = '语言';
$_LANG['ips_lang_range']['GB']   = 'GB中文';
$_LANG['ips_lang_range']['EN']   = '英语';
$_LANG['ips_lang_range']['BIG5']   = 'BIG5中文';
$_LANG['ips_lang_range']['JP']   = 'JP';
$_LANG['ips_lang_range']['FR']   = 'FR';
$_LANG['pay_button']     = '立即使用IPS支付';

    return;
}

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

    }

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

    /**
    * 生成支付代码
    * @param   array   $order  订单信息
    * @param   array   $payment    支付方式信息
    */

    function get_code($order, $payment)
    {
        $billstr    = date('His', time());
        $datestr    = date('Ymd', time());
        $mer_code   = $payment['ips_account'];
        //$billno     = str_pad($order['log_id'], 6, '0', STR_PAD_LEFT) . $billstr;
		$billstr	= str_pad($order['log_id'], 6, '0', STR_PAD_LEFT);
		$billno		= $mer_code.$billstr;
        $amount     = sprintf("%0.02f", $order['order_amount']);
        $strcert    = $payment['ips_key'];
        $strcontent = $mer_code . $billstr . $amount . $datestr . $strcert; // 签名验证串 //
        $signmd5    = MD5($strcontent);

        //$def_url  = '<br /><form id="payform" style="text-align:center;" action="https://www.ips.net.cn/ipay/test_ipayment.asp" method="post">';
		$def_url  = '<br /><form id="payform" style="text-align:center;" action="https://www.ips.com.cn/ipay/ipayment.asp" method="post">';
        $def_url .= "<input type='hidden' name='mer_code' value='" . $mer_code . "'>\n";
        $def_url .= "<input type='hidden' name='billNo' value='" . $billno . "'>\n";
        $def_url .= "<input type='hidden' name='Gateway_type' value='" . $payment['ips_currency'] . "'>\n";
        $def_url .= "<input type='hidden' name='Currency_Type'  value='RMB'>\n";
        $def_url .= "<input type='hidden' name='lang'  value='" . $payment['ips_lang'] . "'>\n";
        $def_url .= "<input type='hidden' name='amount'  value='" . $amount . "'>\n";
        $def_url .= "<input type='hidden' name='date' value='" . $datestr . "'>\n";
        $def_url .= "<input type='hidden' name='DispAmount' value='" . $amount . "'>\n";
        $def_url .= "<input type='hidden' name='OrderEncodeType' value='1'>\n";
        $def_url .= "<input type='hidden' name='RetEncodeType' value='2'>\n";
        $def_url .= "<input type='hidden' name='merchanturl' value='" . return_url(basename(__FILE__, '.php')) . "'>\n";
        $def_url .= "<input type='hidden' name='SignMD5' value='" . $signmd5 . "'>\n";
        $def_url .= "</form><br /><script>document.all.payform.submit();</script>";

        return $def_url;
    }

    function respond()
    {
        $payment       = get_payment($_GET['code']);
        $billno        = $_GET['billno'];
        $amount        = $_GET['amount'];
        $mydate        = $_GET['date'];
        $succ          = $_GET['succ'];
        $msg           = $_GET['msg'];
        $ipsbillno     = $_GET['ipsbillno'];
        $retEncodeType = $_GET['retencodetype'];
        $currency_type = $_GET['Currency_type'];
        $signature     = $_GET['signature'];
        $order_sn      = intval(substr($billno, 6));

        if ($succ == 'Y')
        {
            $content = $billno . $amount . $mydate . $succ . $ipsbillno . $currency_type;
            $cert = $payment['ips_key'];
            $signature_1ocal = md5($content . $cert);

            if ($signature_1ocal == $signature)
            {
                if (!check_money($order_sn, $amount))
                {
                   return false;
                }
				online_pay_return($order_sn);
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

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

?>