/**
* 微信退款
*
* @access public
* @param string $total_amount 订单总金额 单位 分!!!!!!!!!!!!!!!!!!!!!!
* @param string $refund_fee 退款金额 单位 分!!!!!!!!!!!!!!!!!!!!!!
* @param string $out_trade_no 支付时传入的订单号
* @return string $out_request_no 订单的退款单号
*/
public function refund_order($total_amount,$refund_fee, $out_trade_no,$out_request_no)
{
$data = array();
$data['appid'] = $this->user_config['appid'];
$data['mch_id'] = $this->user_config['mch_id'];
$data['nonce_str'] = MD5(time().rand(10000,99999));
// $data['body'] = $body;
$data['out_trade_no'] = (string)$out_trade_no;
$data['out_refund_no'] = (string)$out_request_no;
$data['total_fee'] = (int)$total_amount;
$data['refund_fee'] = (int)$refund_fee;
$data['sign'] = getSign($data,$this->user_config['api']);
//转为xml格式
$xml_str = arrayToXml($data);
//发送请求 使用封装好的curl_post
$result = postXmlRefundCurl($xml_str);
//解析得到的值
$res = xmlstr_to_array($result);
if($res['result_code'] == "SUCCESS")
{
$data = [
'status' => true,
'msg' => '退款成功'
];
}
else
{
$data = [
'status' => false,
'msg' =>'退款失败,'.$res['err_code_des']
];
}
return $data;
}
function postXmlRefundCurl($xml,$url='https://api.mch.weixin.qq.com/secapi/pay/refund') { //初始化curl $ch = curl_init(); //设置抓取的url curl_setopt($ch, CURLOPT_URL, $url); //设置头文件的信息作为数据流输出// curl_setopt($ch, CURLOPT_HEADER, 1); //设置获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //证书检查 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1); curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem'); curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).'/wxcertificate/apiclient_cert.pem'); curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem'); curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).'/wxcertificate/apiclient_key.pem'); curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem'); curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).'/wxcertificate/rootca.pem'); //设置post方式提交 curl_setopt($ch, CURLOPT_POST, 1); //设置post数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //执行命令 $data = curl_exec($ch); if($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "curl出错,错误码:$error"."
"; echo "错误原因查询
"; curl_close($ch); return false; } }