返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >最新PHP对接微信支付,发起商家转账API,商家转账到零钱
  • 459
分享到

最新PHP对接微信支付,发起商家转账API,商家转账到零钱

ThinkPHP5.0php微信 2023-09-26 09:09:11 459人浏览 薄情痞子
摘要

wxapp = $wxapp;    }        public function transfer($batch_name, $out_trade_no, $money, $openid)    {        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';        $pars = [];        $pars['appid'] = $this->wxapp['app_id'];//直连商户的appid        $pars['out_batch_no'] = 'batch' . $out_trade_no;//商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一        $pars['batch_name'] = $batch_name;//该笔批量转账的名称        $pars['batch_remark'] = $batch_name;//转账说明,UTF8编码,最多允许32个字符        $pars['total_amount'] = intval($money * 100);//转账总金额 单位为“分”        $pars['total_num'] = 1;//转账总笔数        $pars['transfer_detail_list'][0] = [            'out_detail_no' => 'transfer' . $out_trade_no,            'transfer_amount' => $pars['total_amount'],            'transfer_remark' => $batch_name,            'openid' => $openid        ];//转账明细列表        $token = $this->getToken($pars);//获取token        $res = $this->https_request($url, json_encode($pars), $token);//发送请求        $data = json_decode($res, true);        if(isset($data['batch_id']) && $data['batch_id']){            return $data;        }        return [];    }    function https_request($url, $data = null, $token)    {        $curl = curl_init();        curl_setopt($curl, CURLOPT_URL, (string)$url);        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);        if (!empty($data)) {            curl_setopt($curl, CURLOPT_POST, 1);            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        }        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        //添加请求头        $headers = [            'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $token,            'Accept: application/json',            'Content-Type: application/json; charset=utf-8',            'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',        ];        if (!empty($headers)) {            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);        }        $output = curl_exec($curl);        curl_close($curl);        return $output;    }    public function getToken($pars)    {        // $url = 'https://api.mch.weixin.qq.com/v3/certificates';        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';        $http_method = 'POST';//请求方法(GET,POST,PUT)        $timestamp = time();//请求时间戳        $url_parts = parse_url($url);//获取请求的绝对URL        $nonce = $timestamp . rand('10000', '99999');//请求随机串        $body = json_encode((object)$pars);//请求报文主体        $stream_opts = [            "ssl" => [                "verify_peer" => false,                "verify_peer_name" => false,            ]        ];        try {            $certPem = $this->getCertPem();            $apiclient_cert_path = $certPem['certPem'];            $apiclient_key_path = $certPem['keyPem'];            $apiclient_cert_arr = openssl_x509_parse(file_get_contents($apiclient_cert_path, false, stream_context_create($stream_opts)));            $serial_no = $apiclient_cert_arr['serialNumberHex'];//证书序列号            $mch_private_key = file_get_contents($apiclient_key_path, false, stream_context_create($stream_opts));//密钥            $merchant_id = $this->wxapp['mchid'];            $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));            $message = $http_method . "\n" .                $canonical_url . "\n" .                $timestamp . "\n" .                $nonce . "\n" .                $body . "\n";            openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');            $sign = base64_encode($raw_sign);//签名            $schema = 'WECHATPAY2-SHA256-RSA2048';            $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',                $merchant_id, $nonce, $timestamp, $serial_no, $sign);//微信返回token        } catch (\Exception $e) {            throw new BaseException(['msg' => $e->getMessage()]);        }        return $token;    }        private function getCertPem()    {        if (empty($this->wxapp['cert_pem']) || empty($this->wxapp['key_pem'])) {            throw new BaseException(['msg' => '请先到后台小程序设置填写微信支付证书文件']);        }        // cert目录        $filePath = dirname(__DIR__) . '/library/wechat/cert/' . $this->wxapp['wxapp_id'] . '/';        return [            'certPem' => $filePath . 'cert.pem',            'keyPem' => $filePath . 'key.pem'        ];    }}

最新PHP对接微信支付,发起商家转账api,商家转账到零钱

上面类文件可直接使用,根据自己需求改动即可,下方是微信支付官方的接口文档

发起商家转账API

来源地址:https://blog.csdn.net/qq_30036559/article/details/127595933

--结束END--

本文标题: 最新PHP对接微信支付,发起商家转账API,商家转账到零钱

本文链接: https://lsjlt.com/news/418282.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作