讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 17.11 退款申請 >

17.11 退款申請

退款申請的接口如下。


https:// api.mch.weixin.qq.com/secapi/pay/refund
  

退款申請時,POST數據示例如下。


<xml>
    <appid>wx2421b1c4370ec43b</appid>
    <mch_id>10000100</mch_id>
    <nonce_str>6cefdb308e1e2e8aabd48cf79e546a02</nonce_str>
    <op_user_id>10000100</op_user_id>
    <out_refund_no>1415701182</out_refund_no>
    <out_trade_no>1415757673</out_trade_no>
    <refund_fee>1</refund_fee>
    <total_fee>1</total_fee>
    <transaction_id></transaction_id>
    <sign>FE56DD4AA85C0EECA82C35595A69E153</sign>
</xml>
  

退款時需要帶上證書。

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

表17-18 退款申請接口的參數說明

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


<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[NfsMFbUFpdbEhPXP]]></nonce_str>
    <sign><![CDATA[B7274EB9F8925EB93100DD2085FA56C0]]></sign>
    <result_code><![CDATA[SUCCESS]]></result_code>
    <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>
    <out_trade_no><![CDATA[1415757673]]></out_trade_no>
    <out_refund_no><![CDATA[1415701182]]></out_refund_no>
    <refund_id><![CDATA[2008450740201411110000174436]]></refund_id>
    <refund_channel><![CDATA]></refund_channel>
    <refund_fee>1</refund_fee> 
</xml>
  

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

表17-19 退款申請接口返回參數說明

退款申請接口類的實現代碼如下。


 1 /**
 2  * 退款申請接口
 3  */
 4 class Refund_pub extends Wxpay_client_pub
 5 {
 6 
 7     function __construct {
 8         // 設置接口鏈接
 9         $this->url = "https:// api.mch.weixin.qq.com/secapi/pay/refund";
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 && $this->parameters["tran-
               saction_id"] == null) {
23                 throw new SDKRuntimeException("退款申請接口中,out_trade_no、transaction_
                   id至少填一個!"."<br>");
24             }elseif($this->parameters["out_refund_no"] == null){
25                 throw new SDKRuntimeException("退款申請接口中,缺少必填參數out_refund_
                   no!"."<br>");
26             }elseif($this->parameters["total_fee"] == null){
27                 throw new SDKRuntimeException("退款申請接口中,缺少必填參數total_fee!".
                   "<br>");
28             }elseif($this->parameters["refund_fee"] == null){
29                 throw new SDKRuntimeException("退款申請接口中,缺少必填參數refund_fee!".
                   "<br>");
30             }elseif($this->parameters["op_user_id"] == null){
31                 throw new SDKRuntimeException("退款申請接口中,缺少必填參數op_user_id!".
                   "<br>");
32             }
33                $this->parameters["appid"] = WxPayConf_pub::APPID;// 公眾賬號ID
34                $this->parameters["mch_id"] = WxPayConf_pub::MCHID;// 商戶號
35             $this->parameters["nonce_str"] = $this->createNoncestr;// 隨機字符串
36             $this->parameters["sign"] = $this->getSign($this->parameters);// 簽名
37             return  $this->arrayToXml($this->parameters);
38         }catch (SDKRuntimeException $e)
39         {
40             die($e->errorMessage);
41         }
42     }
43     /**
44      *     作用:獲取結果,使用證書通信
45      */
46     function getResult
47     {
48         $this->postXmlSSL;
49         $this->result = $this->xmlToArray($this->response);
50         return $this->result;
51     }
52 }