<?php
 namespace Home\Controller;
 use Think\Controller;
 class YongchunController extends Controller{
        //在类初始化方法中，引入相关类库
        protected $p1_MerId;
        protected $merchantKey;
        protected $reqURL_onLine;
        protected $p0_Cmd;
        protected $p9_SAF;
        public function _initialize() {
            $ylist = M('yongchun')->find();
            $this->p1_MerId = $ylist['yid'];
            $this->merchantKey = $ylist['key'];
            $this->reqURL_onLine = "http://api.a8tg.com/GateWay/Bank.aspx";
            $this->p0_Cmd = "Buy";
            $this->p9_SAF = "0";
         }
         public function pay(){
            //var_dump($this->p0_Cmd);die;
            $p2_Order                   = $_GET['order'];
            $dingdan = M('dingdan')->where("id = $p2_Order")->find();
            $p3_Amt                     = $dingdan['zongjia'];
            $p4_Cur                     = "CNY";
            $p5_Pid                     = "cardbuy";
            $p6_Pcat                    = "cardbuy";
            $p7_Pdesc                   = "cardbuy";
            $p8_Url                     = "http://".$_SERVER['HTTP_HOST']."/index.php/Home/Yongchun/NotifyUrl";
            $pa_MP                      = $_REQUEST['payorderid'];
            $pd_FrpId                   = 'ALIPAY';
            $pr_NeedResponse    = "1";

            $hmac = $this->getReqHmacString($p2_Order,$p3_Amt,$p4_Cur,$p5_Pid,$p6_Pcat,$p7_Pdesc,$p8_Url,$pa_MP,$pd_FrpId,$pr_NeedResponse);
            $this->assign('p0_Cmd',$this->p0_Cmd);
            $this->assign('p1_MerId',$this->p1_MerId);
            $this->assign('p2_Order',$p2_Order);
            $this->assign('p3_Amt',$p3_Amt);
            $this->assign('p4_Cur',$p4_Cur);
            $this->assign('p5_Pid',$p5_Pid);
            $this->assign('p6_Pcat',$p6_Pcat);
            $this->assign('p7_Pdesc',$p7_Pdesc);
            $this->assign('p8_Url',$p8_Url);
            $this->assign('p9_SAF',$this->p9_SAF);
            $this->assign('pa_MP',$pa_MP);
            $this->assign('pd_FrpId',$pd_FrpId);
            $this->assign('pr_NeedResponse',$pr_NeedResponse);
            $this->assign('hmac',$hmac);
            $this->assign('reqURL_onLine',$this->reqURL_onLine);

            $this->display();
         }

         public function wxpay(){
            //var_dump($this->p0_Cmd);die;
            $p2_Order                   = $_GET['order'];
            $dingdan = M('dingdan')->where("id = $p2_Order")->find();
            $p3_Amt                     = $dingdan['zongjia'];
            $p4_Cur                     = "CNY";
            $p5_Pid                     = "cardbuy";
            $p6_Pcat                    = "cardbuy";
            $p7_Pdesc                   = "cardbuy";
            $p8_Url                     = "http://".$_SERVER['HTTP_HOST']."/index.php/Home/Yongchun/NotifyUrl";
            $pa_MP                      = $_REQUEST['payorderid'];
            $pd_FrpId                   = 'WEIXIN';
            $pr_NeedResponse    = "1";

            $hmac = $this->getReqHmacString($p2_Order,$p3_Amt,$p4_Cur,$p5_Pid,$p6_Pcat,$p7_Pdesc,$p8_Url,$pa_MP,$pd_FrpId,$pr_NeedResponse);
            $this->assign('p0_Cmd',$this->p0_Cmd);
            $this->assign('p1_MerId',$this->p1_MerId);
            $this->assign('p2_Order',$p2_Order);
            $this->assign('p3_Amt',$p3_Amt);
            $this->assign('p4_Cur',$p4_Cur);
            $this->assign('p5_Pid',$p5_Pid);
            $this->assign('p6_Pcat',$p6_Pcat);
            $this->assign('p7_Pdesc',$p7_Pdesc);
            $this->assign('p8_Url',$p8_Url);
            $this->assign('p9_SAF',$this->p9_SAF);
            $this->assign('pa_MP',$pa_MP);
            $this->assign('pd_FrpId',$pd_FrpId);
            $this->assign('pr_NeedResponse',$pr_NeedResponse);
            $this->assign('hmac',$hmac);
            $this->assign('reqURL_onLine',$this->reqURL_onLine);

            $this->display();
         }
		 
		 
         public function qqpay(){
            //var_dump($this->p0_Cmd);die;
            $p2_Order                   = $_GET['order'];
            $dingdan = M('dingdan')->where("id = $p2_Order")->find();
            $p3_Amt                     = $dingdan['zongjia'];
            $p4_Cur                     = "CNY";
            $p5_Pid                     = "cardbuy";
            $p6_Pcat                    = "cardbuy";
            $p7_Pdesc                   = "cardbuy";
            $p8_Url                     = "http://".$_SERVER['HTTP_HOST']."/index.php/Home/Yongchun/NotifyUrl";
            $pa_MP                      = $_REQUEST['payorderid'];
            $pd_FrpId                   = 'QQCODE';
            $pr_NeedResponse    = "1";

            $hmac = $this->getReqHmacString($p2_Order,$p3_Amt,$p4_Cur,$p5_Pid,$p6_Pcat,$p7_Pdesc,$p8_Url,$pa_MP,$pd_FrpId,$pr_NeedResponse);
            $this->assign('p0_Cmd',$this->p0_Cmd);
            $this->assign('p1_MerId',$this->p1_MerId);
            $this->assign('p2_Order',$p2_Order);
            $this->assign('p3_Amt',$p3_Amt);
            $this->assign('p4_Cur',$p4_Cur);
            $this->assign('p5_Pid',$p5_Pid);
            $this->assign('p6_Pcat',$p6_Pcat);
            $this->assign('p7_Pdesc',$p7_Pdesc);
            $this->assign('p8_Url',$p8_Url);
            $this->assign('p9_SAF',$this->p9_SAF);
            $this->assign('pa_MP',$pa_MP);
            $this->assign('pd_FrpId',$pd_FrpId);
            $this->assign('pr_NeedResponse',$pr_NeedResponse);
            $this->assign('hmac',$hmac);
            $this->assign('reqURL_onLine',$this->reqURL_onLine);

            $this->display();
         }
		 
		 
		 		 
		 
         public function tenpay(){
            //var_dump($this->p0_Cmd);die;
            $p2_Order                   = $_GET['order'];
            $dingdan = M('dingdan')->where("id = $p2_Order")->find();
            $p3_Amt                     = $dingdan['zongjia'];
            $p4_Cur                     = "CNY";
            $p5_Pid                     = "cardbuy";
            $p6_Pcat                    = "cardbuy";
            $p7_Pdesc                   = "cardbuy";
            $p8_Url                     = "http://".$_SERVER['HTTP_HOST']."/index.php/Home/Yongchun/NotifyUrl";
            $pa_MP                      = $_REQUEST['payorderid'];
            $pd_FrpId                   = 'TENPAY';
            $pr_NeedResponse    = "1";

            $hmac = $this->getReqHmacString($p2_Order,$p3_Amt,$p4_Cur,$p5_Pid,$p6_Pcat,$p7_Pdesc,$p8_Url,$pa_MP,$pd_FrpId,$pr_NeedResponse);
            $this->assign('p0_Cmd',$this->p0_Cmd);
            $this->assign('p1_MerId',$this->p1_MerId);
            $this->assign('p2_Order',$p2_Order);
            $this->assign('p3_Amt',$p3_Amt);
            $this->assign('p4_Cur',$p4_Cur);
            $this->assign('p5_Pid',$p5_Pid);
            $this->assign('p6_Pcat',$p6_Pcat);
            $this->assign('p7_Pdesc',$p7_Pdesc);
            $this->assign('p8_Url',$p8_Url);
            $this->assign('p9_SAF',$this->p9_SAF);
            $this->assign('pa_MP',$pa_MP);
            $this->assign('pd_FrpId',$pd_FrpId);
            $this->assign('pr_NeedResponse',$pr_NeedResponse);
            $this->assign('hmac',$hmac);
            $this->assign('reqURL_onLine',$this->reqURL_onLine);

            $this->display();
         }
         public function notifyurl(){
            $return = $this->getCallBackValue($r0_Cmd, $r1_Code, $r2_TrxId, $r3_Amt, $r4_Cur, $r5_Pid, $r6_Order, $r7_Uid, $r8_MP, $r9_BType, $hmac);
            $bRet = $this->CheckHmac($r0_Cmd, $r1_Code, $r2_TrxId, $r3_Amt, $r4_Cur, $r5_Pid, $r6_Order, $r7_Uid, $r8_MP, $r9_BType, $hmac);
            //var_dump($_REQUEST);die;
            if ($bRet) {
                if ($r1_Code == "1") {
                    #   需要比较返回的金额与商家数据库中订单的金额是否相等，只有相等的情况下才认为是交易成功.
                    #   并且需要对返回的处理进行事务控制，进行记录的排它性处理，在接收到支付结果通知后，判断是否进行过业务逻辑处理，不要重复进行业务逻辑处理，防止对同一条交易重复发货的情况发生.

                    if ($r9_BType == "1") {
                        $out_trade_no=$r6_Order;
                        $where['id']=$r6_Order;
                        $chkorder = M('dingdan')->where($where)->find();
                        if($chkorder['status'] == 0){
                            $data['status']=1;
                            M('dingdan')->where($where)->save($data);
                            $dlist  = M('dingdan')->find($out_trade_no);
                            $name = $dlist['goodname'];
                            $shuliang = $dlist['shuliang'];
                            $jiage = $dlist['zongjia'];
                            $dingdanhao = $dlist['dingdanhao'];
                            $gid = $dlist['gid'];
                            $glist = M('good')->find($gid);
                            if($glist['fakaleixin'] == '卡密发卡'){
                                $newkucun = $glist['kucun']-$shuliang;
                                M('good')->where("id=$gid")->setField('kucun',$newkucun);
                                $klist =  M('kami')->where("status=0 and g_id=$gid")->limit($shuliang)->order('rand()')->select();
                                foreach ($klist as $key=>$v)
                                {
                                    $arr[] = $v['id'];
                                    $cdk[] = $v['cdk'];
                                }
                                $where['id'] =  array('in',$arr);
                                M('kami')->where($where)->setField("status",1);
                                M('kami')->where($where)->setField("d_id",$out_trade_no);
                            }else{
                                session('gid',$gid);
                            }
                            session('cdk',$cdk);
                            session('name',$name);
                            session('shuliang',$shuliang);
                            session('zongjia',$jiage);
                            session('gid',$gid);
                            session('dingdanhao',$dingdanhao);
                            $this->redirect('Index/buyend');
                        }else{
                            $this->redirect('Index/buyend');
                            //$this->error('该订单已支付！','Home/Index/buyend');
                        }
                        //echo "交易成功";
                        //echo  "<br />在线支付页面返回";
                        $ob = @new HandleOrders_Model($r6_Order, $r3_Amt, 1);
                        $ob -> updateOrderStatusForBank();
                        //Redirect('../../result.htm?orderid=' . $r6_Order);
                    } elseif ($r9_BType == "2") {
                        #如果需要应答机制则必须回写流,以success开头,大小写不敏感.
                        echo "success";
                        $ob = @new HandleOrders_Model($r6_Order, $r3_Amt, 1);
                        $ob -> updateOrderStatusForBank();
                    }
                }

            }
         }


#签名函数生成签名串
public function getReqHmacString($p2_Order, $p3_Amt, $p4_Cur, $p5_Pid, $p6_Pcat, $p7_Pdesc, $p8_Url, $pa_MP, $pd_FrpId, $pr_NeedResponse) {
    global $p0_Cmd;
    global $p9_SAF;
    global $p1_MerId, $merchantKey;

    #进行签名处理，一定按照文档中标明的签名顺序进行
    $sbOld = "";
    #加入业务类型
    $sbOld = $sbOld . $this->p0_Cmd;
    #加入商户编号
    $sbOld = $sbOld . $this->p1_MerId;
    #加入商户订单号
    $sbOld = $sbOld . $p2_Order;
    #加入支付金额
    $sbOld = $sbOld . $p3_Amt;
    #加入交易币种
    $sbOld = $sbOld . $p4_Cur;
    #加入商品名称
    $sbOld = $sbOld . $p5_Pid;
    #加入商品分类
    $sbOld = $sbOld . $p6_Pcat;
    #加入商品描述
    $sbOld = $sbOld . $p7_Pdesc;
    #加入商户接收支付成功数据的地址
    $sbOld = $sbOld . $p8_Url;
    #加入送货地址标识
    $sbOld = $sbOld . $this->p9_SAF;
    #加入商户扩展信息
    $sbOld = $sbOld . $pa_MP;
    #加入支付通道编码
    $sbOld = $sbOld . $pd_FrpId;
    #加入是否需要应答机制
    $sbOld = $sbOld . $pr_NeedResponse;
    return $this->HmacMd5($sbOld, $this->merchantKey);

}

public function getCallbackHmacString($r0_Cmd, $r1_Code, $r2_TrxId, $r3_Amt, $r4_Cur, $r5_Pid, $r6_Order, $r7_Uid, $r8_MP, $r9_BType) {

    global $p1_MerId, $merchantKey;

    #取得加密前的字符串
    $sbOld = "";
    #加入商家ID
    $sbOld = $sbOld . $this->p1_MerId;
    #加入消息类型
    $sbOld = $sbOld . $r0_Cmd;
    #加入业务返回码
    $sbOld = $sbOld . $r1_Code;
    #加入交易ID
    $sbOld = $sbOld . $r2_TrxId;
    #加入交易金额
    $sbOld = $sbOld . $r3_Amt;
    #加入货币单位
    $sbOld = $sbOld . $r4_Cur;
    #加入产品Id
    $sbOld = $sbOld . $r5_Pid;
    #加入订单ID
    $sbOld = $sbOld . $r6_Order;
    #加入用户ID
    $sbOld = $sbOld . $r7_Uid;
    #加入商家扩展信息
    $sbOld = $sbOld . $r8_MP;
    #加入交易结果返回类型
    $sbOld = $sbOld . $r9_BType;

    return $this->HmacMd5($sbOld, $this->merchantKey);

}

#   取得返回串中的所有参数
public function getCallBackValue(&$r0_Cmd, &$r1_Code, &$r2_TrxId, &$r3_Amt, &$r4_Cur, &$r5_Pid, &$r6_Order, &$r7_Uid, &$r8_MP, &$r9_BType, &$hmac) {
    $r0_Cmd = isset($_REQUEST['r0_Cmd']) ? $_REQUEST['r0_Cmd'] : '';
    $r1_Code = isset($_REQUEST['r1_Code']) ? $_REQUEST['r1_Code'] : '';
    $r2_TrxId = isset($_REQUEST['r2_TrxId']) ? $_REQUEST['r2_TrxId'] : '';
    $r3_Amt = isset($_REQUEST['r3_Amt']) ? $_REQUEST['r3_Amt'] : '';
    $r4_Cur = isset($_REQUEST['r4_Cur']) ? $_REQUEST['r4_Cur'] : '';
    $r5_Pid = isset($_REQUEST['r5_Pid']) ? $_REQUEST['r5_Pid'] : '';
    $r6_Order = isset($_REQUEST['r6_Order']) ? $_REQUEST['r6_Order'] : '';
    $r7_Uid = isset($_REQUEST['r7_Uid']) ? $_REQUEST['r7_Uid'] : '';
    $r8_MP = isset($_REQUEST['r8_MP']) ? $_REQUEST['r8_MP'] : '';
    $r9_BType = isset($_REQUEST['r9_BType']) ? $_REQUEST['r9_BType'] : '';
    $hmac = isset($_REQUEST['hmac']) ? $_REQUEST['hmac'] : '';

    return null;
}

public function CheckHmac($r0_Cmd, $r1_Code, $r2_TrxId, $r3_Amt, $r4_Cur, $r5_Pid, $r6_Order, $r7_Uid, $r8_MP, $r9_BType, $hmac) {
    if ($hmac == $this->getCallbackHmacString($r0_Cmd, $r1_Code, $r2_TrxId, $r3_Amt, $r4_Cur, $r5_Pid, $r6_Order, $r7_Uid, $r8_MP, $r9_BType))
        return true;
    else
        return false;
}

public function HmacMd5($data, $key) {
    // RFC 2104 HMAC implementation for php.
    // Creates an md5 HMAC.
    // Eliminates the need to install mhash to compute a HMAC
    // Hacked by Lance Rushing(NOTE: Hacked means written)

    //需要配置环境支持iconv，否则中文参数不能正常处理
    $key = iconv("GB2312", "UTF-8", $key);
    $data = iconv("GB2312", "UTF-8", $data);

    $b = 64;
    // byte length for md5
    if (strlen($key) > $b) {
        $key = pack("H*", md5($key));
    }
    $key = str_pad($key, $b, chr(0x00));
    $ipad = str_pad('', $b, chr(0x36));
    $opad = str_pad('', $b, chr(0x5c));
    $k_ipad = $key ^ $ipad;
    $k_opad = $key ^ $opad;

    return md5($k_opad . pack("H*", md5($k_ipad . $data)));
}



/**/
 }
 ?>