项目遇到多商户支付,稍微改了下微信sdk的东西,实现功能
public function pay(){ header("Content-Type:text/html;charset=UTF-8"); $get = I('get.'); if(empty($get['phone'])){ echo "<script language='javascript'>alert('系统错误!'); window.location.href='https://wake.caiguayun.com/home/shopid/".session('nowshopid').'/personnel/'.session('nowpersonnel').";</script>";die; } vendor('Weixin.WxPayJsApiPay'); vendor('Weixin.WxPayApi'); vendor('Weixin.log'); vendor('Weixin.WxPayException'); $Activity = $this->Activity_model->where(array('shopid'=>session('nowshopid'),'status'=>1))->find(); $data = array( 'order' =>'wake'.time().rand(1111,9999), 'price' => $Activity['price'], 'phone' => $get['phone'], 'openid' =>session('openid'), 'shopid' =>session('nowshopid'), 'name' =>session('nickname'), 'is_pay' => 0, 'addtime' => time(), 'sales' => 1, 'user_name' => $get['user_name'], 'form' =>$Activity['id'], 'personnel' =>session('nowpersonnel'), ); $res = M('order')->add($data); $tools = new JsApiPay(); self::UnifiedOrder($data); $this->display(""); } public function UnifiedOrder($data) { $input = new WxPayUnifiedOrder(); $input->SetBody('挖客宝'); $input->SetAttach(1); $input->SetNonce_str(md5(time())); $input->SetOut_trade_no($data['order']); //$input->SetTotal_fee($now_price*100); $input->SetTotal_fee($data['price']*100); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetNotify_url($this->notify); $input->SetTrade_type("JSAPI"); $tools = new JsApiPay(); $input->SetOpenid(session('openid')); $config = new WxPayConfig($appid,$MCHID,$newkeys,$AppSecret,$SignTypes); // $config = new WxPayConfig('wxa8f4172d1811fbfa','1525939371','abcd86864f2dca38c2299090bb65acbc','149892d4e4454fb05dd40cace035237c',"HMAC-SHA256"); $order = WxPayApi::unifiedOrder($config, $input); $jsApiParameters = $tools->GetJsApiParameters($order,$config); session('jsApiParameters',$jsApiParameters); }
<script> function onBridgeReady(){ WeixinJSBridge.invoke( 'getBrandWCPayRequest', <?php echo $_SESSION['jsApiParameters'];?>, function(res){ /* if(res.err_msg == "get_brand_wcpay_request:ok" ) {} // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。*/ // WeixinJSBridge.log(res.err_msg); //alert(res.err_code+res.err_desc+res.err_msg); if(res.err_msg == "get_brand_wcpay_request:ok"){ window.location.href='https://wake.caiguayun.com/home/index/index/shopid/'+ <?php echo session('nowshopid') ? session('nowshopid') : 0;?>+'/personnel/'+ <?php echo session('nowpersonnel')? session('nowpersonnel') : 0;?>; }else if(res.err_msg == "get_brand_wcpay_request:cancel"){ window.location.href='https://wake.caiguayun.com/home/index/index/shopid/'+<?php echo session('nowshopid')? session('nowshopid') : 0;?> +'/personnel/'+ <?php echo session('nowpersonnel')? session('nowpersonnel') : 0;?>; }else{ } } ); } if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } }else{ onBridgeReady(); } </script>
链接:https://pan.baidu.com/s/1PyR3XBdKrvli12tFx6W19w
提取码:1lyy
public function pay(){ header("Content-Type:text/html;charset=UTF-8"); $get = I('get.'); if(empty($get['phone'])){ echo "<script language='javascript'>alert('系统错误!'); window.location.href='https://wake.caiguayun.com/home/shopid/".session('nowshopid').'/personnel/'.session('nowpersonnel').";</script>";die; } vendor('Weixin.WxPayJsApiPay'); vendor('Weixin.WxPayApi'); vendor('Weixin.log'); vendor('Weixin.WxPayException'); $Activity = $this->Activity_model->where(array('shopid'=>session('nowshopid'),'status'=>1))->find(); $data = array( 'order' =>'wake'.time().rand(1111,9999), 'price' => $Activity['price'], 'phone' => $get['phone'], 'openid' =>session('openid'), 'shopid' =>session('nowshopid'), 'name' =>session('nickname'), 'is_pay' => 0, 'addtime' => time(), 'sales' => 1, 'user_name' => $get['user_name'], 'form' =>$Activity['id'], 'personnel' =>session('nowpersonnel'), ); $res = M('order')->add($data); $tools = new JsApiPay(); self::UnifiedOrder($data);
$this->display(""); }
public function UnifiedOrder($data) { $input = new WxPayUnifiedOrder(); $input->SetBody('挖客宝'); $input->SetAttach(1); $input->SetNonce_str(md5(time())); $input->SetOut_trade_no($data['order']); //$input->SetTotal_fee($now_price*100); $input->SetTotal_fee($data['price']*100); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetNotify_url($this->notify); $input->SetTrade_type("JSAPI"); $tools = new JsApiPay(); $input->SetOpenid(session('openid')); $config = new WxPayConfig('wx9903dba5a4b35192','1521791431','QAZWSXedcRFV12345tgbnhyujmUJMki8','052b463e5e74a81ae404064c8b138df9',"HMAC-SHA256"); // $config = new WxPayConfig('wxa8f4172d1811fbfa','1525939371','abcd86864f2dca38c2299090bb65acbc','149892d4e4454fb05dd40cace035237c',"HMAC-SHA256"); $order = WxPayApi::unifiedOrder($config, $input); $jsApiParameters = $tools->GetJsApiParameters($order,$config); session('jsApiParameters',$jsApiParameters); }