/**
* 处理退款
* @param $out_trade_no
* @param $total_fee
* @param $refund_fee
* @param $from 1余额 2未结算
* @return array
* @throws Exception
* 策略一:当天支付的钱,从未结算中退;非当天支付的钱,从余额中退(结算的钱到余额中有个缓冲期1-3天,结算到余额要收千分之一的手续费)。确保退款正常,需要在余额中留有备用金。
* 策略二:优先从未结算中退,未结算中余额不足,再从余额中退。(需要查询两次,比较消耗网络。好处就是可以节省被腾讯收取的千分之一的费用。)
*/
public static function refundOrder($out_trade_no,$total_fee,$refund_fee,$from = 1) {
$refund_no = $out_trade_no.$total_fee;
if ((int)$from === 1) {
$refund_account = 'REFUND_SOURCE_RECHARGE_FUNDS';
} else {
$refund_account = 'REFUND_SOURCE_UNSETTLED_FUNDS';
}
// $refund_account = 'REFUND_SOURCE_UNSETTLED_FUNDS';
$param = array(
'appid' => C('APPID'),
'mch_id' => C('MCHID'),
'nonce_str' => self::createNonceStr(),
'out_refund_no' => $refund_no, //由后端生成的退款单号,需要保证唯一,因为多个同样的退款单号只会退款一次。
'out_trade_no' => $out_trade_no, //退款订单在支付时生成的订单号
'total_fee' => $total_fee,
'refund_fee' => $refund_fee,
'refund_account'=> $refund_account, // REFUND_SOURCE_RECHARGE_FUNDS 从余额退,REFUND_SOURCE_UNSETTLED_FUNDS 从未结算退
'op_user_id' => C('MCHID'), //操作员 op_user_id .与商户号相同即可
);
$param['sign'] = self::MakeSign($param);
$xml_data = self::ToXml($param);
$xml_result = self::postXmlSSLCurl($xml_data,'https://api.mch.weixin.qq.com/secapi/pay/refund');
$result = self::FromXml($xml_result);
self::setlog($param,$result,__METHOD__);
if (!$result){
$result_arr = [
'num' => '0',
'desc' => '接口错误',
];
return $result_arr;
}
if ($result['result_code'] != 'SUCCESS'){
$result_arr = [
'num' => '-1',
'desc' => $result['err_code_des'],
'err_code' => $result['err_code'], // NOTENOUGH 余额不足
];
} else {
$result_arr = [
'num' => '1',
'desc' => '退款成功',
'refund_id' => $result['refund_id'],
'refund_no' => $refund_no,
];
}
return $result_arr;
}
经验之谈!