退款接口文檔:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_4
條件:laravel框架
1.根據(jù)訂單號或者支付號,即可進行退款
2.退款金額可以少于支付金額(扣除手續(xù)費什么的)
3.退款需要證書(兩種方式:windows將apiclient_cert.p12執(zhí)行一遍即可導入系統(tǒng);其他系統(tǒng)需要在代碼中引入另外2個.pem文件;建議都使用引入文件,比較方便);證書獲取說明https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
注:linux遇上一個bug,正反斜杠寫法導致的錯誤;
原來的引入路徑為"app_path().'\Libs\common\wx_cert\apiclient_key.pem';//證書路徑",這個寫法在linux上會導致postXmlSSLCurl()函數(shù)返回400錯誤;
改為正確寫法:"app_path().'/Libs/common/wx_cert/apiclient_cert.pem";//證書路徑
以下為退款類
<?php
namespace App\Libs\common;
class WeixinRefund {
function __construct($openid,$appid,$mch_id,$key,$outTradeNo,$totalFee,$outRefundNo,$refundFee){
//初始化退款類需要的變量
$this->openid = $openid;//openid
$this->APPID = $appid;
$this->MCHID = $mch_id;
$this->key = $key;
$this->outTradeNo = $outTradeNo;//訂單號order_sn
$this->totalFee = $totalFee;//訂單總金額
$this->outRefundNo = $outRefundNo;//退款單號
$this->refundFee = $refundFee;//需要退款的金額
$this->SSLCERT_PATH = app_path().'/Libs/common/wx_cert/apiclient_cert.pem';//證書路徑
$this->SSLKEY_PATH = app_path().'/Libs/common/wx_cert/apiclient_key.pem';//證書路徑
}
public function refund(){
//對外暴露的退款接口
$result = $this->wxrefundapi();
return $result;
}
private function wxrefundapi(){
//通過微信api進行退款流程
$parma = array(
'appid'=> $this->APPID,
'mch_id'=> $this->MCHID,
'nonce_str'=> $this->createNoncestr(),
'out_refund_no'=> $this->outRefundNo,
'out_trade_no'=> $this->outTradeNo,
'total_fee'=> floatval(($this->totalFee) * 100),
'refund_fee'=> floatval(($this->refundFee) * 100),
);
$parma['sign'] = $this->getSign($parma);
$xmldata = $this->arrayToXml($parma);
$xmlresult = $this->postXmlSSLCurl($xmldata,'https://api.mch.weixin.qq.com/secapi/pay/refund');
//echo '<prE>';var_dump($xmlresult);die;
$result = $this->xmlToArray($xmlresult);
return $result;
}
//需要使用證書的請求
function postXmlSSLCurl($xml,$url,$second=30)
{
$ch = curl_init();
//超時時間
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
//這里設(shè)置代理,如果有的話
//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//設(shè)置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求結(jié)果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//設(shè)置證書
//使用證書:cert 與 key 分別屬于兩個.pem文件
//默認格式為PEM匾旭,可以注釋
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $this->SSLCERT_PATH);
//默認格式為PEM镣屹,可以注釋
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $this->SSLKEY_PATH);
//post提交方式
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
//返回結(jié)果
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
echo "curl出錯,錯誤碼:$error" . "<br>";
curl_close($ch);
return false;
}
}
//作用:生成簽名
private function getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
//簽名步驟一:按字典序排序參數(shù)
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//簽名步驟二:在 string 后加入 KEY
$String = $String . "&key=" . $this->key;
//簽名步驟三:MD5 加密
$String = md5($String);
//簽名步驟四:所有字符轉(zhuǎn)為大寫
$result_ = strtoupper($String);
return $result_;
}
///作用:格式化參數(shù)价涝,簽名過程需要使用
private function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar = '';
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
//作用:產(chǎn)生隨機字符串女蜈,不長于 32 位
private function createNoncestr($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//數(shù)組轉(zhuǎn)換成 xml
private function arrayToXml($arr) {
$xml = "<xml>
<appid>".$arr['appid']."</appid>
<mch_id>".$arr['mch_id']."</mch_id>
<nonce_str>".$arr['nonce_str']."</nonce_str>
<out_refund_no>".$arr['out_refund_no']."</out_refund_no>
<out_trade_no>".$arr['out_trade_no']."</out_trade_no>
<refund_fee>".$arr['refund_fee']."</refund_fee>
<total_fee>".$arr['total_fee']."</total_fee>
<transaction_id></transaction_id>
<sign>".$arr['sign']."</sign>
</xml>";
/* $xml = "";
foreach ($arr as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
} else {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
}
}
$xml .= "</xml>";
*/
//echo($xml);die;
return $xml;
}
//xml 轉(zhuǎn)換成數(shù)組
private function xmlToArray($xml) {
//禁止引用外部 xml 實體
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;
}
}
調(diào)用該類:
public function wx_refund($order_id,$price){
////此處省略了獲取參數(shù)的相關(guān)代碼
//調(diào)用退款接口(openid/訂單號/訂單金額/退款號/退款金額 注:這里訂單號=退款號,訂單實付金額=退款金額
$wx_refund = new WeixinRefund($openid,$config['c_appid'],$mch['c_appid'],$mch['c_key'], $order['t_order_number'], $totalFee, $order['t_order_number'], $refundFee);
$return = $wx_refund->refund();//返回值為數(shù)組格式
$return['order_id'] = $order_id;
$this->log_add('退款返回值',$return);//寫入本地日志,使用的是laravel自帶的Logger日志記錄函數(shù)
if ($return['result_code'] == 'SUCCESS') {//支付成功處理訂單信息(更新訂單支付狀態(tài)
return true;
}else{
return false;
}
}
以下為退款成功時的返回值,作參考使用
{
["return_code"] => string(7)"SUCCESS"
["return_msg"] => string(2)"OK"
["appid"] => string(18)"wxafe501b449dcae9a"
["mch_id"] => string(10)"1568688591"
["nonce_str"] => string(16)"QrTSO1nsbUm85lav"
["sign"] => string(32)"0475BBBECB3E0EA44CE2BF0DC87948F3"
["result_code"] => string(7)"SUCCESS"
["transaction_id"] => string(28)"4200000455202001013871227500"
["out_trade_no"] => string(32)"20200101175340157787242047007765"
["out_refund_no"] => string(32)"20200101175340157787242047007765"
["refund_id"] => string(29)"50000403182020010113932540592"
["refund_channel"] => array(0) {}
["refund_fee"] => string(1)"1"
["coupon_refund_fee"] => string(1)"0"
["total_fee"] => string(1)"1"
["cash_fee"] => string(1)"1"
["coupon_refund_count"] => string(1)"0"
["cash_refund_fee"] => string(1)"1"
}