有關微信公眾號和H5支付的一些記錄


最近項目里面需要做公眾號和H5支付的功能,根據自己的體驗,整理了一下,做個記錄。

首先我解釋一下,為什么有公眾號支付還要做H5支付?因為不確定每個用戶在公眾號上打開網站,所以另外做了H5支付。

以下是官方的解釋:

H5支付是指商戶在微信客戶端外的移動端網頁展示商品或服務,用戶在前述頁面確認使用微信支付時,商戶發起本服務呼起微信客戶端進行支付。

主要用於觸屏版的手機瀏覽器請求微信支付的場景。可以方便的從外部瀏覽器喚起微信支付。

提醒:H5支付不建議在APP端使用,如需要在APP中使用微信支付,請接APP支付,文檔詳見微信支付開發文檔

既然是公眾號支付,准備工作要做好(下載的SDK是V3版本):

  必須是認證的服務號才能支付,詳細接口權限見官方文檔:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433401084。

  1.登陸公眾號:

  

  2.

  

  3.登陸商戶平台(pay.weixin.qq.com)

    在開發配中設置如下:

    H5支付直接填寫服務器的域名即可。

    JSAPI授權目錄這里需要注意,如果支付授權目錄沒有設置正確,在請求JSAPI時,會提示“-1當前頁面的url未注冊”的錯誤。” 

      首先要看你支付的當前頁面URL,

      比如是:http://www.xxx.com/wxpay/jsapi.php

        你就必須填寫:http://www.xxx.com/wxpay/

      假如是:http://www.xxx.com/wxpay/order/id/56.html

        你就必須寫: http://www.xxx.com/wxpay/order/id/    

      假如是:http://www.xxx.com/wxpay/order?id=56

        你就必須寫:http://www.xxx.com/wxpay/order/       

  

