支付通知文件,返回如下信息,ewei_shopv2\payment\wechat\notify.php 通过 $this->get['attach']获取
Array
(
[get] => Array
(
[appid] => wx8a86306dcc35b538
[attach] => 5:17 //前者为公众号id,后者是支付方式代码
[bank_type] => CFT
[cash_fee] => 1
[device_info] => ewei_shopv2
[fee_type] => CNY
[is_subscribe] => Y
[mch_id] => 1485040312
[nonce_str] => d1l11eOETpaY5AeRwC1E18e4T388O14o
[openid] => o2SfSwZusX6_wKAFOBGrBAiUpPNk
[out_trade_no] => OS20171128143422642364
[result_code] => SUCCESS
[return_code] => SUCCESS
[sign] => 5B1B86703788788B021E51ED5D667E74
[time_end] => 20171128143428
[total_fee] => 1
[trade_type] => JSAPI
[transaction_id] => 4200000042201711287611180681
)
[type] => 17
[total_fee] => 0.01
[set] =>
[setting] =>
[sec] =>
[sign] =>
[isapp] =>
[is_jie] =>
)
退款处理
/**
* 支付退款,传入以下参数即可实现退款,退款金额必须小于等于总金额,对于退款失败的订单需要重新传入退款订单号,不要新生成
* 2018.1.12
* yoby
* 参数
* $openid
* $ordersn商户订单号
* $wechatid 退款订单号,自己生成
* $total_fee总金额
* $refund_fee退款金额
* $merchid 商家入驻id
*/
function wechat_refund($openid,$ordersn,$wechatid,$total_fee,$refund_fee,$merchid){
global $_W;
if (empty($openid)) {
return error(-1, 'openid不能为空');
}
$rs = pdo_fetch('select * from '.tablename('ewei_shop_merch_user').' where id=:id',[':id'=>$merchid]);
$sec = iunserializer($rs['sec']);
$rs['sec'] = $sec;
$apikey = $rs['prikey'];
//退款签名
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
$pars = [
'appid'=>$rs['appid'],
'mch_id'=>$rs['mchid'],
'nonce_str'=>random(8),
'out_trade_no'=>$ordersn,
'out_refund_no'=>$wechatid,
'total_fee'=>$total_fee,
'refund_fee'=>$refund_fee
];
ksort($pars, SORT_STRING);
$string1 = '';
foreach ($pars as $k => $v) {
$string1 .= $k . '=' . $v . '&';
}
$string1 .= 'key=' . $apikey;
$pars['sign'] = strtoupper(md5($string1));
$xml = array2xml($pars);
$extras = array();
$errmsg = '未上传完整的微信支付证书,请到入驻商户信息中上传!';
if (is_array($sec)) {
if (empty($sec['cert']) || empty($sec['key']) || empty($sec['root'])) {
if ($_W['ispost']) {
show_json(0, array('message' => $errmsg));
}
show_message($errmsg, '', 'error');
}
$certfile = IA_ROOT . '/addons/ewei_shopv2/cert/' . random(8);
file_put_contents($certfile, $sec['cert']);
$keyfile = IA_ROOT . '/addons/ewei_shopv2/cert/' . random(8);
file_put_contents($keyfile, $sec['key']);
$rootfile = IA_ROOT . '/addons/ewei_shopv2/cert/' . random(8);
file_put_contents($rootfile, $sec['root']);
$extras['CURLOPT_SSLCERT'] = $certfile;
$extras['CURLOPT_SSLKEY'] = $keyfile;
$extras['CURLOPT_CAINFO'] = $rootfile;
}
load()->func('communication');
$resp = ihttp_request($url, $xml, $extras);
//load()->func('logging');
// logging_run(date('m-d H:i:s').'退款日志'.$resp['content']);
@unlink($certfile);
@unlink($keyfile);
@unlink($rootfile);
if (empty($resp['content'])) {
return error(-2, '网络错误');
}
$arr = json_decode(json_encode(simplexml_load_string($resp['content'], 'SimpleXMLElement', LIBXML_NOCDATA)), true);
if (($arr['return_code'] == 'SUCCESS') && ($arr['result_code'] == 'SUCCESS')) {
return ['code'=>'200','msg'=>'退款成功'];
}
else {
if ($arr['return_msg'] == $arr['err_code_des']) {
$error = $arr['return_msg'];
}
else {
$error =$arr['err_code_des'];
}
return ['code'=>'0','msg'=>'退款失败,错误描述:'.$error];
}
}