<?php
namespace app\common\controller;
use app\index\controller\Base;
use think\Db;
use think\facade\Env;
class Wxtuikuan extends Base
{private $SSLCERT_PATH; private $SSLKEY_PATH; protected $mchid;protected $appid;public function __construct(){$config=Db::name('setting')->find(1);$this->SSLCERT_PATH = 'public'.$config['apiclient_cert'];$this->SSLKEY_PATH = 'public'.$config['apiclient_key'];$this->mchid = $config['MCHID']; $this->appid = $config['APPID']; }public function myTixian(){$info['amount']=1;$info['orderno']=generateOrderNumber(6);$info['openid']='o7nYL5QN7eaAYw1HyxCEBzWbwa_k';$this->tixian($info);}public function tixian($info = []){if (empty($info)){return false;}$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';$batch_name = '用户提现';$pars = [];$pars['appid'] = $this->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(strval($info['amount'] * 100));$pars['total_num'] = 1;$pars['transfer_detail_list'][0] = ['out_detail_no'=> $info['orderno'],'transfer_amount'=>intval(strval($info['amount'] * 100)),'transfer_remark'=>$batch_name,'openid'=>$info['openid'],];$token = $this->getToken($pars,$this->mchid);$res = $this->https_request($url,json_encode($pars),$token);$resArr = json_decode($res,true);return $resArr;}public function getToken($pars,$merchant_id){$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 = Env::get('root_path').$this->SSLCERT_PATH;$apiclient_key_path = Env::get('root_path').$this->SSLKEY_PATH;$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));$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;}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;}}