讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 17.1.4 微信支付基礎類 >

17.1.4 微信支付基礎類

統一下單接口的地址如下。


https:// api.mch.weixin.qq.com/pay/unifiedorder
  

向統一下單接口,POST數據示例如下。


<xml>
    <appid>wx2421b1c4370ec43b</appid>
    <attach>支付測試</attach>
    <body>JSAPI支付測試</body>
    <mch_id>10000100</mch_id>
    <detail><![CDATA[{ "goods_detail":[ { "goods_id":"iphone6s_16G", "wxpay_goods_id":
    "1001", "goods_name":"iPhone6s 16G", "quantity":1, "price":528800, "goods_category":
    "123456", "body":"蘋果手機" }, { "goods_id":"iphone6s_32G", "wxpay_goods_id":
    "1002", "goods_name":"iPhone6s 32G", "quantity":1, "price":608800, "goods_category"
    :"123789", "body":"蘋果手機" } ] }]]></detail>
    <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
    <notify_url>http:// wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php</notify_url>
    <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
    <out_trade_no>1415659990</out_trade_no>
    <spbill_create_ip>14.23.150.211</spbill_create_ip>
    <total_fee>1</total_fee>
    <trade_type>JSAPI</trade_type>
    <sign>0CB01533B8C1EF103065174F50BCA001</sign>
</xml>
  

上述數據的參數說明如表17-2所示。

表17-2 統一下單接口的參數說明

正確創建時,返回的數據示例如下。


<xml>
    <return_code><![CDATA[SUCCESS]]></return_code>
    <return_msg><![CDATA[OK]]></return_msg>
    <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
    <mch_id><![CDATA[10000100]]></mch_id>
    <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>
    <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>
    <result_code><![CDATA[SUCCESS]]></result_code>
    <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>
    <trade_type><![CDATA[JSAPI]]></trade_type>
</xml>
  

上述數據的參數說明如表17-3所示。

表17-3 統一下單接口返回參數說明

下面將列舉微信支付基礎類的實現,這些類包含產生隨機字符串、格式化參數、生成簽名、array轉XML、以POST方式提交XML到對應的接口等功能,是後面實現各種支付場景的前提。

