|
@@ -0,0 +1,122 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace app\admin\controller;
|
|
|
|
+
|
|
|
|
+use think\Response;
|
|
|
|
+use think\Config;
|
|
|
|
+
|
|
|
|
+ * 示例接口
|
|
|
|
+ */
|
|
|
|
+class Demo
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ protected $noNeedLogin = ['*'];
|
|
|
|
+
|
|
|
|
+ protected $noNeedRight = ['*'];
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * 测试方法
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ public function test()
|
|
|
|
+ {
|
|
|
|
+ $batch_name = '余额提现';
|
|
|
|
+ $out_trade_no = time().rand(1000,9999);
|
|
|
|
+ $money = 1;
|
|
|
|
+ $openid = 'o4a9g5cDIEVzS7zVzE-_VEK8y9Yg';
|
|
|
|
+ $this->tixian($batch_name,$out_trade_no,$money,$openid);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function tixian($batch_name,$out_trade_no,$money,$openid){
|
|
|
|
+ $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
|
|
|
|
+ $pars = [];
|
|
|
|
+ $pars['appid'] = config('wxconfig.appId');
|
|
|
|
+ $pars['out_batch_no'] = 'sjzz'.date('Ymd').mt_rand(1000, 9999);
|
|
|
|
+ $pars['batch_name'] = $batch_name;
|
|
|
|
+ $pars['batch_remark'] = $batch_name;
|
|
|
|
+ $pars['total_amount'] = intval($money * 100);
|
|
|
|
+ $pars['total_num'] = 1;
|
|
|
|
+ $pars['transfer_detail_list'][0] = [
|
|
|
|
+ 'out_detail_no'=>'Dh'.$out_trade_no,
|
|
|
|
+ 'transfer_amount'=>$pars['total_amount'],
|
|
|
|
+ 'transfer_remark'=>$batch_name,
|
|
|
|
+ 'openid'=>$openid
|
|
|
|
+ ];
|
|
|
|
+ $token = $this->getToken($pars);
|
|
|
|
+ $res = $this->https_request($url,json_encode($pars),$token);
|
|
|
|
+ $resArr = json_decode($res,true);
|
|
|
|
+ halt($resArr);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ 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/transfer/batches';
|
|
|
|
+ $http_method = 'POST';
|
|
|
|
+ $timestamp = time();
|
|
|
|
+ $url_parts = parse_url($url);
|
|
|
|
+ $nonce = $timestamp.rand('10000','99999');
|
|
|
|
+ $body = json_encode((object)$pars);
|
|
|
|
+ $stream_opts = [
|
|
|
|
+ "ssl" => [
|
|
|
|
+ "verify_peer"=>false,
|
|
|
|
+ "verify_peer_name"=>false,
|
|
|
|
+ ]
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ $apiclient_cert_path = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/apiclient/apiclient_cert.pem';
|
|
|
|
+ $apiclient_key_path = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/apiclient/apiclient_key.pem';
|
|
|
|
+
|
|
|
|
+ $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 = config('wxconfig.payMchId');
|
|
|
|
+ $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);
|
|
|
|
+ return $token;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|