下面是代碼部分(沒有自己封裝代碼,直接用官方的示例代碼進行修改)

  下載好官方的sdk,我是放到網站的根目錄,看網上有把SDK進行集成封裝好的,大家可自行搜索。

  1.在lib----->下的Wxpay.Config.php文件中,填好APPID、MCHID、KEY(操作密碼)、APPSECRET(公眾帳號secert)

  設置證書

  

  在配置文件的最后最好加上回調地址(當時我沒有設置報錯了,想不起來是什么錯誤了,沒及時記錄……)

    

  2.寫一個驗證token的方法(代碼是網上找的,順便找了一下有關token的登陸介紹:http://blog.csdn.net/resilient/article/details/72673222)

 1 <?php
 2     /**
 3      * wechat php test
 4      */
 5     //define your token
 6     define("TOKEN", "shendai");
 7     $wechatObj = new wechatCallbackapiTest();
 8     $wechatObj->valid();
 9 
10     class wechatCallbackapiTest
11     {
12         public function valid()
13         {
14             $echoStr = $_GET["echostr"];
15             //valid signature , option
16             if ($this->checkSignature())
17             {
18                 echo $echoStr;
19                 exit;
20             }
21         }
22         public function responseMsg()
23         {
24             //get post data, May be due to the different environments
25             $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
26             //extract post data
27             if (!empty($postStr))
28             {
29                 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
30                 $fromUsername = $postObj->FromUserName;
31                 $toUsername = $postObj->ToUserName;
32                 $keyword = trim($postObj->Content);
33                 $time = time();
34                 $textTpl = "<xml>
35                             <ToUserName><![CDATA[%s]]></ToUserName>
36                             <FromUserName><![CDATA[%s]]></FromUserName>
37                             <CreateTime>%s</CreateTime>
38                             <MsgType><![CDATA[%s]]></MsgType>
39                             <Content><![CDATA[%s]]></Content>
40                             <FuncFlag>0</FuncFlag>
41                             </xml>";
42                 if (!empty($keyword))
43                 {
44                     $msgType = "text";
45                     $contentStr = "Welcome to wechat world!";
46                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
47                     echo $resultStr;
48                 }
49                 else
50                 {
51                     echo "Input something...";
52                 }
53             }
54             else
55             {
56                 echo "";
57                 exit;
58             }
59         }
60         private function checkSignature()
61         {
62             $signature = $_GET["signature"];
63             $timestamp = $_GET["timestamp"];
64             $nonce = $_GET["nonce"];
65             $token = TOKEN;
66             $tmpArr = array($token, $timestamp, $nonce);
67             sort($tmpArr);
68             $tmpStr = implode($tmpArr);
69             $tmpStr = sha1($tmpStr);
70             if ($tmpStr == $signature)
71             {
72                 return true;
73             }
74             else
75             {
76                 return false;
77             }
78         }
79 
80 
81     }
82 
83 ?>
View Code

  3.公眾號支付文件jsapi.php,只用示例代碼進行了修改

  1 <?php
  2     ini_set('date.timezone','Asia/Shanghai');
  3     //error_reporting(E_ERROR);
  4     require_once "$wx_root/lib/WxPay.Api.php";
  5     require_once "WxPay.JsApiPay.php";
  6     require_once 'log.php';
  7 
  8     //初始化日志
  9     $logHandler= new CLogFileHandler("$wx_root/logs/".date('Y-m-d').'.log');
 10     $log = Log::Init($logHandler, 15);
 11 
 12     //打印輸出數組信息
 13     function printf_info($data)
 14     {
 15         foreach($data as $key=>$value){
 16             echo "<font color='#00ff55;'>$key</font> : $value <br/>";
 17         }
 18     }
 19 
 20     //①、獲取用戶openid
 21     $tools = new JsApiPay();
 22     $openId = $tools->GetOpenid();
 23 
 24     //②、統一下單
 25     $input = new WxPayUnifiedOrder();
 26     $input->SetBody("xxx充值");
 27     $input->SetAttach("xxx");
 28     $input->SetOut_trade_no($orderNo); //訂單號
 29     $input->SetTotal_fee($trade['fee']); //充值的金額
 30     $input->SetTime_start(date("YmdHis"));
 31     $input->SetTime_expire(date("YmdHis", time() + 600));
 32     $input->SetGoods_tag("xxx充值");
 33     $input->SetNotify_url("http://xxx.xxx.com/xxx/notify");//支付成功的回調地址
 34     $input->SetTrade_type("JSAPI"); //支付類型
 35     $input->SetOpenid($openId);
 36     $order = WxPayApi::unifiedOrder($input);
 37 //    echo '<font color="#f00"><b>xxx支付頁面</b></font><br/>';
 38 //    printf_info($order);
 39     $jsApiParameters = $tools->GetJsApiParameters($order);
 40 
 41     //獲取共享收貨地址js函數參數
 42     $editAddress = $tools->GetEditAddressParameters();
 43 
 44     //③、在支持成功回調通知中處理成功之后的事宜,見 notify.php
 45     /**
 46      * 注意:
 47      * 1、當你的回調地址不可訪問的時候,回調通知會失敗,可以通過查詢訂單來確認支付是否成功
 48      * 2、jsapi支付時需要填入用戶openid,WxPay.JsApiPay.php中有獲取openid流程 (文檔可以參考微信公眾平台“網頁授權接口”,
 49      * 參考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
 50      */
 51 ?>
 52 
 53 <html>
 54 <head>
 55     <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
 56     <meta name="viewport" content="width=device-width, initial-scale=1"/>
 57     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 58     <title>xxx-支付</title>
 59     <style>
 60         html, body {
 61             margin: 0;
 62             width: 100%;
 63             height: 100%;
 64         }
 65         .modal {
 66             position: relative;
 67             width: 100%;
 68             height: 100%;
 69             background-color: rgba(0, 0, 0, .6);
 70         }
 71         .dialog {
 72             position: absolute;
 73             top: 50%;
 74             left: 50%;
 75             width: 210px;
 76             transform: translate(-50%, -50%);
 77             background-color: #fff;
 78             border-radius: 4px;
 79         }
 80         .dialog .head {
 81             height: 36px;
 82             line-height: 36px;
 83             border-bottom: 1px solid #1aad19;
 84             text-align: center;
 85             font-size: 14px;
 86         }
 87 
 88         .dialog .paymoney {
 89             padding: 14px;
 90 
 91         }
 92         .dialog .paymoney span {
 93             display: block;
 94             font-size: 14px;
 95             text-align: center;
 96         }
 97         .dialog .paymoney b {
 98             display: block;
 99             font-size: 28px;
100             text-align: center;
101         }
102         .dialog .paymoney a {
103             background: #1aad19;
104             padding: 8px 0;
105             margin-top:5px;
106             display: block;
107             border-radius: 4px;
108             text-decoration: none;
109             text-align: center;
110             font-size: 18px;
111             color: #fff;
112         }
113         .dialog .btn-pay {
114 
115         }
116     </style>
117     <script type="text/javascript">
118         //調用微信JS api 支付
119         function jsApiCall()
120         {
121             WeixinJSBridge.invoke(
122                 'getBrandWCPayRequest',
123                 <?php echo $jsApiParameters; ?>,
124                 function(res){
125                     if(res.err_msg == 'get_brand_wcpay_request:ok')
126                     {
127                         window.location.href = '支付成功后跳轉地址';
128                     }
129                 }
130             );
131         }
132 
133         function callpay()
134         {
135             if (typeof WeixinJSBridge == "undefined"){
136                 if( document.addEventListener ){
137                     document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
138                 }else if (document.attachEvent){
139                     document.attachEvent('WeixinJSBridgeReady', jsApiCall);
140                     document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
141                 }
142             }else{
143                 jsApiCall();
144             }
145         }
146     </script>
147     <script type="text/javascript">
148         //獲取共享地址
149         //    function editAddress()
150         //    {
151         //        WeixinJSBridge.invoke(
152         //            'editAddress',
153         //            <?php //echo $editAddress; ?>//,
154         //            function(res){
155         //                var value1 = res.proviceFirstStageName;
156         //                var value2 = res.addressCitySecondStageName;
157         //                var value3 = res.addressCountiesThirdStageName;
158         //                var value4 = res.addressDetailInfo;
159         //                var tel = res.telNumber;
160         //
161         //                alert(value1 + value2 + value3 + value4 + ":" + tel);
162         //            }
163         //        );
164         //    }
165         //
166         //    window.onload = function(){
167         //        if (typeof WeixinJSBridge == "undefined"){
168         //            if( document.addEventListener ){
169         //                document.addEventListener('WeixinJSBridgeReady', editAddress, false);
170         //            }else if (document.attachEvent){
171         //                document.attachEvent('WeixinJSBridgeReady', editAddress);
172         //                document.attachEvent('onWeixinJSBridgeReady', editAddress);
173         //            }
174         //        }else{
175         //            editAddress();
176         //        }
177         //    };
178 
179     </script>
180 </head>
181 <body>
182 <div class="modal">
183     <div class="dialog">
184         <div class="head">
185             支付
186         </div>
187         <div class="paymoney">
188             <span>xxx有限公司</span>
189             <b>¥<?php echo $trade['fee']; ?></b>
190 
191             <a type="button" onclick="callpay()" >立即支付</a>
192         </div>
193 
194     </div>
195 </div>
196  <br/>
197 </body>
198 </html>
View Code

  4.在notify.php文件中寫成功支付后的業務代碼

 1 //重寫回調處理函數
 2 public function NotifyProcess($data, &$msg)
 3 {
 4     Log::DEBUG("call back:" . json_encode($data));
 5     $notfiyOutput = array();
 6         
 7     if(!array_key_exists("transaction_id", $data)){
 8         $msg = "輸入參數不正確";
 9         return false;
10     }
11     //查詢訂單,判斷訂單真實性
12     if(!$this->Queryorder($data["transaction_id"])){
13         $msg = "訂單查詢失敗";
14         return false;
15     }
16 
17     $out_trade_no = $data["out_trade_no"]; //訂單號
18 
19     //判斷該筆訂單是否在商戶網站中已經做過處理
20     $trade = db('訂單表')->where('orderNo',$out_trade_no)->find();
21     //請務必判斷請求時的total_fee、seller_id與通知時獲取的total_fee、seller_id為一致的
22     if ($data['mch_id'] == '商戶號' && $data['total_fee'] == $trade['fee'])
23     {
24         db('訂單表')->where('id',$trade['id'])->update(['feeStatus'=>1]);
25 
26     }
27     else
28     {
29        return false;
30     }
31 
32     return true;
33 }        
View Code

  5.編寫支付接口回調接口(有重復代碼,懶得去改了)

 1  /*微信客戶端支付*/
 2     public function pay()
 3     {
 4         if(input('param.total') > 0)
 5         {
 6             //添加一個訂單
 7            當用戶點擊支付按鈕時添加一條訂單記錄
 8            ...
 9 
10             $wx_root = './Wxpay';
11             $file = './Wxpay/example/jsapi.php';
12             require_once $file;
13         }
14     }
15     /*H5支付*/
16     public function h5Pay()
17     {
18         if(input('param.total') > 0)
19         {
20             //添加一個訂單
21            當用戶點擊支付按鈕時添加一條訂單記錄
22            ...
23 
24             $wx_root = './Wxpay';
25             $file = './Wxpay/example/mweb.php';
26             require_once $file;
27         }
28 
29     }
30     /*微信支付回調*/
31     public function notify()
32     {
33         $wx_root = './Wxpay';
34         $file = './Wxpay/example/notify.php';
35         require_once $file;
36     }
View Code

頁面測試出現的問題:

  1.文件加載錯誤,所以支付接口里面已寫成了絕對路徑。(在控制器里面,include文件是相對於網站的根目錄的);

  2.出現’-1頁面未注冊‘的報錯,查各種資料,說是支付授權目錄的問題,填寫規則上面已有說明;

  3.Notice: Use of undefined constant CURLOP_TIMEOUT - assumed 'CURLOP_TIMEOUT 報錯 ,解決如下:

  (在example的JsApi.php的大概99行,修改如下:)

//        curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

 

  4.H5支付出現curl出錯,錯誤碼60,解決如下:

    在lib--->wxpay.api.php大概538行處,修改如下代碼

 1 //        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
 2 //        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗
 3         if(stripos($url,"https://")!==FALSE){
 4             curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
 5             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 6             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 7         }
 8         else
 9         {
10             curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
11             curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗
12         }
View Code

 

 

H5支付集成到SDK里面:

  1.在lib---->Wxpay.data.php大概753行中,添加代碼

 

 1  /**
 2      * 設置trade_type=MWEB,此參數必傳。該字段用於上報支付的場景信息,針對H5支付有三種場景,請根據對應場景上報
 3      * @param string $value
 4      **/
 5 
 6     public function SetScene_info($value)
 7     {
 8         $this->values['scene_info'] = $value;
 9     }
10     /**
11      * 判斷trade_type=MWEB,此參數必傳。該字段用於上報支付的場景信息,針對H5支付有三種場景,請根據對應場景上報
12      * @return true 或 false
13      **/
14     public function IsScene_info()
15     {
16         return array_key_exists('scene_info', $this->values);
17     }
View Code

 

  2.在example--->NativePay.php中添加代碼:

 1 /**
 2      *
 3      * 生成H5支付url,支付url有效期為2小時
 4      * @param UnifiedOrderInput $input
 5      */
 6     public function GetH5PayUrl($input)
 7     {
 8         if($input->GetTrade_type() == "MWEB")
 9         {
10             $result = WxPayApi::unifiedOrder($input);
11             return $result;
12         }
13     }
View Code

  3.在example中添加一個mweb.php文件:

  1 <?php
  2     ini_set('date.timezone','Asia/Shanghai');
  3 //error_reporting(E_ERROR);
  4     require_once "$wx_root/lib/WxPay.Api.php";
  5     require_once "WxPay.NativePay.php";
  6     require_once 'log.php';
  7     /**
  8      * 流程:
  9      * 1、調用統一下單,取得mweb_url,通過mweb_url調起微信支付中間頁
 10      * 2、用戶在微信支付收銀台完成支付或取消支付
 11      * 3、支付完成之后,微信服務器會通知支付成功
 12      * 4、在支付成功通知中需要查單確認是否真正支付成功(見:notify.php)
 13      */
 14     $input = new WxPayUnifiedOrder();
 15     $input->SetBody("xxx充值");
 16     $input->SetAttach("xxx");
 17     $input->SetOut_trade_no($orderNo);
 18     $input->SetTotal_fee($trade['fee']);
 19     $input->SetTime_start(date("YmdHis"));
 20     $input->SetTime_expire(date("YmdHis", time() + 600));
 21     $input->SetGoods_tag("xxx充值");
 22     $input->SetNotify_url("http://xxx.com/notify"); //支付完成之后,微信服務器通知的回調地址
 23     $input->SetTrade_type("MWEB"); //特別注意這里的類型
 24     $input->SetScene_info('{"h5_info": {"type":"Wap","wap_url": "服務器的域名","wap_name": "xxx充值"}}'); H5支付的特定格式
 25     $url = new NativePay();
 26     $result = $url->GetH5PayUrl($input);
 27     $url = $result["mweb_url"].'&redirect_url=http%3A%2F%2Fxxx.com'; //后面加的&redirect_url=http%3A%2F%2Fxxx.com是支付成功后的跳轉頁面,$url為生成的支付鏈接url
 28 ?>
 29 <html>
 30 <head>
 31     <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
 32     <meta name="viewport" content="width=device-width, initial-scale=1"/>
 33     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 34     <title>xxx-支付</title>
 35     <style>
 36         html, body {
 37             margin: 0;
 38             width: 100%;
 39             height: 100%;
 40         }
 41         .modal {
 42             position: relative;
 43             width: 100%;
 44             height: 100%;
 45             background-color: rgba(0, 0, 0, .6);
 46         }
 47         .dialog {
 48             position: absolute;
 49             top: 50%;
 50             left: 50%;
 51             width: 210px;
 52             transform: translate(-50%, -50%);
 53             background-color: #fff;
 54             border-radius: 4px;
 55         }
 56         .dialog .head {
 57             height: 36px;
 58             line-height: 36px;
 59             border-bottom: 1px solid #1aad19;
 60             text-align: center;
 61             font-size: 14px;
 62         }
 63 
 64         .dialog .paymoney {
 65             padding: 14px;
 66 
 67         }
 68         .dialog .paymoney span {
 69             display: block;
 70             font-size: 14px;
 71             text-align: center;
 72         }
 73         .dialog .paymoney b {
 74             display: block;
 75             font-size: 28px;
 76             text-align: center;
 77         }
 78         .dialog .paymoney a {
 79             background: #1aad19;
 80             padding: 8px 0;
 81             margin-top:5px;
 82             display: block;
 83             border-radius: 4px;
 84             text-decoration: none;
 85             text-align: center;
 86             font-size: 18px;
 87             color: #fff;
 88         }
 89         .dialog .btn-pay {
 90 
 91         }
 92     </style>
 93 </head>
 94 <body>
 95 <div class="modal">
 96     <div class="dialog">
 97         <div class="head">
 98             支付
 99         </div>
100         <div class="paymoney">
101             <span>xxx有限公司</span>
102             <b>¥<?php echo $trade['fee']; ?></b>
103             <a type="button" href="<?php echo $url; ?>">立即支付</a>
104         </div>
105 
106     </div>
107 </div>
108 </body>
109 </html>
View Code

 

關於H5支付的一個坑,生成的支付鏈接不能在控制器中直接跳轉,試過好多次都報‘參數格式錯誤,請聯系商家的字樣’,后來把鏈接寫到頁面的A標簽就好了。

順便在網上找到的H5報錯的資料,http://blog.csdn.net/paymm/article/details/73188596。

 

ps:在網上看到了一個封裝好的H5支付的方法,親測有用:

  1 <?php
  2     /**
  3      *非微信瀏覽器 調用微信h5支付
  4      */
  5 
  6     namespace wstmart\wap\controller;
  7 
  8     use think\Controller;
  9     use think\Loader;
 10     use think\Request;
 11     use think\Url;
 12     use think\Config;
 13     use think\Page;
 14 
 15     class Cc extends Base
 16     {
 17         /**
 18          * @return array  下單方法
 19          */
 20         public function wxpaymoney(Request $request)
 21         {
 22             $param = $request->param();
 23             if (!empty($param))
 24             {
 25                 $param = json_decode(json_encode(simplexml_load_string($param, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
 26                 echo '-------';
 27                 p($param);
 28                 echo '*******';
 29             }
 30 
 31             $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';  //下單地址
 32             $appid = 'xxx';//公眾號appid
 33             $appsecret = 'xxx';
 34             $mch_id = 'xxx';//商戶平台id
 35             $nonce_str = 'qyzf' . rand(100000, 999999);//隨機數
 36             $out_trade_no = 'wap' . date('YmdHis') . rand(100000, 999999);
 37             $ip = $this->getClientIp();
 38             $scene_info = '{"h5_info": {"type":"Wap","app_name": "project info","package_name": "wap訂單微信支付"}}';
 39 //        $appidInfo=$this->getTokens($appid,$appsecret);
 40 //        echo '=======';
 41 //        p($appidInfo);
 42 //        echo '*******';die;
 43 //        $openid=$appidInfo['openid'];
 44             $total_fee = 1;
 45             $trade_type = 'MWEB';
 46             $attach = 'wap支付測試';
 47             $body = 'H5支付測試';
 48             $notify_url = 'http://paysdk.weixin.qq.com/example/notify.php';
 49             $arr = [
 50                 'appid'     => $appid, 'mch_id' => $mch_id, 'nonce_str' => $nonce_str, 'out_trade_no' => $out_trade_no, 'spbill_create_ip' => $ip, 'scene_info' => $scene_info, //            'openid'=>$openid,
 51                 'total_fee' => $total_fee, 'trade_type' => $trade_type, 'attach' => $attach, 'body' => $body, 'notify_url' => $notify_url
 52             ];
 53             $sign = $this->getSign($arr);
 54             //<openid>'.$openid.'</openid>
 55             $data = '<xml>  
 56                      <appid>' . $appid . '</appid>  
 57                      <attach>' . $attach . '</attach>  
 58                      <body>' . $body . '</body>  
 59                      <mch_id>' . $mch_id . '</mch_id>  
 60                      <nonce_str>' . $nonce_str . '</nonce_str>  
 61                      <notify_url>' . $notify_url . '</notify_url>  
 62                      <out_trade_no>' . $out_trade_no . '</out_trade_no>  
 63                      <spbill_create_ip>' . $ip . '</spbill_create_ip>  
 64                      <total_fee>' . $total_fee . '</total_fee>  
 65                      <trade_type>' . $trade_type . '</trade_type>  
 66                      <scene_info>' . $scene_info . '</scene_info>  
 67                      <sign>' . $sign . '</sign>  
 68                      </xml>';
 69             $result = $this->https_request($url, $data);
 70             echo '====================';
 71             var_dump($result);
 72             echo '*******************';
 73             //禁止引用外部xml實體
 74             libxml_disable_entity_loader(true);
 75             $result_info = json_decode(json_encode(simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
 76             p($result_info);
 77             echo '<a href="' . $result_info['mweb_url'] . '">測試微信支付</a>';
 78             die;
 79         }
 80 
 81         /**
 82          *curl請求
 83          */
 84         private function https_request($url, $data = null)
 85         {
 86             p($data);
 87             $curl = curl_init();
 88             curl_setopt($curl, CURLOPT_URL, $url);
 89             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 90             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
 91             if (!empty ($data))
 92             {
 93                 curl_setopt($curl, CURLOPT_POST, 1);
 94                 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 95             }
 96             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 97             $output = curl_exec($curl);
 98             curl_close($curl);
 99 
100             return $output;
101         }
102 
103         /**
104          * @return 獲取ip
105          */
106         private function getClientIp()
107         {
108             $cip = 'unknown';
109             if ($_SERVER['REMOTE_ADDR'])
110             {
111                 $cip = $_SERVER['REMOTE_ADDR'];
112             }
113             elseif (getenv($_SERVER['REMOTE_ADDR']))
114             {
115                 $cip = getenv($_SERVER['REMOTE_ADDR']);
116             }
117 
118             return $cip;
119         }
120 
121         /**
122          * @return 異步通知
123          */
124         public function wxnotify(Request $request)
125         {
126             Loader::import('payment.wxwap.PayNotifyCallBacks');
127             $notify = new \PayNotifyCallBacks();
128             $notify->Handle(false);
129             die;
130         }
131 
132         /**
133          * @param Request
134          */
135         public function wxcallback(Request $request)
136         {
137             $data = $request->param();
138             file_put_contents('./testwx1.txt', '888888888888888' . $data['orderid'] . '-----' . $data['transaction_id'] . '******');
139 
140         }
141 
142         /**
143          *     作用:格式化參數,簽名過程需要使用
144          */
145         public function formatBizQueryParaMap($paraMap, $urlencode)
146         {
147 //        var_dump($paraMap);//die;
148             $buff = "";
149             ksort($paraMap);
150             foreach ($paraMap as $k => $v)
151             {
152                 if ($urlencode)
153                 {
154                     $v = urlencode($v);
155                 }
156                 //$buff .= strtolower($k) . "=" . $v . "&";
157                 $buff .= $k . "=" . $v . "&";
158             }
159             $reqPar = '';
160             if (strlen($buff) > 0)
161             {
162                 $reqPar = substr($buff, 0, strlen($buff) - 1);
163             }
164             p($reqPar);//die;
165 
166             return $reqPar;
167         }
168 
169         /**
170          *     作用:生成簽名
171          */
172         public function getSign($Obj)
173         {
174             p($Obj);//die;
175             foreach ($Obj as $k => $v)
176             {
177                 $Parameters[$k] = $v;
178             }
179             //簽名步驟一:按字典序排序參數
180             ksort($Parameters);
181             $String = $this->formatBizQueryParaMap($Parameters, false);
182             //echo '【string1】'.$String.'</br>';
183             //簽名步驟二:在string后加入KEY   5a02bd8ecxxxxxxxxxxxxc1aae7d199  這里的秘鑰是 商戶平台設置的一定要改不然報簽名錯誤
184             $String = $String . "&key=ShenDai8787com1313ShenDai027SDWL";
185             //echo "【string2】".$String."</br>";
186             //簽名步驟三:MD5加密
187             $String = md5($String);
188             //echo "【string3】 ".$String."</br>";
189             //簽名步驟四:所有字符轉為大寫
190             $result_ = strtoupper($String);
191 
192             //echo "【result】 ".$result_."</br>";
193             return $result_;
194         }
195 
196 
197         private function httpGet($url)
198         {
199             $curl = curl_init();
200             curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
201             curl_setopt($curl, CURLOPT_TIMEOUT, 500);
202             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
203             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
204             curl_setopt($curl, CURLOPT_URL, $url);
205 
206             $res = curl_exec($curl);
207             curl_close($curl);
208 
209             return $res;
210         }
211     }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM