PHP實現微信申請退款(證書權限必須設爲可執行)

前期準備:
固然是搞定了微信支付,否則怎麼退款,此次仍是使用官方的demo。固然網上可能也有不少大神本身重寫和封裝了demo,或許更加好用簡潔,可是我仍是不提倡用,緣由以下:
(1)可能功能不全,或許他只是實現了微信支付,可是還有申請退款、查詢退款、訂單查詢、撤銷訂單等業務功能多是你後續須要的,若是你依賴於大神的SDK的便捷,若是有新的業務需求,你就懵逼了;
(2)安全考慮,涉及到支付涉及到金錢,必需要很是安全。官方SDK雖然我也吐槽,但至少會相對比較安全,再次重寫,雖然暫時沒看出問題,可是萬一有漏洞就很差了。
php

本篇仍是使用到官方提供的SDK中的最重要的一個類文件WxPay.Api.PHP中提供的refund()方法來實現,此方法在WxPay.Api.php文件的第141行,代碼以下:api

  1. /** 
  2.  *  
  3.  * 申請退款,WxPayRefund中out_trade_no、transaction_id至少填一個且 
  4.  * out_refund_no、total_fee、refund_fee、op_user_id爲必填參數 
  5.  * appid、mchid、spbill_create_ip、nonce_str不須要填入 
  6.  * @param WxPayRefund $inputObj 
  7.  * @param int $timeOut 
  8.  * @throws WxPayException 
  9.  * @return 成功時返回,其餘拋異常 
  10.  */  
  11. public static function refund($inputObj, $timeOut = 6){  
  12.     $url = "https://api.mch.weixin.qq.com/secapi/pay/refund";  
  13.     //檢測必填參數  
  14.     if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {  
  15.         throw new WxPayException("退款申請接口中,out_trade_no、transaction_id至少填一個!");  
  16.     }else if(!$inputObj->IsOut_refund_noSet()){  
  17.         throw new WxPayException("退款申請接口中,缺乏必填參數out_refund_no!");  
  18.     }else if(!$inputObj->IsTotal_feeSet()){  
  19.         throw new WxPayException("退款申請接口中,缺乏必填參數total_fee!");  
  20.     }else if(!$inputObj->IsRefund_feeSet()){  
  21.         throw new WxPayException("退款申請接口中,缺乏必填參數refund_fee!");  
  22.     }else if(!$inputObj->IsOp_user_idSet()){  
  23.         throw new WxPayException("退款申請接口中,缺乏必填參數op_user_id!");  
  24.     }  
  25.     $inputObj->SetAppid(WxPayConfig::APPID);//公衆帳號ID  
  26.     $inputObj->SetMch_id(WxPayConfig::MCHID);//商戶號  
  27.     $inputObj->SetNonce_str(self::getNonceStr());//隨機字符串  
  28.       
  29.     $inputObj->SetSign();//簽名  
  30.     $xml = $inputObj->ToXml();  
  31.     $startTimeStamp = self::getMillisecond();//請求開始時間  
  32.     $response = self::postXmlCurl($xml, $url, true, $timeOut);  
  33.     $result = WxPayResults::Init($response);  
  34.     self::reportCostTime($url, $startTimeStamp, $result);//上報請求花費時間  
  35.       
  36.     return $result;  
  37. }  

官方的方法,寫的很清楚須要哪些參數,還有一些必須參數SDK已經幫咱們補齊了,我將這個方法從新封裝一下,便於在項目中調用:數組

  1. /** 
  2.  * 微信退款 
  3.  * @param  string   $order_id   訂單ID 
  4.  * @return 成功時返回(array類型),其餘拋異常 
  5.  */  
  6. function wxRefund($order_id){  
  7.     //個人SDK放在項目根目錄下的Api目錄下  
  8.     require_once APP_ROOT."/Api/wxpay/lib/WxPay.Api.php";  
  9.     //查詢訂單,根據訂單裏邊的數據進行退款  
  10.     $order = M('order')->where(array('id'=>$order_id,'is_refund'=>2,'order_status'=>1))->find();  
  11.     $merchid = WxPayConfig::MCHID;  
  12.       
  13.     if(!$order) return false;  
  14.       
  15.     $input = new WxPayRefund();  
  16.     $input->SetOut_trade_no($order['order_sn']);         //本身的訂單號  
  17.     $input->SetTransaction_id($order['transaction_id']);     //微信官方生成的訂單流水號,在支付成功中有返回  
  18.     $input->SetOut_refund_no(getrand_num(true));         //退款單號  
  19.     $input->SetTotal_fee($order['total_price']);         //訂單標價金額,單位爲分  
  20.     $input->SetRefund_fee($order['total_price']);            //退款總金額,訂單總金額,單位爲分,只能爲整數  
  21.     $input->SetOp_user_id($merchid);  
  22.       
  23.     $result = WxPayApi::refund($input); //退款操做  
  24.       
  25.     // 這句file_put_contents是用來查看服務器返回的退款結果 測試完能夠刪除了  
  26.     //file_put_contents(APP_ROOT.'/Api/wxpay/logs/log3.txt',arrayToXml($result),FILE_APPEND);  
  27.     return $result;  
  28. }  

這裏須要吐槽一下,居然不說返回值的類型,在支付的時候返回的是XML數據,這裏居然返回的是數組,讓我措手不及,哈哈不過仍是返回數組比較好,能夠直接判斷處理。安全

方法調用就更加簡單了:服務器

  1. //微信退款  
  2. $result = wxRefund($order_id);  
  3. // 這句file_put_contents是用來查看服務器返回的退款結果 測試完能夠刪除了  
  4. //file_put_contents(APP_ROOT.'/Api/wxpay/logs/log4.txt',arrayToXml($result),FILE_APPEND);  
  5. if(($result['return_code']=='SUCCESS') && ($result['result_code']=='SUCCESS')){  
  6.     //退款成功  
  7. }else if(($result['return_code']=='FAIL') || ($result['result_code']=='FAIL')){  
  8.     //退款失敗  
  9.     //緣由  
  10.     $reason = (empty($result['err_code_des'])?$result['return_msg']:$result['err_code_des']);  
  11. }else{  
  12.     //失敗  
  13. }  

退款成功返回以下:微信


親測無誤:app

相關文章
相關標籤/搜索