微信支付接口基類的實現代碼如下。


  1 /**
  2  * 所有接口的基類
  3  */
  4 class Common_util_pub
  5 {
  6     function __construct {
  7     }
  8 
  9     function trimString($value)
 10     {
 11         $ret = null;
 12         if (null != $value) 
 13         {
 14             $ret = $value;
 15             if (strlen($ret) == 0) 
 16             {
 17                 $ret = null;
 18             }
 19         }
 20         return $ret;
 21     }
 22     
 23     /**
 24      *     作用:產生隨機字符串,不長於32位
 25      */
 26     public function createNoncestr( $length = 32 ) 
 27     {
 28         $chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
 29         $str ="";
 30         for ( $i = 0; $i < $length; $i++ )  {  
 31             $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
 32         }  
 33         return $str;
 34     }
 35     
 36     /**
 37      *     作用:格式化參數,簽名過程需要使用
 38      */
 39     function formatBizQueryParaMap($paraMap, $urlencode)
 40     {
 41         $buff = "";
 42         ksort($paraMap);
 43         foreach ($paraMap as $k => $v)
 44         {
 45             if($urlencode)
 46             {
 47                $v = urlencode($v);
 48             }
 49             // $buff .= strtolower($k) . "=" . $v . "&";
 50             $buff .= $k . "=" . $v . "&";
 51         }
 52         $reqPar;
 53         if (strlen($buff) > 0) 
 54         {
 55             $reqPar = substr($buff, 0, strlen($buff)-1);
 56         }
 57         return $reqPar;
 58     }
 59     
 60     /**
 61      *     作用:生成簽名
 62      */
 63     public function getSign($Obj)
 64     {
 65         foreach ($Obj as $k => $v)
 66         {
 67             $Parameters[$k] = $v;
 68         }
 69         // 簽名步驟一:按字典序排序參數
 70         ksort($Parameters);
 71         $String = $this->formatBizQueryParaMap($Parameters, false);
 72         // 簽名步驟二:在string後加入key
 73         $String = $String."&key=".WxPayConf_pub::KEY;
 74         // 簽名步驟三:MD5加密
 75         $String = md5($String);
 76         // 簽名步驟四:所有字符轉為大寫
 77         $result_ = strtoupper($String);
 78         return $result_;
 79     }
 80     
 81     /**
 82      *     作用:array轉XML
 83      */
 84     function arrayToXml($arr)
 85     {
 86         $xml = "<xml>";
 87         foreach ($arr as $key=>$val)
 88         {
 89              if (is_numeric($val))
 90              {
 91                  $xml.="<".$key.">".$val."</".$key.">"; 
 92 
 93              }
 94              else
 95                  $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
 96         }
 97         $xml.="</xml>";
 98         return $xml; 
 99     }
100     
101     /**
102      *     作用:將XML轉為array
103      */
104     public function xmlToArray($xml)
105     {        
106         // 將XML轉為array
107         $array_data = json_decode(json_encode(simplexml_load_string($xml, 'Simple-
            XMLElement', LIBXML_NOCDATA)), true);
108         return $array_data;
109     }
110 
111     /**
112      *     作用:以POST方式提交XML到對應的接口URL
113      */
114     public function postXmlCurl($xml,$url,$second=30)
115     {        
116         // 初始化curl        
117            $ch = curl_init;
118         // 設置超時
119         curl_setopt($ch, CURLOPT_TIMEOUT, $second);
120         curl_setopt($ch,CURLOPT_URL, $url);
121         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
122         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
123         // 設置header
124         curl_setopt($ch, CURLOPT_HEADER, FALSE);
125         // 要求結果為字符串且輸出到屏幕上
126         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
127         // POST提交方式
128         curl_setopt($ch, CURLOPT_POST, TRUE);
129         curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
130         // 運行curl
131         $data = curl_exec($ch);
132         // 返回結果
133         if($data)
134         {
135             curl_close($ch);
136             return $data;
137         }
138         else 
139         { 
140             $error = curl_errno($ch);
141             echo "curl出錯,錯誤碼:$error"."<br>"; 
142             echo "<a href='http:// curl.haxx.se/libcurl/c/libcurl-errors.html'>
                錯誤原因查詢</a></br>";
143             curl_close($ch);
144             return false;
145         }
146     }
147 
148     /**
149      *     作用:使用證書,以POST方式提交XML到對應的接口URL
150      */
151     function postXmlSSLCurl($xml,$url,$second=30)
152     {
153         $ch = curl_init;
154         // 超時時間
155         curl_setopt($ch,CURLOPT_TIMEOUT,$second);
156         // 這裡設置代理,如果有的話
157         // curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
158         // curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
159         curl_setopt($ch,CURLOPT_URL, $url);
160         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
161         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
162         // 設置header
163         curl_setopt($ch,CURLOPT_HEADER,FALSE);
164         // 要求結果為字符串且輸出到屏幕上
165         curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
166         // 設置證書
167         // 使用證書:cert 與 key 分別屬於兩個.pem文件
168         // 默認格式為PEM,可以註釋
169         curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
170         curl_setopt($ch,CURLOPT_SSLCERT, WxPayConf_pub::SSLCERT_PATH);
171         // 默認格式為PEM,可以註釋
172         curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
173         curl_setopt($ch,CURLOPT_SSLKEY, WxPayConf_pub::SSLKEY_PATH);
174         // POST提交方式
175         curl_setopt($ch,CURLOPT_POST, true);
176         curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
177         $data = curl_exec($ch);
178         // 返回結果
179         if($data){
180             curl_close($ch);
181             return $data;
182         }
183         else { 
184             $error = curl_errno($ch);
185             echo "curl出錯,錯誤碼:$error"."<br>"; 
186             echo "<a href='http:// curl.haxx.se/libcurl/c/libcurl-errors.html'>
        錯誤原因查詢</a></br>";
187             curl_close($ch);
188             return false;
189         }
190     }
191     
192     /**
193      *     作用:打印數組
194      */
195     function printErr($wording='',$err='')
196     {
197         print_r('<pre>');
198         echo $wording."</br>";
199         var_dump($err);
200         print_r('</pre>');
201     }
202 }
 

請求型接口主要是設置請求參數,生成接口參數XML。


 1 /**
 2  * 請求型接口的基類
 3  */
 4 class Wxpay_client_pub extends Common_util_pub
 5 {
 6     var $parameters;                                        // 請求參數,類型為關聯數組
 7     public $response;                                       // 微信返回的響應
 8     public $result;                                         // 返回參數,類型為關聯數組
 9     var $url;                                               // 接口鏈接
10     var $curl_timeout;                                      // curl超時時間
11 
12     /**
13      *     作用:設置請求參數
14      */
15     function setParameter($parameter, $parameterValue)
16     {
17         $this->parameters[$this->trimString($parameter)] = $this->trimString
       ($parameterValue);
18     }
19 
20     /**
21      *     作用:設置標配的請求參數,生成簽名,生成接口參數XML
22      */
23     function createXml
24     {
25         $this->parameters["appid"] = WxPayConf_pub::APPID;          // 公眾賬號ID
26         $this->parameters["mch_id"] = WxPayConf_pub::MCHID;         // 商戶號
27         $this->parameters["nonce_str"] = $this->createNoncestr; // 隨機字符串
28         $this->parameters["sign"] = $this->getSign($this->parameters);// 
29         $abc =$this->arrayToXml($this->parameters);;
30         return  $abc;
31     }
32 
33     /**
34      *     作用:POST請求XML
35      */
36     function postXml
37     {
38         $xml = $this->createXml;
39         $this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout);
40         return $this->response;
41     }
42 
43     /**
44      *     作用:使用證書POST請求XML
45      */
46     function postXmlSSL
47     {
48         $xml = $this->createXml;
49         $this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout);
50         return $this->response;
51     }
52 
53     /**
54      *     作用:獲取結果,默認不使用證書
55      */
56     function getResult
57     {
58         $this->postXml;
59         $this->result = $this->xmlToArray($this->response);
60         return $this->result;
61     }
62 }
  

響應型接口基類的成員函數包括XML轉成關聯數組、校驗簽名、生成返回的XML參數。


 1 /**
 2  * 響應型接口基類
 3  */
 4 class Wxpay_server_pub extends Common_util_pub
 5 {
 6     public $data;                                // 接收到的數據,類型為關聯數組
 7     var $returnParameters;                        // 返回參數,類型為關聯數組
 8 
 9     /**
10      * 將微信的請求XML轉換成關聯數組,以方便數據處理
11      */
12     function saveData($xml)
13     {
14         $this->data = $this->xmlToArray($xml);
15     }
16 
17     function checkSign
18     {
19         $tmpData = $this->data;
20         unset($tmpData['sign']);
21         $sign = $this->getSign($tmpData);        // 本地簽名
22         if ($this->data['sign'] == $sign) {
23             return TRUE;
24         }
25         return FALSE;
26     }
27 
28     /**
29      * 獲取微信的請求數據
30      */
31     function getData
32     {
33         return $this->data;
34     }
35 
36     /**
37      * 設置返回微信的XML數據
38      */
39     function setReturnParameter($parameter, $parameterValue)
40     {
41         $this->returnParameters[$this->trimString($parameter)] = $this->trimString
       ($parameterValue);
42     }
43 
44     /**
45      * 生成接口參數XML
46      */
47     function createXml
48     {
49         return $this->arrayToXml($this->returnParameters);
50     }
51 
52     /**
53      * 將XML數據返回微信
54      */
55     function returnXml
56     {
57         $returnXml = $this->createXml;
58         return $returnXml;
59     }
60 }
  

在上述接口的基礎上,統一支付接口主要設置接口鏈接、檢測參數,以及生成prepay_id。


 1 /**
 2  * 統一支付接口類
 3  */
 4 class UnifiedOrder_pub extends Wxpay_client_pub
 5 {
 6     function __construct
 7     {
 8         // 設置接口鏈接
 9         $this->url = "https:// api.mch.weixin.qq.com/pay/unifiedorder";
10         // 設置curl超時時間
11         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
12     }
13 
14     /**
15      * 生成接口參數XML
16      */
17     function createXml
18     {
19         try
20         {
21             // 檢測必填參數
22             if($this->parameters["out_trade_no"] == null)
23             {
24                 throw new SDKRuntimeException("缺少統一支付接口必填參數out_trade_no!".
                   "<br>");
25             }elseif($this->parameters["body"] == null){
26                 throw new SDKRuntimeException("缺少統一支付接口必填參數body!"."<br>");
27             }elseif ($this->parameters["total_fee"] == null ) {
28                 throw new SDKRuntimeException("缺少統一支付接口必填參數total_fee!".
                   "<br>");
29             }elseif ($this->parameters["notify_url"] == null) {
30                 throw new SDKRuntimeException("缺少統一支付接口必填參數notify_url!".
                   "<br>");
31             }elseif ($this->parameters["trade_type"] == null) {
32                 throw new SDKRuntimeException("缺少統一支付接口必填參數trade_type!
                   "."<br>");
33             }elseif ($this->parameters["trade_type"] == "JSAPI" && $this->parameters
                   ["openid"] == NULL){
35                 throw new SDKRuntimeException("統一支付接口中,缺少必填參數openid!
                   trade_type為JSAPI時,openid為必填參數!"."<br>");
36             }
37             $this->parameters["appid"] = WxPayConf_pub::APPID;// 公眾賬號ID
38             $this->parameters["mch_id"] = WxPayConf_pub::MCHID;// 商戶號
39             $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];// 終端IP
40             $this->parameters["nonce_str"] = $this->createNoncestr;// 隨機字符串
41             $this->parameters["sign"] = $this->getSign($this->parameters);// 簽名
42             $xyz =$this->arrayToXml($this->parameters);
43             return  $xyz;
44         }catch (SDKRuntimeException $e)
45         {
46             die($e->errorMessage);
47         }
48     }
49 
50     /**
51      * 獲取prepay_id
52      */
53     function getPrepayId
54     {
55         $this->postXml;
56         $this->result = $this->xmlToArray($this->response);
57         $prepay_id = $this->result["prepay_id"];
58         return $prepay_id;
59     }
60 }
 

短鏈接接口主要是將微信支付的長鏈接轉換為短鏈接。


 1 /**
 2  * 短鏈接轉換接口
 3  */
 4 class ShortUrl_pub extends Wxpay_client_pub
 5 {
 6     function __construct
 7     {
 8         // 設置接口鏈接
 9         $this->url = "https:// api.mch.weixin.qq.com/tools/shorturl";
10         // 設置curl超時時間
11         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
12     }
13 
14     /**
15      * 生成接口參數XML
16      */
17     function createXml
18     {
19         try
20         {
21             if($this->parameters["long_url"] == null )
22             {
23                 throw new SDKRuntimeException("短鏈接轉換接口中,缺少必填參數long_url!".
                   "<br>");
24             }
25                $this->parameters["appid"] = WxPayConf_pub::APPID;// 公眾賬號ID
26                $this->parameters["mch_id"] = WxPayConf_pub::MCHID;// 商戶號
27             $this->parameters["nonce_str"] = $this->createNoncestr;// 隨機字符串
28             $this->parameters["sign"] = $this->getSign($this->parameters);// 簽名
29             return  $this->arrayToXml($this->parameters);
30         }catch (SDKRuntimeException $e)
31         {
32             die($e->errorMessage);
33         }
34     }
35 
36     /**
37      * 獲取prepay_id
38      */
39     function getShortUrl
40     {
41         $this->postXml;
42         $prepay_id = $this->result["short_url"];
43         return $prepay_id;
44     }
45 }