【实例简介】
//使用方法一:
* 配置好自己的参数,注意这里的两个证书路径得根据你自己的项目证书路径来写,同时存放证书的目录要开放可读权限
* 单纯的支付不需要证书 , 退款的时候需要证书
$config = array(
'appid'=> 'wx123456789876',
'mch_id' => '123456789',
'pay_apikey' => '123456789876123456789876123456789876',
'api_cert'=> getcwd().'/cert/apiclient_cert.pem',
'api_key'=> getcwd().'/cert/apiclient_key.pem'
);
$wxpay = new WxPay($config);//初始化类(同时传递参数)
$data = $wxpay->wxpay($openid,$total_fee,$body,$order_sn);//微信支付,将返回值$data(json格式)返回给页面,进行JSAPI支付
$wxpay->refund($transaction_id,$out_refund_no,$total_fee,$refund_fee);//微信退款
//使用方法二:
$wxpay = new WxPay();//初始化类
$wxpay->appid = 'wx123456789876';//配置参数
$wxpay->mch_id = '123456789';
$wxpay->pay_apikey = '123456789876123456789876123456789876';
$wxpay->api_cert = getcwd().'/cert/apiclient_cert.pem';
$wxpay->api_key = getcwd().'/cert/apiclient_key.pem';
$data = $wxpay->wxpay($openid,$total_fee,$body,$order_sn);//微信支付,将返回值$data(json格式)返回给页面,进行JSAPI支付
$wxpay->refund($transaction_id,$out_refund_no,$total_fee,$refund_fee);//微信退款
【实例截图】无
【核心代码】
public function refund($transaction_id,$out_refund_no,$total_fee,$refund_fee){
$config = $this->config;
//退款参数
$refundorder = array(
'appid' => $config['appid'],
'mch_id' => $config['mch_id'],
'nonce_str' => self::getNonceStr(),
'transaction_id'=> $transaction_id,
'out_refund_no' => $out_refund_no,
'total_fee' => $total_fee * 100,
'refund_fee' => $refund_fee * 100
);
$refundorder['sign'] = self::makeSign($refundorder);
//请求数据,进行退款
$xmldata = self::array2xml($refundorder);
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
$res = self::curl_post_ssl($url, $xmldata);
if(!$res){
return array('status'=>0, 'msg'=>"Can't connect the server" );
}
// 这句file_put_contents是用来查看服务器返回的结果 测试完可以删除了
//file_put_contents('./log3.txt',$res,FILE_APPEND);
$content = self::xml2array($res);
if(strval($content['result_code']) == 'FAIL'){
return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
}
if(strval($content['return_code']) == 'FAIL'){
return array('status'=>0, 'msg'=>strval($content['return_msg']));
}
return $content;
}