-
-
- (掃碼模式二,微信瀏覽器內部支付(jsapi模式),手機端瀏覽器支付 (H5支付或者wap支付)
-
-
-
-
-
- 先進微信https://pay.weixin.qq.com/index.php做參數配置工作
-
-
-
- 產品中心--開發者配置
-
-
-
- A 支付授權目錄(支付發起的頁面url 必須精確到最後一個目錄/下 必須有 / 否則不行)
-
-
-
- http://www.qq.com/abd/sdfs/1.html 就要在後臺配置http://www.qq.com/abd/sdfs/
-
-
-
- 如果url是http://www.qq.com/abd/sdfs?1.html 那就把url改成 http://www.qq.com/abd/sdfs/?1.html 否則驗證不通過無法調用微信支付
-
-
-
- B 回調地址 就是付款完成了微信會回調你的url告訴你處理結果(我這裏沒用上,微信死活不回調我,我就js輪詢主動去查詢微信那邊的訂單狀態做處理)
-
-
-
- C H5支付需要開通 填寫頂級域名即可
-
-
-
-
-
- 1.掃碼模式二
-
-
-
- //微信支付
-
-
-
- $wxpay = new Wxpay();
-
-
-
- $order_info['body']='畫品購買';
-
-
-
- $order_info['order_number']=$order_id;
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $order_info['money']=$money;//畫品總價
-
-
-
- }else{
-
-
-
- $order_info['money']='1';
-
-
-
- }
-
-
-
- $order_info['id']=$order_id;
-
-
-
- $result=$wxpay->do_nativepay($order_info);
-
-
-
- if($result){
-
-
-
- //這個if裏面的就是自己定義的內容,比如生成二維碼之後你要插入一條數據進入數據庫之類 上面
-
-
-
- // 組裝是必須要有的 下面這個是自己自定義的
-
-
-
- $data['url']=$result;
-
-
-
- $data['order_id']=$order_id;
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $data['money']=$money;//畫品總價
-
-
-
- }else{
-
-
-
- $data['money']='1';
-
-
-
- }
-
-
-
-
-
- $data['pay_type']=2;
-
-
-
- }
-
-
-
- return ajax_success($data); 這裏包含微信返回的支付信息
-
-
-
-
-
- 下面是在魔板和js裏面進行的處理:
-
-
-
-
-
- <img src="http://paysdk.weixin.qq.com/example/qrcode.php?data='+data.info.url+'" class="resize" style="width:70%;"> 這裏就生成了二維碼可以掃碼付款
-
-
-
-
-
- setTimeout(function(){
-
-
-
- getoneorder(data.info.order_id);
-
-
-
- },2000);
-
-
-
- //微信支付輪詢js開始
-
-
-
- function getoneorder(pr){
-
-
-
- if(pr) {
-
-
-
- $.ajax({
-
-
-
- url: '/wechat/checkwxorderstatus', //這個方法是查詢微信那邊的訂單處理狀結果 因爲的回調微信沒有觸發.
-
-
-
- data: {order_id: pr},
-
-
-
- type: 'GET',
-
-
-
- // dataType : 'json',
-
-
-
- success: function (msg) {
-
-
-
- if (msg.status==1) {
-
-
-
- // istry = false;
-
-
-
- layer.msg('支付成功');
-
-
-
- window.location.href='/member';
-
-
-
- }else if (msg.status==2 ) {
-
-
-
- layer.msg('支付失敗');
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }else{ //暫未支付
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }
-
-
-
-
-
- },
-
-
-
- error: function () {
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }
-
-
-
- })
-
-
-
- }
-
-
-
- }
-
-
-
- //微信支付輪詢js結束
-
-
-
-
-
- //查詢微信的訂單狀態並處理訂單業務邏輯返回處理結果
-
-
-
- public function checkWxOrderStatus(Request $request){
-
-
-
- $data = $request->all();
-
-
-
- $order_id=$data['order_id'];
-
-
-
- $wxpay=new Wxpay();
-
-
-
- $result=$wxpay->query_order($order_id);
-
-
-
- if(!empty($result['trade_state'])&&$result['trade_state']=='SUCCESS'){//去微信查詢訂單狀態是ok
-
-
-
- $doorder=Order::doOrder($order_id,2); //傳訂單id處理訂單
-
-
-
- if($doorder=='success'){
-
-
-
- return array(
-
-
-
- 'trade_state'=>'SUCCESS',
-
-
-
- 'order_status'=>'SUCCESS',
-
-
-
- 'status'=>'1' //支付成功
-
-
-
- );
-
-
-
- }else{
-
-
-
- return array(
-
-
-
- 'trade_state'=>'SUCCESS',
-
-
-
- 'order_status'=>'FAIL',
-
-
-
- 'status'=>'2' //付款成功但是沒改訂單狀態
-
-
-
- );
-
-
-
- }
-
-
-
- }else{
-
-
-
- return array(
-
-
-
- 'trade_state'=>'FAIL',
-
-
-
- 'order_status'=>'FAIL',
-
-
-
- 'status'=>'3' //付款不成功訂單也沒修改
-
-
-
- );
-
-
-
- }
-
-
-
- }
-
-
-
-
-
-
-
- 2.微信內部瀏覽器支付(jsapi模式)和手機瀏覽器模式參見下面合併在一起判斷處理的,我這裏是是根據UA進行判斷用戶進入哪種支付模式,如果是微信打開的進入第一個否則進入第二個H5支付
-
-
-
- //手機UA判斷
-
-
-
- if($this->isWeixin()){ //微信內部瀏覽器,這個方法網上很多自己搜索
-
-
-
- $wxpay = new Mobilewxpay();
-
-
-
- $order_info['uatype']='2';
-
-
-
- $order_info['body']='畫品購買';
-
-
-
- $order_info['opend_id']=Auth::user()->openid;
-
-
-
- $order_info['order_number']=$parent_order_id;
-
-
-
- //區分生產和測試環境的支付金額
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $order_info['money']=$total_money;
-
-
-
- }else{
-
-
-
- $order_info['money']='1';
-
-
-
- }
-
-
-
- $order_info['id']=$parent_order_id;
-
-
-
- $order_info['pay_type']=2;
-
-
-
- //未付款的訂單纔可以進行付款
-
-
-
- $wxpay->unifiedOrder($order_info);
-
-
-
- $resulturl = $wxpay->GetJsApiParameters($order_info);
-
-
-
- if (!empty($resulturl)){
-
-
-
- //這一步是把微信返回的json格式的數據轉換成數組
-
-
-
- $resulturl = json_decode($resulturl,true);
-
-
-
- }
-
-
-
- $order_info['resulturl']= $resulturl;
-
-
-
- }else{ //手機自帶瀏覽器
-
-
-
- $wxpay = new Mobilewxpay();
-
-
-
- $order_info['uatype']='1';
-
-
-
- $order_info['body']='畫品購買';
-
-
-
- $order_info['order_number']=$parent_order_id;
-
-
-
- //區分生產和測試環境的支付金額
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $order_info['money']=$total_money;
-
-
-
- }else{
-
-
-
- $order_info['money']='1';
-
-
-
- }
-
-
-
- $order_info['id']=$parent_order_id;
-
-
-
- //未付款的訂單纔可以進行付款
-
-
-
- $h5result= $wxpay->MobileH5Pay($order_info);
-
-
-
- $order_info['pay_type']=2;
-
-
-
- $order_info['h5resulturl']=!empty($h5result['mweb_url'])?$h5result['mweb_url']:'';
-
-
-
- }
-
-
-
- return ajax_success($order_info);
-
-
-
- 提交訂單的通過上面的ajax返回數據
-
-
-
- 下面的js裏面的處理:
-
-
-
- if(data.info.pay_type==2){//微信支付
-
-
-
- if(data.info.uatype == 1){//手機自帶瀏覽器H5支付走這裏
-
-
-
- var h5resulturlss=data.info.h5resulturl.replace(/amp;/, "");//這裏進入了一個坑IOS手機上url會被轉義這裏是處理這個情況的,請注意支付跳轉的url裏面的&是否被轉義成& 切記大坑. 如果是賦值在a鏈接的一個鏈接的話不會有這種情況發生.如果變量賦值給js的話會有這種情況.
-
-
-
- var h5resulturl12=decodeURI(h5resulturlss);
-
-
-
- window.open(h5resulturl12);//這裏是走微信支付協議調用微信彈框
-
-
-
- }else{//微信內部瀏覽器jsapi支付走這裏
-
-
-
- callpay(data.info.resulturl);//這裏是呼喚微信支付彈框
-
-
-
- }
-
-
-
- setTimeout(function(){
-
-
-
- getoneorder(data.info.id);
-
-
-
- },2000);
-
-
-
- return false;
-
-
-
- }
-
-
-
-
-
-
-
-
-
- //調用微信JS api 支付
-
-
-
- function jsApiCall(pr)
-
-
-
- {
-
-
-
- WeixinJSBridge.invoke(
-
-
-
- 'getBrandWCPayRequest',
-
-
-
- pr,
-
-
-
- function(res){
-
-
-
- WeixinJSBridge.log(res.err_msg);
-
-
-
- // alert(res.err_code+res.err_desc+res.err_msg);
-
-
-
- }
-
-
-
- );
-
-
-
- }
-
-
-
- function callpay(pr)
-
-
-
- {
-
-
-
- if (typeof WeixinJSBridge == "undefined"){
-
-
-
- if( document.addEventListener ){
-
-
-
- document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
-
-
-
- }else if (document.attachEvent){
-
-
-
- document.attachEvent('WeixinJSBridgeReady', jsApiCall);
-
-
-
- document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
-
-
-
- }
-
-
-
- }else{
-
-
-
- jsApiCall(pr);
-
-
-
- }
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
- //微信支付輪詢js開始
-
-
-
- function getoneorder(pr){
-
-
-
- if(pr) {
-
-
-
- $.ajax({
-
-
-
- url: '/wechat/checkwxorderstatus',//這裏還是查詢微信那邊的訂單狀態做判斷處理
-
-
-
- data: {order_id: pr},
-
-
-
- type: 'GET',
-
-
-
- // dataType : 'json',
-
-
-
- success: function (msg) {
-
-
-
- if (msg.status==1) {
-
-
-
- // istry = false;
-
-
-
- layer.open({
-
-
-
- content: '<div class="demo-tip-box"><div class="tip-text">支付成功</div></div>'
-
-
-
- ,skin: 'msg'
-
-
-
- ,time: 2 //2秒後自動關閉
-
-
-
- });
-
-
-
- // window.setTimeout('window.location.href="/buy/paysuccess?type=1&oid='+pr+'"',2000);
-
-
-
- window.setTimeout('window.location.href="/member/myorder"',2000);
-
-
-
- }else if (msg.status==2 ) {
-
-
-
- layer.open({
-
-
-
- content: '<div class="demo-tip-box"><div class="tip-text">支付失敗</div></div>'
-
-
-
- ,skin: 'msg'
-
-
-
- ,time: 2 //2秒後自動關閉
-
-
-
- });
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }else{ //暫未支付
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }
-
-
-
-
-
- },
-
-
-
- error: function () {
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }
-
-
-
- })
-
-
-
- }
-
-
-
- }
-
-
-
- //微信支付輪詢js結束
-
-
-
- $(document).ready(function(){
-
-
-
- getoneorder();
-
-
-
- });
-
-
-
-
-
-
-
- 附上我的支付類文件 (掃描支付模式二使用的)
-
-
-
- <?php
-
-
-
- namespace App\Tools\weixin;
-
-
-
- /**
-
-
-
-
- 微信支付
-
-
-
-
-
-
- */
-
-
-
-
-
- class Wxpay {
-
-
-
-
-
-
-
- //掃碼支付模式一
-
-
-
- public function do_nativepay1($productId){
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.NativePay.php';
-
-
-
- $notify = new NativePay();
-
-
-
- $url1 = $notify->GetPrePayUrl($productId);
-
-
-
- return $url1;
-
-
-
- }
-
-
-
-
-
- //掃描支付模式二
-
-
-
- public function do_nativepay($order_info) {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.NativePay.php';
-
-
-
- $notify = new \NativePay();
-
-
-
- $input = new \WxPayUnifiedOrder();
-
-
-
- $input->SetBody($order_info['body']);
-
-
-
- $input->SetAttach("畫品購買");
-
-
-
- $input->SetOut_trade_no($order_info['id']);
-
-
-
- //$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $input->SetTotal_fee($order_info['money']*100);
-
-
-
- }else{
-
-
-
- $input->SetTotal_fee($order_info['money']);
-
-
-
- }
-
-
-
-
-
- $input->SetTime_start(date("YmdHis"));
-
-
-
- $input->SetTime_expire(date("YmdHis", time() + 600));
-
-
-
- $input->SetGoods_tag("test");
-
-
-
- $input->SetNotify_url('http://artflyer.qq.com/buy/getpay');
-
-
-
- // $input->SetNotify_url(url('http://artflyer.qq.com/wechat/getpay'));
-
-
-
- $input->SetTrade_type("NATIVE");
-
-
-
- $input->SetProduct_id($order_info['id']);
-
-
-
- $result = $notify->GetPayUrl($input);
-
-
-
- $url2 = $result["code_url"];
-
-
-
- return $url2;
-
-
-
- }
-
-
-
-
-
- // 支付付款
-
-
-
- public function do_wxpay($order_info) {
-
-
-
- ini_set('date.timezone', 'Asia/Shanghai');
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.JsApiPay.php';
-
-
-
- //①、獲取用戶openid
-
-
-
- $tools = new \JsApiPay();
-
-
-
- $openId = $tools->GetOpenid();
-
-
-
- //②、統一下單
-
-
-
- $input = new \WxPayUnifiedOrder();
-
-
-
- $input->SetBody($order_info['goods_name']);
-
-
-
- $input->SetAttach('');
-
-
-
- $input->SetOut_trade_no($order_info['order_sn']);
-
-
-
- $input->SetTotal_fee($order_info['order_amount']);
-
-
-
- $input->SetTime_start(date("YmdHis"));
-
-
-
- $input->SetTime_expire(date("YmdHis", time() + 600));
-
-
-
- $input->SetGoods_tag('');
-
-
-
- $input->SetNotify_url(url('http://artflyer.qq.com/wechat/getpay'));
-
-
-
- // $input->SetNotify_url(WxPayConfig::NOTIFY_URL);
-
-
-
- $input->SetTrade_type("JSAPI");
-
-
-
- $input->SetOpenid($openId);
-
-
-
- $order = \WxPayApi::unifiedOrder($input);
-
-
-
- $jsApiParameters = $tools->GetJsApiParameters($order);
-
-
-
- //生成帶參數的同步放回地址
-
-
-
- include_once('../app/Tools/weixin/lib/WxPay.Api.php');
-
-
-
- $url_arr = json_decode($jsApiParameters, TRUE);
-
-
-
- unset($url_arr['signType']);
-
-
-
- unset($url_arr['paySign']);
-
-
-
- $url_arr['order_sn'] = $order_info['order_sn'];
-
-
-
- $notify = new \WxPayResults();
-
-
-
- $notify->FromArray($url_arr);
-
-
-
- $sign = $notify->SetSign();
-
-
-
- $url_arr['sign'] = $sign;
-
-
-
- $return_url = \WxPayConfig::RETURN_URL . '?' . http_build_query($url_arr);
-
-
-
- //支付頁面
-
-
-
- $pay_url = \WxPayConfig::PAY_URL . '?order_sn=' . $order_info['order_sn'];
-
-
-
- include_once 'weixin.html';
-
-
-
- }
-
-
-
-
-
- // 同步回調驗證
-
-
-
- public function do_return($arr = array()) {
-
-
-
- include_once('../app/Tools/weixin/lib/WxPay.Api.php');
-
-
-
- $notify = new \WxPayResults();
-
-
-
- if (empty($arr)) {
-
-
-
- $arr = $_GET;
-
-
-
- }
-
-
-
- $notify->FromArray($arr);
-
-
-
- if ($notify->CheckSign() == true) {
-
-
-
- return TRUE;
-
-
-
- } else {
-
-
-
- return FALSE;
-
-
-
- }
-
-
-
- }
-
-
-
-
-
- // 異步回調驗證
-
-
-
- public function do_notify($xml) {
-
-
-
- include_once('../app/Tools/weixin/lib/WxPay.Api.php');
-
-
-
- $notify = new WxPayResults();
-
-
-
- $notify->FromXml($xml);
-
-
-
- if ($notify->CheckSign() == true) {
-
-
-
- return TRUE;
-
-
-
- } else {
-
-
-
- return FALSE;
-
-
-
- }
-
-
-
- }
-
-
-
-
-
- //xml轉化成數組
-
-
-
- public function FromXml($xml) {
-
-
-
- include_once('../app/Tools/weixin/lib/notify.php');
-
-
-
- $notify = new PayNotifyCallBack();
-
-
-
- return $notify->FromXml($xml);
-
-
-
- }
-
-
-
-
-
- /**
-
-
-
-
- 交易查詢 查詢訂單交易狀態
-
-
-
-
-
-
-
- @param string $order_sn 07073訂單號
-
-
-
-
-
- @return [type] [array]
-
-
-
-
- */
-
-
-
-
-
- public function query_order($order_sn) {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.Api.php';
-
-
-
- if (isset($order_sn) && $order_sn != "") {
-
-
-
- $input = new \WxPayOrderQuery();
-
-
-
- $input->SetOut_trade_no($order_sn);
-
-
-
- $result_arr = \WxPayApi::orderQuery($input);
-
-
-
- }
-
-
-
- return $result_arr;
-
-
-
- }
-
-
-
-
-
- // 退款
-
-
-
- public function wx_refund($order_info) {
-
-
-
- require_once "../app/Tools/weixin/lib/WxPay.Api.php";
-
-
-
-
-
- $input = new WxPayRefund();
-
-
-
- $input->SetTransaction_id($order_info['transaction_id']);
-
-
-
- $input->SetTotal_fee($order_info['total_fee']);
-
-
-
- $input->SetRefund_fee($order_info['refund_fee']);
-
-
-
- $input->SetOut_refund_no($order_info['batch_no']);
-
-
-
- $input->SetOp_user_id(WxPayConfig::MCHID);
-
-
-
- $res = WxPayApi::refund($input);
-
-
-
- return $res;
-
-
-
- }
-
-
-
-
-
- // 退款狀態查詢
-
-
-
- public function wx_refund_query($order_info) {
-
-
-
- require_once '../app/Tools/weixin/lib/WxPay.Api.php';
-
-
-
-
-
- $input = new WxPayRefundQuery();
-
-
-
- $input->SetTransaction_id($order_info['transaction_id']);
-
-
-
- $res = WxPayApi::refundQuery($input);
-
-
-
-
-
- return $res;
-
-
-
- }
-
-
-
-
-
- // 支付付款
-
-
-
- public function do_wappay($order_info) {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.WapPay.php';
-
-
-
- // 統一下單
-
-
-
- $input = new \WxPayUnifiedOrder();
-
-
-
- $input->SetBody($order_info['goods_name']);
-
-
-
- $input->SetAttach("");
-
-
-
- $input->SetOut_trade_no($order_info['order_sn']);
-
-
-
- $input->SetTotal_fee($order_info['order_amount']);
-
-
-
- $input->SetTime_start(date("YmdHis"));
-
-
-
- $input->SetTime_expire(date("YmdHis", time() + 600));
-
-
-
- $input->SetGoods_tag("");
-
-
-
- $input->SetNotify_url(\WxPayConfig::NOTIFY_URL);
-
-
-
- $input->SetTrade_type("WAP");
-
-
-
- $input->SetProduct_id($order_info['id']);
-
-
-
-
-
- $wxpay = new \WapPay();
-
-
-
- // 預支付訂單信息
-
-
-
- $prepay_res = $wxpay->GetPrepayInfo($input);
-
-
-
- var_dump($prepay_res);
-
-
-
- $wxpay->GetWapPayUrl($prepay_res['prepayid']);
-
-
-
- }
-
-
-
-
-
- // 微信登錄
-
-
-
- public function wxlogin() {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.JsApiPay.php';
-
-
-
-
-
- // 獲取用戶openid
-
-
-
- $tools = new JsApiPay();
-
-
-
- $data = $tools->GetOpenData();
-
-
-
- return $data;
-
-
-
- }
-
-
-
-
-
- // 獲得微信用戶信息
-
-
-
- public function wxuser($access_token, $openid) {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.JsApiPay.php';
-
-
-
-
-
- // 獲取用戶openid
-
-
-
- $tools = new JsApiPay();
-
-
-
- $data = $tools->getUserInfo($access_token, $openid);
-
-
-
- return $data;
-
-
-
- }
-
-
-
-
-
- //將xml轉爲array
-
-
-
- public function xmlToArray($xml){
-
-
-
- $array_data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
-
-
-
- return $array_data;
-
-
-
- }
-
-
-
-
-
- /**
-
-
-
-
- 作用:array轉xml
-
-
-
-
- */
-
-
-
- function arrayToXml($arr)
-
-
-
- {
-
-
-
- $xml = "<xml>";
-
-
-
- foreach ($arr as $key=>$val)
-
-
-
- {
-
-
-
- if (is_numeric($val))
-
-
-
- {
-
-
-
- $xml.="<".$key.">".$val."</".$key.">";
-
-
-
-
-
- }
-
-
-
- else
-
-
-
- $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
-
-
-
- }
-
-
-
- $xml.="</xml>";
-
-
-
- return $xml;
-
-
-
- }
-
-
-
- /*
-
-
-
-
- 移動端H5支付
-
-
-
-
- */
-
-
-
- public function mobile_h5pay(){
-
-
-
- //①、獲取用戶openid
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.JsApiPay.php';
-
-
-
- $tools = new \JsApiPay();
-
-
-
- $openId = $tools->GetOpenid();
-
-
-
- //②、統一下單
-
-
-
- $input = new \WxPayUnifiedOrder();
-
-
-
- $input->SetBody("test");
-
-
-
- $input->SetAttach("test");
-
-
-
- $input->SetOut_trade_no(\WxPayConfig::MCHID.date("YmdHis"));
-
-
-
- $input->SetTotal_fee("1");
-
-
-
- $input->SetTime_start(date("YmdHis"));
-
-
-
- $input->SetTime_expire(date("YmdHis", time() + 600));
-
-
-
- $input->SetGoods_tag("test");
-
-
-
- $input->SetNotify_url("http://www1.qq.cn/wechat/getpay");
-
-
-
- $input->SetTrade_type("MWEB"); //H5支付的交易類型爲MWEB
-
-
-
- // $input->SetOpenid($openId); //這裏要註釋掉
-
-
-
- $order = \WxPayApi::unifiedOrder($input);
-
-
-
- return $order;
-
-
-
- }
-
-
-
-
-
- }
-
-
-
-
-
- 下面這個是H5支付和微信內部瀏覽器支付使用的類文件
-
-
-
- <?php
-
-
-
- namespace App\Tools\weixin;
-
-
-
- header('Content-type:text/html;charset=utf-8');
-
-
-
- /**微信支付
-
-
-
-
- Class Mobilewxpay
-
-
-
-
-
- @package App\Tools\weixin
-
-
-
-
- */
-
-
-
- class Mobilewxpay {
-
-
-
- public $APPID = 'wx811a9fc';
-
-
-
- public $MCHID = '14912';
-
-
-
- public $KEY = '68e4b3eae5b171f17fc5';
-
-
-
- public $APPSECRET = '6bce72a750664d61a3b47c';
-
-
-
- /**
-
-
-
- *
-
-
-
-
- 拼接簽名字符串
-
-
-
-
-
- @param array $urlObj
-
-
-
-
-
- @return 返回已經拼接好的字符串
-
-
-
-
- */
-
-
-
- function ToUrlParams($urlObj)
-
-
-
- {
-
-
-
- $buff = "";
-
-
-
- foreach ($urlObj as $k => $v)
-
-
-
- {
-
-
-
- if($k != "sign"){
-
-
-
- $buff .= $k . "=" . $v . "&";
-
-
-
- }
-
-
-
- }
-
-
-
-
-
- $buff = trim($buff, "&");
-
-
-
- return $buff;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 統一下單,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
-
-
-
-
-
- appid、mchid、spbill_create_ip、nonce_str不需要填入
-
-
-
-
-
- @param WxPayUnifiedOrder $inputObj
-
-
-
-
-
- @param int $timeOut
-
-
-
-
-
- @throws WxPayException
-
-
-
-
-
- @return 成功時返回,其他拋異常
-
-
-
-
- */
-
-
-
- function unifiedOrder($order_info,$timeOut = 6)
-
-
-
- {
-
-
-
- $datas = array();
-
-
-
- $datas['body'] = '購買';
-
-
-
- $datas['out_trade_no'] = $order_info['id'];//訂單號
-
-
-
- $datas['total_fee'] = $order_info['money'];
-
-
-
- $datas['time_start'] = date("YmdHis");
-
-
-
- $datas['time_expire'] = date("YmdHis", time() + 600);
-
-
-
- $datas['notify_url'] = 'http://artwap.qq.com/wechat/getpay';//沒有用上這個url微信死活不回調我,用的js輪詢查詢支付和訂單狀態
-
-
-
- $datas['trade_type'] = 'JSAPI';//微信內部瀏覽器支付
-
-
-
- // $datas['openid'] = $order_info['opend_id'];
-
-
-
- $datas['openid'] = 'o_rTz0bCFLXRx5XSmE_ijh9xaC3U';
-
-
-
- $datas['appid'] = $this->APPID;//公衆賬號ID
-
-
-
- $datas['mch_id'] = $this->MCHID;//商戶號
-
-
-
- $datas['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];//ip
-
-
-
- $datas['nonce_str'] = $this->getNonceStr();//隨機字符串
-
-
-
- //簽名步驟一:按字典序排序參數
-
-
-
- ksort($datas);
-
-
-
- $string = $this->ToUrlParamss($datas);
-
-
-
- //簽名步驟二:在string後加入KEY
-
-
-
- $string = $string . "&key=".$this->KEY;
-
-
-
- //簽名步驟三:MD5加密
-
-
-
- $string = md5($string);
-
-
-
- //簽名步驟四:所有字符轉爲大寫
-
-
-
- $result = strtoupper($string);
-
-
-
- $datas['sign'] = $result;//簽名
-
-
-
- $xml = $this->ToXml($datas);
-
-
-
- $response = $this->postXmlCurl($xml, $url, false, $timeOut);
-
-
-
- $data = $this->FromXml($response);
-
-
-
- return $data;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 產生隨機字符串,不長於32位
-
-
-
-
-
- @param int $length
-
-
-
-
-
- @return 產生的隨機字符串
-
-
-
-
- */
-
-
-
- function getNonceStr($length = 32)
-
-
-
- {
-
-
-
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
-
-
-
- $str ="";
-
-
-
- for ( $i = 0; $i < $length; $i++ ) {
-
-
-
- $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
-
-
-
- }
-
-
-
- return $str;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 輸出xml字符
-
-
-
-
- **/
-
-
-
- function ToXml($datas)
-
-
-
- {
-
-
-
- $xml = "<xml>";
-
-
-
- foreach ($datas as $key=>$val)
-
-
-
- {
-
-
-
- if (is_numeric($val)){
-
-
-
- $xml.="<".$key.">".$val."</".$key.">";
-
-
-
- }else{
-
-
-
- $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
-
-
-
- }
-
-
-
- }
-
-
-
- $xml.="</xml>";
-
-
-
- return $xml;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 格式化參數格式化成url參數
-
-
-
-
- */
-
-
-
- function ToUrlParamss($datas)
-
-
-
- {
-
-
-
- $buff = "";
-
-
-
- foreach ($datas as $k => $v)
-
-
-
- {
-
-
-
- if($k != "sign" && $v != "" && !is_array($v)){
-
-
-
- $buff .= $k . "=" . $v . "&";
-
-
-
- }
-
-
-
- }
-
-
-
- $buff = trim($buff, "&");
-
-
-
- return $buff;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 以post方式提交xml到對應的接口url
-
-
-
-
- *
-
-
-
-
- @param string $xml 需要post的xml數據
-
-
-
-
-
- @param string $url url
-
-
-
-
-
- @param bool $useCert 是否需要證書,默認不需要
-
-
-
-
-
- @param int $second url執行超時時間,默認30s
-
-
-
-
- */
-
-
-
- function postXmlCurl($xml, $url, $useCert = false, $second = 30)
-
-
-
- {
-
-
-
- $ch = curl_init();
-
-
-
- //設置超時
-
-
-
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
-
-
-
- curl_setopt($ch,CURLOPT_URL, $url);
-
-
-
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
-
-
-
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//嚴格校驗
-
-
-
- //設置header
-
-
-
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
-
-
-
- //要求結果爲字符串且輸出到屏幕上
-
-
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-
-
-
- //post提交方式
-
-
-
- curl_setopt($ch, CURLOPT_POST, TRUE);
-
-
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
-
-
-
- //運行curl
-
-
-
- $data = curl_exec($ch);
-
-
-
- //返回結果
-
-
-
- curl_close($ch);
-
-
-
- return $data;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 將xml轉爲array
-
-
-
-
-
- @param string $xml
-
-
-
-
- */
-
-
-
- public function FromXml($xml) {
-
-
-
- //將XML轉爲array
-
-
-
- return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 獲取jsapi支付的參數
-
-
-
-
-
- @param array $UnifiedOrderResult 統一支付接口返回的數據
-
-
-
-
-
- @return json數據,可直接填入js函數作爲參數
-
-
-
-
- */
-
-
-
- function GetJsApiParameters($order_info,$timeOut = 6){
-
-
-
-
-
- $UnifiedOrderResult = $this->unifiedOrder($order_info,$timeOut = 6);
-
-
-
- if(!array_key_exists("appid", $UnifiedOrderResult)
-
-
-
- || !array_key_exists("prepay_id", $UnifiedOrderResult)
-
-
-
- || $UnifiedOrderResult['prepay_id'] == "")
-
-
-
- {
-
-
-
- echo $UnifiedOrderResult['err_code_des'];
-
-
-
- exit;
-
-
-
- }
-
-
-
- $da = array();
-
-
-
- $da['appId'] = $UnifiedOrderResult["appid"];
-
-
-
- $timeStamp = time();
-
-
-
- $da['timeStamp'] = "$timeStamp";
-
-
-
- $da['nonceStr'] = $this->getNonceStr();
-
-
-
- $da['package'] = "prepay_id=" . $UnifiedOrderResult['prepay_id'];
-
-
-
- $da['signType'] = 'MD5';
-
-
-
- //簽名步驟一:按字典序排序參數
-
-
-
- ksort($da);
-
-
-
- $string = $this->ToUrlParamss($da);
-
-
-
- //簽名步驟二:在string後加入KEY
-
-
-
- $string = $string . "&key=".$this->KEY;
-
-
-
- //簽名步驟三:MD5加密
-
-
-
- $string = md5($string);
-
-
-
- //簽名步驟四:所有字符轉爲大寫
-
-
-
- $result = strtoupper($string);
-
-
-
- $da['paySign'] = $result;
-
-
-
- $parameters = json_encode($da);
-
-
-
- return $parameters;
-
-
-
- }
-
-
-
- /*
-
-
-
-
- 微信H5相關處理開始
-
-
-
-
- */
-
-
-
- function get_client_ip() {
-
-
-
- static $realip;
-
-
-
- if (isset($_SERVER)) {
-
-
-
- if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
-
-
-
- $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
-
-
-
- } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
-
-
-
- $realip = $_SERVER["HTTP_CLIENT_IP"];
-
-
-
- } else {
-
-
-
- $realip = $_SERVER["REMOTE_ADDR"];
-
-
-
- }
-
-
-
- } else {
-
-
-
- if (getenv("HTTP_X_FORWARDED_FOR")) {
-
-
-
- $realip = getenv("HTTP_X_FORWARDED_FOR");
-
-
-
- } else if (getenv("HTTP_CLIENT_IP")) {
-
-
-
- $realip = getenv("HTTP_CLIENT_IP");
-
-
-
- } else {
-
-
-
- $realip = getenv("REMOTE_ADDR");
-
-
-
- }
-
-
-
- }
-
-
-
- return $realip;
-
-
-
- }
-
-
-
-
-
- //H5支付開始(手機自帶瀏覽器處理)
-
-
-
- function createNoncestr( $length = 32 ){
-
-
-
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
-
-
-
- $str ="";
-
-
-
- for ( $i = 0; $i < $length; $i++ ) {
-
-
-
- $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
-
-
-
- }
-
-
-
- return $str;
-
-
-
- }
-
-
-
- public function getIp(){
-
-
-
- $ip = '';
-
-
-
- if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
-
-
-
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
-
-
-
- }elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
-
-
-
- $ip = $_SERVER['HTTP_CLIENT_IP'];
-
-
-
- }else{
-
-
-
- $ip = $_SERVER['REMOTE_ADDR'];
-
-
-
- }
-
-
-
- $ip_arr = explode(',', $ip);
-
-
-
- return $ip_arr[0];
-
-
-
- }
-
-
-
- public function postXmlCurlh5($xml,$url,$second = 30){
-
-
-
- $ch = curl_init();
-
-
-
- //設置超時
-
-
-
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
-
-
-
- curl_setopt($ch,CURLOPT_URL, $url);
-
-
-
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
-
-
-
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
-
-
-
- //設置header
-
-
-
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
-
-
-
- //要求結果爲字符串且輸出到屏幕上
-
-
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-
-
-
- //post提交方式
-
-
-
- curl_setopt($ch, CURLOPT_POST, TRUE);
-
-
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
-
-
-
- //運行curl
-
-
-
- $data = curl_exec($ch);
-
-
-
- //返回結果
-
-
-
- if($data){
-
-
-
- curl_close($ch);
-
-
-
- return $data;
-
-
-
- }else{
-
-
-
- $error = curl_errno($ch);
-
-
-
- curl_close($ch);
-
-
-
- echo "curl出錯,錯誤碼:$error"."<br>";
-
-
-
- }
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 生成簽名
-
-
-
-
-
- @return 簽名,本函數不覆蓋sign成員變量,如要設置簽名需要調用SetSign方法賦值
-
-
-
-
- */
-
-
-
- protected function MakeSign($arr) {
-
-
-
- //簽名步驟一:按字典序排序參數
-
-
-
- ksort($arr);
-
-
-
- $string = $this->ToUrlParams($arr);
-
-
-
- //簽名步驟二:在string後加入KEY
-
-
-
- $string = $string . "&key=" . $this->KEY;
-
-
-
- //簽名步驟三:MD5加密
-
-
-
- $string = md5($string);
-
-
-
- //簽名步驟四:所有字符轉爲大寫
-
-
-
- $result = strtoupper($string);
-
-
-
- return $result;
-
-
-
- }
-
-
-
-
-
-
-
- public function MobileH5Pay($order_info){
-
-
-
- $headers =array();
-
-
-
- $headers[] ='Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8';
-
-
-
- $headers[] ='Connection: Keep-Alive';
-
-
-
- $headers[] ='Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3';
-
-
-
- $headers[] ='Accept-Encoding: gzip, deflate';
-
-
-
- $headers[] ='User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101Firefox/22.0';
-
-
-
- $appid = $this->APPID; //應用APPID
-
-
-
- $mch_id = $this->MCHID; //微信支付商戶號
-
-
-
- $key = $this->KEY; //微信商戶API密鑰
-
-
-
-
-
- $rand = rand(00000,99999);
-
-
-
- $attach='artflyer.cn';
-
-
-
- $out_trade_no = $order_info['id'];//平臺內部訂單號
-
-
-
- $nonce_str = $this->createNoncestr();//隨機字符串
-
-
-
- $body = "artflyer.cn";//內容
-
-
-
- $total_fee = $order_info['money']; //金額
-
-
-
- $spbill_create_ip = $this->getIp(); //IP
-
-
-
- $trade_type = 'MWEB';//交易類型 因爲是h5支付所以交易類型必須是MWEB
-
-
-
- $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.qq.com/wechat/getpay","wap_name":"artflyer.cn"}}';//場景信息 必要參數
-
-
-
- //以下信息可以不改
-
-
-
- $signA ="appid=$appid&attach=$out_trade_no&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
-
-
-
- $strSignTmp = $signA."&key=$key"; //拼接字符串 注意順序微信有個測試網址 順序按照他的來 直接點下面的校正測試 //包括下面XML 是否正確
-
-
-
- $sign = strtoupper(MD5($strSignTmp)); // MD5 後轉換成大寫
-
-
-
- $post_data = "<xml>
-
-
-
- <appid>$appid</appid>
-
-
-
- <mch_id>$mch_id</mch_id>
-
-
-
- <body>$body</body>
-
-
-
- <out_trade_no>$out_trade_no</out_trade_no>
-
-
-
- <total_fee>$total_fee</total_fee>
-
-
-
- <spbill_create_ip>$spbill_create_ip</spbill_create_ip>
-
-
-
- <notify_url>$notify_url</notify_url>
-
-
-
- <trade_type>$trade_type</trade_type>
-
-
-
- <scene_info>$scene_info</scene_info>
-
-
-
- <attach>$out_trade_no</attach>
-
-
-
- <nonce_str>$nonce_str</nonce_str>
-
-
-
- <sign>$sign</sign>
-
-
-
- </xml>";//拼接成XML 格式
-
-
-
-
-
- $dataxml = $this->postXmlCurlh5($post_data,$url,$headers); //後臺POST微信傳參地址 同時取得微信返回的參數 POST 方法我寫下面了
-
-
-
- $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //將微信返回的XML 轉換成數組
-
-
-
- return $objectxml;
-
-
-
- }
-
-
-
-
-
-
-
- }
-
-
-
- 有問題也可以發郵件給微信技術 [email protected]
-
-
-
- 搞的比較亂別罵...... 有問題可以QQ交流 245864009
-
-
-
laravel下微信支付接入,三種支付模式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.