tp5微信退款 话不多少直接上代码:
微信退款方法
public function Tuikuan($order_no){
$order=Db('订单表')->where('order_no',$order_no)->find();
if(empty($order)){
return 1;//订单不存在
}
$OrderData=Db('支付订单表')->where('statuss',0)->where('out_trade_no','like',$order_no.'%')->select();
if(empty($OrderData)){
return 1;//没有支付订单
}
$config = array(
'mch_id' => '商户号',
'appid' => '微信公众号id',
'key' => '微信支付密匙',
);
foreach($OrderData as $k=>$v){
$res=$this->TuiKuanOrder($v,$config);
}
}
//退款调用方法
public function TuiKuanOrder($arr,$config){
$refundNo='tuikuan'.$arr['id'].'-'.time().rand(1000,9999);
$unified = array(
'appid' => $config['appid'],
'mch_id' => $config['mch_id'],
'nonce_str' => self::createNonceStr(),
'total_fee' => $arr['total_fee'], //订单金额 单位 转为分
'refund_fee' => $arr['total_fee'], //退款金额 单位 转为分 支付多少退多少
'sign_type' => 'MD5', //签名类型 支持HMAC-SHA256和MD5,默认为MD5
'transaction_id' => $arr['transaction_id'], //微信订单号
'out_trade_no' => $arr['out_trade_no'], //商户订单号
'out_refund_no' => $refundNo, //商户退款单号
'refund_desc' => '商品已售完', //退款原因(选填)
);
$unified['sign'] = self::getSign($unified, $config['key']);//获取签名
$responseXml = $this->curlPost('https://api.mch.weixin.qq.com/secapi/pay/refund', self::arrayToXml($unified));
$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
dump($unified);
dump($unifiedOrder);
if ($unifiedOrder === false) {
die('parse xml error');
}
if ($unifiedOrder->return_code != 'SUCCESS') {
die($unifiedOrder->return_msg);
}
if ($unifiedOrder->result_code != 'SUCCESS') {
die($unifiedOrder->err_code);
}
//dump($unifiedOrder);
//echo true;
return true;
//return $unified;
}
//随机字符串,不长于32位
public static function createNonceStr($length = 16) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//获取sign签名
public static function getSign($params, $key) {
ksort($params, SORT_STRING);
$unSignParaString = self::formatQueryParaMap($params, false);
$signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
return $signStr;
}
//生成签名
protected static function formatQueryParaMap($paraMap, $urlEncode = false) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if (null != $v && "null" != $v) {
if ($urlEncode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
}
$reqPar = '';
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
//curl post提交退款
public function curlPost($url = '', $postData = '', $options = array()) {
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https请求 不验证证书和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//第一种方法,cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, "/www/...绝对路径/apiclient_cert.pem");//绝对路径
//默认格式为PEM,可以注释
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, "/www/...绝对路径t/apiclient_key.pem");//绝对路径
//第二种方式,两个文件合成一个.pem文件
// curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
$data = curl_exec($ch);
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
echo "curl出错,错误码:$error" . "<br>";
curl_close($ch);
return false;
}
}
//生成xml文件
public function arrayToXml($arr) {
$xml = "<xml>";
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
微信支付及退款详解查看链接https://www.jianshu.com/p/0ceef29f2707/