<?php
// +----------------------------------------------------------------------
// | ITUX.CN [ ITUX.CN软件定制开发 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2023 https://itux.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 此软件并不是自由软件,未经许可不能去掉ITUX相关版权
// +----------------------------------------------------------------------
// | Author: 13103895694 <itholiday@126.com>
// +----------------------------------------------------------------------
//微信企业付款接口sdk
namespace addons\gaga\library\cqrs\libs;
use think\Db;
class WxpayRefund
{
private $_appid;
private $_merchantid;
private $_key;
private $apiclient_cert;
private $apiclient_key;
public function __construct($config)
{
$this->_appid = $config['app_id'];
$this->_merchantid = $config['mch_id'];
$this->_key = $config['api_key'];
$this->apiclient_cert = ROOT_PATH . 'public' . DS . $config['apiclient_cert'];
$this->apiclient_key = ROOT_PATH . 'public' . DS . $config['apiclient_key'];
}
public function batchRechargeRefund($refund_id, $uid, $refund_money, $money, $loop = 0)
{
static $return_data = [];
if ($money <= 0) return $return_data;
$money = bcadd($money, 0, 2);
$refund_no = $refund_id . '_' . $loop;
$lastOrder = Db::name('vip_recharge_log')->whereIn('refund_status', [0, 1])->where('user_id', $uid)->where('status', 1)->where('app_id', $this->_appid)->order('pay_money desc,refund_money asc')->limit(1)->find();
if (empty($lastOrder)) {
return $return_data;
}
$remain = bcsub($lastOrder['pay_money'], $lastOrder['refund_money'], 2);
$now_refund = $remain >= $money ? $money : $remain;
$order_refund_money = bcadd($lastOrder['refund_money'], $now_refund, 2);
$order_refund_status = $order_refund_money >= $lastOrder['pay_money'] ? 2 : 1;
$result = $this->doRefund(floatval($lastOrder['pay_money']), floatval($now_refund), $refund_no, "", $lastOrder['order_sn'], "预计退款{$refund_money}元[此单已退{$now_refund}元]");
$res = json_decode(json_encode($result), true);
if (!empty($res) && $res['return_code'] == "SUCCESS" && $res['result_code'] == "SUCCESS") {
$tmp_refund_id = $lastOrder['refund_id'] ? $refund_no : ($lastOrder['refund_id'] . '|' . $refund_no);
Db::name('vip_recharge_log')->where('id', $lastOrder['id'])->update(['refund_status' => $order_refund_status, 'refund_money' => $order_refund_money, 'refund_id' => $tmp_refund_id]);
$money = bcsub($money, $now_refund, 2);
$loop++;
$return_data[] = ['id' => $lastOrder['id'], 'now_refund' => $now_refund, 'refund_no' => $refund_no];
}
if ($money <= 0) return $return_data;
return $this->batchRechargeRefund($refund_id, $uid, $refund_money, $money, $loop);
}
/**
* 退款
* @param float $totalFee 订单金额 单位元
* @param float $refundFee 退款金额 单位元
* @param string $refundNo 退款单号
* @param string $wxOrderNo 微信订单号
* @param string $orderNo 商户订单号
* @return string
*/
public function doRefund($totalFee, $refundFee, $refundNo, $wxOrderNo = '', $orderNo = '', $desc = "", $notify_url = "")
{
$unified = array(
'appid' => $this->_appid,
'mch_id' => $this->_merchantid,
'nonce_str' => self::createNonceStr(),
'total_fee' => intval($totalFee * 100), //订单金额 单位 转为分
'refund_fee' => intval($refundFee * 100), //退款金额 单位 转为分
'sign_type' => 'MD5', //签名类型 支持HMAC-SHA256和MD5,默认为MD5
'transaction_id' => $wxOrderNo, //微信订单号
'out_trade_no' => $orderNo, //商户订单号
'out_refund_no' => $refundNo, //商户退款单号
'refund_desc' => $desc, //退款原因(选填)
);
if (!empty($notify_url)) $unified['notify_url'] = $notify_url;
$unified['sign'] = self::getSign($unified, $this->_key);
$responseXml = $this->curlPost('https://api.mch.weixin.qq.com/secapi/pay/refund', self::arrayToXml($unified));
$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
return $unifiedOrder;
}
public function refundquery($refundNo)
{
$unified = array(
'appid' => $this->_appid,
'mch_id' => $this->_merchantid,
'nonce_str' => self::createNonceStr(),
'sign_type' => 'MD5', //签名类型 支持HMAC-SHA256和MD5,默认为MD5
'out_refund_no' => $refundNo, //商户退款单号
);
$unified['sign'] = self::getSign($unified, $this->_key);
$responseXml = $this->curlPost('https://api.mch.weixin.qq.com/pay/refundquery', self::arrayToXml($unified));
$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
return $unifiedOrder;
}
public static function curlGet($url = '', $options = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https请求 不验证证书和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
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, $this->apiclient_cert);//绝对路径
//默认格式为PEM,可以注释
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $this->apiclient_key);//绝对路径
//第二种方式,两个文件合成一个.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;
}
}
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;
}
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;
}
public static function xmlToArray($xml)
{
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
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;
}
}
微信支付退款php
最新推荐文章于 2024-05-19 16:29:37 发布
620

被折叠的 条评论
为什么被折叠?



