退款申请的接口如下。
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 }