<?php
require('paymentPlugin.php');
class pay_tenpay extends paymentPlugin{

    var $name = '腾讯财付通[即时到账]';//腾讯财付通
    var $logo = 'TENPAY';
    var $version = 20070902;
    var $charset = 'gb2312';
    var $applyUrl = 'http://union.tenpay.com/mch/mch_register_b2c.shtml';//即时到帐
    var $applyUrlAgain = 'https://www.tenpay.com/mchhelper/mch_register_c2c.shtml';//担保
    //var $submitUrl = 'http://portal.tenpay.com/cfbiportal/cgi-bin/cfbiin.cgi'; 
    var $submitUrl = 'https://www.tenpay.com/cgi-bin/v1.0/pay_gate.cgi'; 
    var $submitButton = 'http://img.alipay.com/pimg/button_alipaybutton_o_a.gif'; ##需要完善的地方
    var $supportCurrency =  array("CNY"=>"1");
    var $supportArea =  array("AREA_CNY");
    var $desc = '财付通是腾讯公司为促进中国电子商务的发展需要，满足互联网用户价值需求，针对网上交易安全而精心推出的一系列服务。';
    var $intro = '财付通是腾讯公司于2005年9月正式推出专业在线支付平台，致力于为互联网用户和企业提供安全、便捷、专业的在线支付服务。<br>财付通构建全新的综合支付平台，业务覆盖B2B、B2C和C2C各领域，提供卓越的网上支付及清算服务。<br>财付通先后荣膺2006年电子支付平台十佳奖、2006年最佳便捷支付奖、2006年中国电子支付最具增长潜力平台奖和2007年最具竞争力电子支付企业奖等奖项，并于2007年首创获得“国家电子商务专项基金”资金支持。<br><br><font color="red">本接口需点击【立即申请即时到帐帐户】链接进行在线签约和付费后方可使用。</font>';    
    var $applyProp = array("postmethod"=>"get","sp_suggestuser"=>"2289480");//代理注册参数组
    var $orderby = 5;

    function toSubmit($payment){
        $merId = $this->getConf($payment['M_OrderId'], 'member_id');
        $ikey = $this->getConf($payment['M_OrderId'], 'PrivateKey');

        $payment['M_Currency'] = "1";    //$order->M_Currency = "1";
        
        $orderdate = date("Ymd",$payment['M_Time']);    //$order->M_Time
        $payment['M_Amount'] = ceil($payment['M_Amount'] * 100);    //$order->M_Amount
        //$v_orderid = $merId.$orderdate."0000" . $payment['M_OrderId'];  //$order->M_OrderId
        $v_orderid = $merId.$orderdate.substr($payment['M_OrderId'],-10);
        $md5string=strtoupper(md5("cmdno=1&date=".$orderdate."&bargainor_id=".$merId."&transaction_id=".$v_orderid."&sp_billno=".$payment['M_OrderNO']."&total_fee=".$payment['M_Amount']."&fee_type=1&return_url=".$this->callbackUrl."&attach=".$payment['M_OrderId']."&key=".$ikey));
        $subject = $payment['M_OrderNO'];
    

        $return["cmdno"] = "1";
        $return["date"] = $orderdate;
        $return["bank_type"] = "0";
        $return["desc"] = $subject;
        $return["purchaser_id"] = "";
        $return["bargainor_id"] = $merId;
        $return["transaction_id"] = $v_orderid;//$payment['M_OrderId'];
        $return["sp_billno"] = $payment['M_OrderNO'];   //$order->M_OrderNO;
        $return["total_fee"] = $payment['M_Amount'];    //$order->M_Amount;
        $return["fee_type"] =  $payment['M_Currency'];  //$order->M_Currency;
        $return["return_url"] = $this->callbackUrl;
        $return["attach"] = $payment['M_OrderId'];
        $return["ikey"]=$ikey;
        $return["sign"] = $md5string;
        return $return;
    }

    function callback($in,&$paymentId,&$money,&$message,&$tradeno){    
        $cmdno=$in["cmdno"];
        $pay_result=$in["pay_result"];
        $pay_info=$in["pay_info"];
        $date=$in["date"];
        $bargainor_id=$in["bargainor_id"];
        $transaction_id=$in["transaction_id"];
        $sp_billno=$in["sp_billno"];
        $total_fee=$in["total_fee"];
        $fee_type=$in["fee_type"];
        $attach=$in["attach"];
        $sign=$in["sign"];
        $mac ="";
        $v_orderid = substr($v_order_no,-6);
        $ikey = $this->getConf($in['attach'], 'PrivateKey');
        $md5mac = strtoupper(md5("cmdno=$cmdno&pay_result=$pay_result&date=$date&transaction_id=$transaction_id&sp_billno=$sp_billno&total_fee=$total_fee&fee_type=$fee_type&attach=$attach&key=$ikey"));
        $paymentId=$in['attach'];
        $money=intval($total_fee)/100;
        $tradeno = $in['transaction_id'];
        if($md5mac!=$sign){
            $message = '签名认证失败,请立即与商店管理员联系';
            return PAY_ERROR;
        }else{
            if($pay_result==0){
                return PAY_SUCCESS;
            }else{
                $message = '支付失败,请立即与商店管理员联系'.$pay_info;
                return PAY_FAILED;
            }
        }
    }

    function getfields(){
        return array(
                'member_id'=>array(
                        'label'=>'客户号',
                        'type'=>'string'
                ),
                'PrivateKey'=>array(
                        'label'=>'私钥',
                        'type'=>'string'
                )
            );
    }
    function applyForm($agentfield){
      //$tmp_form.='<a href="javascript:void(0)" onclick="document.applyForm.submit()">立即注册即时到帐帐户</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:void(0)" onclick="document.applyFormAgain.submit()">立即注册担保帐户</a>';
      $tmp_form= '<a href="javascript:void(0)" onclick="document.applyForm.submit()">立即申请即时到帐帐户</a>';
      $tmp_jsdz_form="<form name='applyForm' method='".$agentfield['postmethod']."' action='http://top.shopex.cn/recordpayagent.php' target='_blank'>";
      $tmp_db_form="<form name='applyFormAgain' method='".$agentfield['postmethod']."'  action='http://top.shopex.cn/recordpayagent.php' target='_blank'>";
      foreach($agentfield as $key => $val){
          if ($key=="agenturl")
            $tmp_jsdz_form.="<input type='hidden' name='".$key."' value='".$val."'>";
          elseif ($key=="agenturlAgain")
             $tmp_db_form.="<input type='hidden' name='agenturl' value='".$val."'>";
          elseif ($key == "payagentkey"){
              $tmp_jsdz_form.="<input type='hidden' name='".$key."' value='".$val."JSDZ'>";
              $tmp_db_form.="<input type='hidden' name='".$key."' value='".$val."DB'>";
          }
          else {
              $tmp_jsdz_form.="<input type='hidden' name='".$key."' value='".$val."'>";
              $tmp_db_form.="<input type='hidden' name='".$key."' value='".$val."'>";
          }
      }
      $tmp_jsdz_form.="</form>";
      $tmp_db_form.="</form>";
      $tmp_form.=$tmp_jsdz_form;//.$tmp_db_form;
      return $tmp_form;
   }
}
?>
