tp6 v3微信退款

   /**
     * @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;


    }
   // 生成随机字符串
    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格式
    }

调用

    $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;