* out_refund_no、total_fee、refund_fee、op_user_id为必填参数
* appid、mchid、spbill_create_ip、nonce_str不需要填入
* @param WxPayRefund
$inputObj
* @param int
$timeOut
* @throws WxPayException
* @
return
成功时返回,其他抛异常
*/
public
static
function
refund(
$inputObj
,
$timeOut
= 6){
$url
=
"https://api.mch.weixin.qq.com/secapi/pay/refund"
;
if
(!
$inputObj
->IsOut_trade_noSet() && !
$inputObj
->IsTransaction_idSet()) {
throw
new
WxPayException(
"退款申请接口中,out_trade_no、transaction_id至少填一个!"
);
}
else
if
(!
$inputObj
->IsOut_refund_noSet()){
throw
new
WxPayException(
"退款申请接口中,缺少必填参数out_refund_no!"
);
}
else
if
(!
$inputObj
->IsTotal_feeSet()){
throw
new
WxPayException(
"退款申请接口中,缺少必填参数total_fee!"
);
}
else
if
(!
$inputObj
->IsRefund_feeSet()){
throw
new
WxPayException(
"退款申请接口中,缺少必填参数refund_fee!"
);
}
else
if
(!
$inputObj
->IsOp_user_idSet()){
throw
new
WxPayException(
"退款申请接口中,缺少必填参数op_user_id!"
);
}
$inputObj
->SetAppid(WxPayConfig::APPID);
$inputObj
->SetMch_id(WxPayConfig::MCHID);
$inputObj
->SetNonce_str(self::getNonceStr());
$inputObj
->SetSign();
$xml
=
$inputObj
->ToXml();
$startTimeStamp
= self::getMillisecond();
$response
= self::postXmlCurl(
$xml
,
$url
, true,
$timeOut
);
$result
= WxPayResults::Init(
$response
);
self::reportCostTime(
$url
,
$startTimeStamp
,
$result
);
return
$result
;
}
官方的方法,写的很清楚需要哪些参数,还有一些必须参数SDK已经帮我们补齐了,我将这个方法重新封装一下,便于在项目中调用:
* 微信退款
* @param string
$order_id
订单ID
* @
return
成功时返回(
array
类型),其他抛异常
*/
function
wxRefund(
$order_id
){
require_once
APP_ROOT.
"/Api/wxpay/lib/WxPay.Api.php"
;
$order
= M('order')->where(
array
('id'=>
$order_id
,'is_refund'=>2,'order_status'=>1))->find();
$merchid
= WxPayConfig::MCHID;
if
(!
$order
)
return
false;
$input
=
new
WxPayRefund();
$input
->SetOut_trade_no(
$order
['order_sn']);
$input
->SetTransaction_id(
$order
['transaction_id']);
$input
->SetOut_refund_no(getrand_num(true));
$input
->SetTotal_fee(
$order
['total_price']);
$input
->SetRefund_fee(
$order
['total_price']);
$input
->SetOp_user_id(
$merchid
);
$result
= WxPayApi::refund(
$input
);
return
$result
;
}
这里需要吐槽一下,竟然不说返回值的类型,在支付的时候返回的是XML数据,这里竟然返回的是数组,让我措手不及,哈哈不过还是返回数组比较好,可以直接判断处理。
方法调用就更加简单了:
$result
= wxRefund(
$order_id
);
if
((
$result
['return_code']=='SUCCESS') && (
$result
['result_code']=='SUCCESS')){
}
else
if
((
$result
['return_code']=='FAIL') || (
$result
['result_code']=='FAIL')){
$reason
= (
empty
(
$result
['err_code_des'])?
$result
['return_msg']:
$result
['err_code_des']);
}
else
{
}