<?php
namespace wechat;
/**
* 微信退款
*
* Class WachetRefund
* @package wechat
*/
class WachetRefund
{
/**
* 微信退款
* @param string $order_sn 商户系统订单号
* @param string $transaction_id 微信生成的订单号,在支付通知中有返回
* @param string $out_refund_no 商户系统内部的退款单号,商户系统内部唯一,只能是数字、大小写字母_-|*@ ,同一退款单号多次请求只退一笔。
* @param float $total_fee 订单总金额
* @return array[]|string
*/
public static function wachetRefund(string $order_sn, string $transaction_id, string $out_refund_no, float $total_fee){
$data = [
'appid' => '*', // 小程序ID
'mch_id' => '*', // 微信支付分配的商户号
'nonce_str' => self::getNonceStr(),
'transaction_id' => $transaction_id,
'out_refund_no' => $out_refund_no,
'total_fee' => intval(floatval($total_fee) * 100),
'refund_fee' => intval(floatval($total_fee) * 100), // 退款总金额,订单总金额,单位为分,只能为整数
];
$data['sign'] = self::makeSign($data); // 根据微信商户秘钥生成签名
$xmldata = self::array2xml($data);
// 请求url
$opUrl = "https://api.mch.weixin.qq.com/secapi/pay/refund";
$res = self::curl_post_ssl_refund($opUrl, $xmldata);
if (!$res) {
self::return_err("Can't connect the server");
}
$content = self::xml2array($res);
if (isset($content['result_code']) && strval($content['result_code']) == 'FAIL') {
return strval($content['err_code_des']);
}
if (isset($content['return_code']) && strval($content['return_code']) == 'FAIL') {
return strval($content['return_msg']);
}
return array('data' => $content);
}
/**
* 将一个数组转换为 XML 结构的字符串
* @param array $arr 要转换的数组
* @param int $level 节点层级, 1 为 Root.
* @return string XML 结构的字符串
*/
protected static function array2xml($arr, $level = 1)
{
$s = $level == 1 ? "<xml>" : '';
foreach ($arr as $tagname => $value) {
if (is_numeric($tagname)) {
$tagname = $value['TagName'];
unset($value['TagName']);
}
if (!is_array($value)) {
$s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : '') . $value . (!is_numeric($value) ? ']]>' : '') . "</{$tagname}>";
} else {
$s .= "<{$tagname}>" . self::array2xml($value, $level + 1) . "</{$tagname}>";
}
}
$s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
return $level == 1 ? $s . "</xml>" : $s;
}
/**
*
* 产生随机字符串,不长于32位
* @param int $length
* @return string
*/
protected static function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 生成签名
* @param $data
* @return string
*/
protected static function makeSign($data)
{
//获取微信支付秘钥
$key = 'rWBLjwNO7KC7asW19WszmovtyONEsNLp';
// 去空
$data = array_filter($data);
//签名步骤一:按字典序排序参数
ksort($data);
$string_a = http_build_query($data);
$string_a = urldecode($string_a);
//签名步骤二:在string后加入KEY
//$config=$this->config;
$string_sign_temp = $string_a . "&key=" . $key;
//签名步骤三:MD5加密
$sign = md5($string_sign_temp);
// 签名步骤四:所有字符转为大写
return strtoupper($sign);
}
/**
* 进行请求
* @param string $url 请求url
* @param string $vars 传输数据
* @param int $second 超时时间
* @param array $aHeader
* @return bool|string
*/
protected static function curl_post_ssl_refund(string $url, string $vars, int $second = 30, array $aHeader = [])
{
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); // 默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLCERT,ROOT_PATH . '/addons/epay/certs/apiclient_cert.pem'); // 微信支付API证书cert
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); // 默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEY,ROOT_PATH . '/addons/epay/certs/apiclient_key.pem'); // 微信支付API证书key
/**
* 这里设置代理,如果有的话
* curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
* curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
*/
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_HEADER,FALSE);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
//dump(curl_error($ch));die();
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
curl_close($ch);
return false;
}
}
/**
* 将xml转为array
* @param string $xml xml字符串
* @return array 转换得到的数组
*/
protected static function xml2array(string $xml)
{
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
/**
* 错误返回提示
* @param string $errMsg 错误信息
* @param int $status 错误码
* @return void
*/
protected static function return_err(string $errMsg = 'error', int $status = 0)
{
exit(json_encode(array('status' => $status, 'result' => 'fail', 'errmsg' => $errMsg)));
}
/**
* 正确返回
* @param array $data 要返回的数组
* @return json的数据
*/
protected static function return_data($data = array())
{
exit(json_encode(array('status' => 1, 'result' => 'success', 'data' => $data)));
}
}