tp6 v3微信退款

php 复制代码
   /**
     * @Notes:退款
     * @param $out_trade_no 支付时候订单号(order表 original_bn)两个参数选一个这个要选对
     * @param $out_refund_no 退款订单号
     * @param $total 订单金额
     * @param $refund 退款金额
     * @Time: 2023-08-10
     */
    public function refundMoney($out_trade_no , $out_refund_no , $total , $refund)
    {
        $config = config('wechat.wechat');
        $time=time();
        $refundData=[
            'out_refund_no'=>$out_refund_no,	//退款订单号自己生成就好
            'reason'=>'商品退款',
            'notify_url'=>$config['pay']['refundNotifyUrl'],//退款回调地址
            'out_trade_no' => $out_trade_no,
            'amount'=>[
                'refund'=> $refund, //退款标价金额,单位为分,可以做部分退款
                'total'=> $total, //订单总金额,单位为分
                'currency'=>'CNY'
            ]
        ];
        $url='https://api.mch.weixin.qq.com/v3/refund/domestic/refunds';
        $urlarr = parse_url($url); //拆解为:[scheme=>https,host=>api.mch.weixin.qq.com,path=>/v3/pay/transactions/native]
        $mchid =$config['pay']['merchantId'];//商户ID
        $xlid = $config['pay']['serialNumber'];//证书序列号
        $refundData=json_encode($refundData);
        $nonce = $this->randomString();
        $key = $this->getSign($refundData,$urlarr['path'],$nonce,$time);
        $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"',$mchid,$xlid,$nonce,$time,$key);
        $header  = array(
            'Accept: application/json',
            'Content-Type: application/json',
            'User-Agent:*/*',
            'Authorization: WECHATPAY2-SHA256-RSA2048 '.$token
        );
        $res=$this->curl_post_https($url,$refundData,$header);
        $res_array=json_decode($res,true);
        return $res_array;


    }
php 复制代码
   // 生成随机字符串
    private function randomString($len = 32)
    {
        $string = '';
        $char = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        for ($i = 0; $i < $len; $i++) {
            $string .= $char[mt_rand(0, strlen($char) - 1)];
        }
        return $string;
    }


    //微信支付签名
    function getSign($data=array(),$url,$randstr,$time){
        $str = "POST"."\n".$url."\n".$time."\n".$randstr."\n".$data."\n";
        $key = file_get_contents(getcwd() .'/cert/apiclient_key.pem');//在商户平台下载的秘钥,读取到变量 最好是放到public下边 
        $str = $this->getSha256WithRSA($str,$key);
        return $str;
    }


    //加密
    public function getSha256WithRSA($content, $privateKey){
        $binary_signature = "";
        $algo = "SHA256";
        openssl_sign($content, $binary_signature, $privateKey, $algo);
        $sign = base64_encode($binary_signature);
        return $sign;
    }
    //退款用
    function curl_post_https($url,$data,$header){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在,如果出错则修改为0,默认为1
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据,json格式
    }

调用

php 复制代码
    $res = WxPay::instance()->refundMoney($order['original_bn'] , $order['order_bn'] , $sum , $order['total_money']);

        $msg = '';
        if (!empty($res['code'])) {
            $msg = $res['message'];
        }
        if (!empty($res['status'])) {
            if ($res['status'] == 'SUCCESS'){
                $msg = '退款成功';
            } else if ($res['status'] == 'CLOSED') {
                $msg = '退款关闭';
            } else if ($res['status'] == 'PROCESSING') {
                $msg = '退款处理中';
            } else if ($res['status'] == 'ABNORMAL') {
                $msg = '退款异常';
            }
        }
        return $msg;
相关推荐
凌奕6 小时前
微信小程序接入微信 AI:让用户"说一句话"就能下单
微信·微信小程序·agent
弓乙图1 天前
弓乙歌/岐黄真源赋
经验分享·微信
2601_961194021 天前
初中英语教资笔试资源|科三教案模板和知识点资料
百度·微信·微信公众平台·facebook·twitter·新浪微博
梁辰兴2 天前
微信小程序开发者工具下载与安装
微信·微信小程序·小程序·教程·软件安装·开发者工具
Thomas_YXQ4 天前
Unity无GC读取图片与网格完整方案
大数据·人工智能·unity·微信·产品运营
山川而川-R4 天前
调用微信开源二维码模型
微信·开源
凌涘5 天前
依托 BEM 规范深度剖析 WeUI 微信按钮组件开发与实现
前端·微信
m0_696212686 天前
如何用微信 API 实现多群消息同步和定时群发
微信
罗光记6 天前
Solon Server 启动模式深度解析:从 0.3MB 内核到 10+ Server 插件
其他·百度·微信·微信公众平台·新浪微博
阳明山水9 天前
销量预测模型评估进阶:从 MAPE 到库存周转率与缺货损失金额
人工智能·机器学习·微信·微信公众平台·微信开放平台