123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\admin\logic;
-
-
- use app\common\model\Pay;
- use app\common\server\WeChatServer;
- use think\facade\Db;
-
-
- class WechatMerchantTransferLogic
- {
-
-
- public static function transfer($withdrawApply)
- {
-
- if($withdrawApply['left_money'] < 1) {
- return [
- 'code' => 0,
- 'msg' => '扣除手续费后提现金额不能小于1元'
- ];
- }
-
- $count = Db::name('withdraw_apply')
- ->whereTime('update_time', 'd')
- ->where('user_id', $withdrawApply['user_id'])
- ->where('type', 2)
- ->where('status', '>=', 2)
- ->count();
- if($count >= 7) {
- return [
- 'code' => 0,
- 'msg' => '同一个用户一天最多可付款7次'
- ];
- }
-
- $sum = Db::name('withdraw_apply')
- ->whereTime('update_time', 'd')
- ->where('type', 2)
- ->where('status', 'in', [2, 3])
- ->sum('left_money');
- $sum = $sum + $withdrawApply['left_money'];
- if($sum > 100000) {
- return [
- 'code' => 0,
- 'msg' => '一个商户默认同一日付款总额限额10万元'
- ];
- }
-
-
- $userAuth = Db::name('user_auth')->where('user_id', $withdrawApply['user_id'])->order('client', 'asc')->find();
- if(!$userAuth) {
-
- return [
- 'code'=> 0,
- 'msg' => '查询不到该用户的openid'
- ];
- }
-
-
- $config = WeChatServer::getPayConfigBySource($userAuth['client'])['config'];
-
-
-
- $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
-
- $http_method = 'POST';
-
- $data = [
- 'appid' => $config['app_id'],
- 'out_batch_no' => $withdrawApply['batch_no'],
- 'batch_name' => '提现至微信零钱',
- 'batch_remark' => '提现至微信零钱',
- 'total_amount' => $withdrawApply['left_money'] * 100,
- 'total_num' => 1,
- 'transfer_detail_list' => [
- [
- 'out_detail_no' => $withdrawApply['sn'],
- 'transfer_amount' => $withdrawApply['left_money'] * 100,
- 'transfer_remark' => '提现至微信零钱',
- 'openid' => $userAuth['openid'],
- ]]
- ];
- if ($withdrawApply['left_money'] >= 2000) {
- if (empty($withdrawApply['real_name'])) {
- throw new \Exception('转账金额 >= 2000元,收款用户真实姓名必填');
- }
- $data['transfer_detail_list'][0]['user_name'] = self::getEncrypt($withdrawApply['real_name'],$config);
- }
-
- $token = self::token($url,$http_method,$data,$config);
- $result = self::https_request($url,json_encode($data),$token);
- $result_arr = json_decode($result,true);
-
- if(!isset($result_arr['create_time'])) {
-
-
- $msg = $result_arr['message'] ?? '提现失败,请稍后再试';
- if ($msg == '产品权限异常') {
- $msg = '请在商户平台-商家转账产品设置中开通产品权限';
- }
-
- return [
- 'code' => 0,
- 'msg' => $msg,
- ];
- }
-
-
- Db::name('withdraw_apply')
- ->where('id', $withdrawApply['id'])
- ->update([
- 'status' => 2,
- 'update_time' => time(),
- 'pay_desc' => $result
- ]);
-
- return [
- 'code' => 1,
- 'msg' => '零钱提现中'
- ];
- }
-
-
-
- public static function token($url,$http_method,$data,$config)
- {
- $timestamp = time();
- $url_parts = parse_url($url);
- $nonce = $timestamp.rand('10000','99999');
- $body = empty($data) ? '' : json_encode((object)$data);
- $stream_opts = [
- "ssl" => [
- "verify_peer"=>false,
- "verify_peer_name"=>false,
- ]
- ];
-
- $apiclient_cert_arr = openssl_x509_parse(file_get_contents($config['cert_path'],false, stream_context_create($stream_opts)));
- $serial_no = $apiclient_cert_arr['serialNumberHex'];
- $mch_private_key = file_get_contents($config['key_path'],false, stream_context_create($stream_opts));
- $merchant_id = $config['mch_id'];
- $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 $schema.' '.$token;
- }
-
-
-
- public static function https_request($url,$data,$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:'.$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 static function getEncrypt($str,$config)
- {
-
- $public_key = file_get_contents($config['cert_path']);
- $encrypted = '';
- if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) {
-
- $sign = base64_encode($encrypted);
- } else {
- throw new \Exception('encrypt failed');
- }
- return $sign;
- }
-
-
-
- public static function details($withdrawApply)
- {
- $userAuth = Db::name('user_auth')->where('user_id', $withdrawApply['user_id'])->order('client', 'asc')->find();
- if(!$userAuth) {
-
- return [
- 'code'=> 0,
- 'msg' => '查询不到该用户的openid'
- ];
- }
-
- $pay = Pay::where(['code' => 'wechat'])->find()->toArray();
- $config = [
- 'mch_id' => $pay['config']['mch_id'] ?? '',
- 'cert_path' => dirname(__FILE__, 2).'/../../public/'.$pay['config']['apiclient_cert'] ?? '',
- 'key_path' => dirname(__FILE__, 2).'/../../public/'.$pay['config']['apiclient_key'] ?? '',
- ];
-
-
- $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches/out-batch-no/'.$withdrawApply['batch_no'].'/details/out-detail-no/'.$withdrawApply['sn'];
-
- $http_method = 'GET';
-
- $data = [];
- $token = self::token($url,$http_method,$data,$config);
- $result = self::https_request($url,$data,$token);
- $result_arr = json_decode($result,true);
- return $result_arr;
- }
- }
|