<?php
function pay_refund_ok($id){
$refuninfo = get_model_one(array('id'=>$id),'shop_goods_refund');
$PayConfig = PayConfig();
$nonce_str = createNonceStr(20);
$data['appid'] = $PayConfig["wx"]['APPID'];
$data['mch_id'] = $PayConfig["wx"]['MCHID'];
$data['nonce_str'] = $nonce_str; //随机字符串
$data['transaction_id'] = get_onerecord(array('orderidall' => $refuninfo['order_id']), 'payment_order', 'transaction_id');//商户订单号
//$data['total_fee'] = $refuninfo['price']*100/100;//总金额
//$data['refund_fee'] = $refuninfo['price']*100/100;//退款金额
$data['total_fee'] = 1;//总金额
$data['refund_fee'] = 1;//退款金额
$data['out_refund_no'] = $refuninfo['order_refund']; //商户内部唯一退款单号
$data = array_filter($data);
$data['sign'] = tuikuan_hb_MakeSign($data, $PayConfig["wx"]["KEY"]);
$url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
$xml = tuikuan_hb_arraytoxml($data);
$res = tuikuan_curl($xml, $url);
$return = tuikuan_hb_xmltoarray($res);
if ($return['return_code'] == 'SUCCESS' && $return['result_code'] == 'SUCCESS') {
refun_up($return['out_refund_no'],$return['refund_id']);
log_resulttest('退款out_refund_no'.json_encode($return['out_refund_no']));
return true;
}else{
return false;
}
}
/**
* 生成签名
* @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
*/
function tuikuan_hb_MakeSign($args,$key)
{
//签名步骤一:按字典序排序参数
ksort($args);
$string = tuikuan_hb_ToUrlParams($args);
//签名步骤二:在string后加入KEY
$string = $string . "&key=".$key;
//签名步骤三:MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
return $result;
}
function tuikuan_hb_arraytoxml($data){
/* $str='<xml>';
foreach($data as $k=>$v) {
$str.='<'.$k.'>'.$v.'</'.$k.'>';
}
$str.='</xml>';
return $str; */
if(!is_array($data) || count($data)<=0){return false;}
$xml="<xml>";
foreach ($data as $key=>$val){
if(is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}
else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
/**
* @author laver
* @name 支付系统 函数库
*/
function tuikuan_hb_xmltoarray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
/* 退款 curl函数 */
function tuikuan_curl($xml,$url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // 增加 HTTP Header(头)里的字段
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch, CURLOPT_SSLCERT,'/www/wwwroot/www.**.com/apiclient_cert.pem');
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch, CURLOPT_SSLKEY,'/www/wwwroot/www.**.com/apiclient_key.pem');
$data = curl_exec($ch); //运行curl
//var_dump($data);exit;
curl_close($ch);
return $data;
//print_r($data);exit;
/*if(curl_exec($ch) === false)
{
echo 'Curl error--: ' . curl_error($ch);
}*/
}
?>