Update UserTransfer.php

This commit is contained in:
邹景立 2021-03-18 18:37:37 +08:00
parent bffa774206
commit f6010d56fc

View File

@ -3,6 +3,7 @@
namespace app\data\command; namespace app\data\command;
use app\data\service\DataService; use app\data\service\DataService;
use app\data\service\UserRebateService;
use think\admin\Command; use think\admin\Command;
use think\admin\Exception; use think\admin\Exception;
use think\admin\storage\LocalStorage; use think\admin\storage\LocalStorage;
@ -33,12 +34,13 @@ class UserTransfer extends Command
*/ */
protected function execute(Input $input, Output $output) protected function execute(Input $input, Output $output)
{ {
$map = [['type', 'in', ['wechat_banks', 'wechat_wallet']], ['status', 'in', 3]]; $map = [['type', 'in', ['wechat_banks', 'wechat_wallet']], ['status', 'in', [3, 4]]];
foreach ($this->app->db->name('DataUserTransfer')->where($map)->cursor() as $vo) try { foreach ($this->app->db->name('DataUserTransfer')->where($map)->cursor() as $vo) try {
if ($vo['status'] === 3) {
if ($vo['type'] === 'wechat_banks') { if ($vo['type'] === 'wechat_banks') {
$result = $this->transferBank($vo); $result = $this->createTransferBank($vo);
} else { } else {
$result = $this->transferWallet($vo); $result = $this->createTransferWallet($vo);
} }
if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
$this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([ $this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([
@ -46,13 +48,21 @@ class UserTransfer extends Command
'trade_no' => $result['partner_trade_no'], 'trade_no' => $result['partner_trade_no'],
'trade_time' => $result['payment_time'] ?? date('Y-m-d H:i:s'), 'trade_time' => $result['payment_time'] ?? date('Y-m-d H:i:s'),
'change_time' => date('Y-m-d H:i:s'), 'change_time' => date('Y-m-d H:i:s'),
'change_desc' => '线上微信提现成功', 'change_desc' => '创建微信提现成功',
]); ]);
} else { } else {
$this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([ $this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([
'change_time' => date('Y-m-d H:i:s'), 'change_desc' => $result['err_code_des'] ?? '线上提现失败', 'change_time' => date('Y-m-d H:i:s'), 'change_desc' => $result['err_code_des'] ?? '线上提现失败',
]); ]);
} }
} elseif ($vo['status'] === 4) {
if ($vo['type'] === 'wechat_banks') {
$this->queryTransferBank($vo);
} else {
$this->queryTransferWallet($vo);
}
continue;
}
} catch (\Exception $exception) { } catch (\Exception $exception) {
$this->output->writeln("订单 {$vo['code']} 提现失败,{$exception->getMessage()}"); $this->output->writeln("订单 {$vo['code']} 提现失败,{$exception->getMessage()}");
$this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([ $this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([
@ -73,9 +83,9 @@ class UserTransfer extends Command
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
private function transferBank(array $item): array private function createTransferBank(array $item): array
{ {
$wechat = TransfersBank::instance($this->getPayment()); $wechat = TransfersBank::instance($this->getConfig());
return $wechat->create([ return $wechat->create([
'partner_trade_no' => $item['code'], 'partner_trade_no' => $item['code'],
'enc_bank_no' => $item['bank_code'], 'enc_bank_no' => $item['bank_code'],
@ -97,12 +107,12 @@ class UserTransfer extends Command
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
private function transferWallet(array $item): array private function createTransferWallet(array $item): array
{ {
$config = $this->getPayment(); $config = $this->getConfig();
$wechat = Transfers::instance($config); $wechat = Transfers::instance($config);
$openid = $this->getUserOpenid($item['uid'], $config); $openid = $this->getUserOpenid($item['uid'], $config);
if (empty($openid)) throw new Exception("提现{$item['code']}获取用户OPENID失败"); if (empty($openid)) throw new Exception("提现{$item['code']}用户OPENID失败");
return $wechat->create([ return $wechat->create([
'openid' => $openid, 'openid' => $openid,
'amount' => intval($item['amount'] - $item['charge_amount']) * 100, 'amount' => intval($item['amount'] - $item['charge_amount']) * 100,
@ -113,6 +123,68 @@ class UserTransfer extends Command
]); ]);
} }
/**
* 查询更新提现打款状态
* @param array $item
* @throws Exception
* @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function queryTransferWallet(array $item)
{
$config = $this->getConfig();
$wechat = Transfers::instance($config);
$result = $wechat->query($item['partner_trade_no']);
if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
$this->app->db->name('DataUserTransfer')->where(['code' => $item['code']])->update([
'status' => 5,
'trade_no' => $result['partner_trade_no'],
'trade_time' => $result['payment_time'],
'change_time' => date('Y-m-d H:i:s'),
'change_desc' => '微信提现打款成功',
]);
}
}
/**
* 查询更新提现打款状态
* @param array $item
* @throws Exception
* @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function queryTransferBank(array $item)
{
$config = $this->getConfig();
$wechat = TransfersBank::instance($config);
$result = $wechat->query($item['partner_trade_no']);
if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
if ($result['status'] === 'SUCCESS') {
$this->app->db->name('DataUserTransfer')->where(['code' => $item['code']])->update([
'status' => 5,
'trade_time' => $result['pay_succ_time'] ?: date('Y-m-d H:i:s'),
'change_time' => date('Y-m-d H:i:s'),
'change_desc' => '微信提现打款成功',
]);
}
if (in_array($result['status'], ['FAILED', 'BANK_FAIL'])) {
$this->app->db->name('DataUserTransfer')->where(['code' => $item['code']])->update([
'status' => 0,
'change_time' => date('Y-m-d H:i:s'),
'change_desc' => '微信提现打款失败',
]);
// 刷新用户可提现余额
UserRebateService::instance()->amount($item['uid']);
}
}
}
/** /**
* 根据配置获取用户OPENID * 根据配置获取用户OPENID
* @param int $uid * @param int $uid
@ -130,11 +202,11 @@ class UserTransfer extends Command
if (!empty($user['openid1'])) return $user['openid1']; if (!empty($user['openid1'])) return $user['openid1'];
if (!empty($user['openid2'])) return $user['openid2']; if (!empty($user['openid2'])) return $user['openid2'];
} }
if ($config['type'] === 'wxapp' && $user['openid1']) { if ($config['type'] === 'wxapp') {
return $user['openid1']; if (!empty($user['openid1'])) return $user['openid1'];
} }
if ($config['type'] === 'wechat' && !empty($user['openid2'])) { if ($config['type'] === 'wechat') {
return $user['openid2']; if (!empty($user['openid2'])) return $user['openid2'];
} }
return null; return null;
} }
@ -147,19 +219,25 @@ class UserTransfer extends Command
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
protected function getPayment(): array private function getConfig(): array
{ {
$data = sysdata('TransferWxpay'); $data = sysdata('TransferWxpay');
if (empty($data)) throw new Exception('未配置微信提现商户'); if (empty($data)) throw new Exception('未配置微信提现商户');
$key1 = LocalStorage::instance()->set("{$data['wechat_mch_id']}_key.pem", $data['wechat_mch_key_text'], true); // 商户证书文件处理
$key2 = LocalStorage::instance()->set("{$data['wechat_mch_id']}_cert.pem", $data['wechat_mch_cert_text'], true); $local = LocalStorage::instance();
if (!$local->has($file1 = "{$data['wechat_mch_id']}_key.pem", true)) {
$local->set($file1, $data['wechat_mch_key_text'], true);
}
if (!$local->has($file2 = "{$data['wechat_mch_id']}_cert.pem", true)) {
$local->set($file2, $data['wechat_mch_cert_text'], true);
}
return [ return [
'type' => $data['wechat_type'], 'type' => $data['wechat_type'],
'appid' => $data['wechat_appid'], 'appid' => $data['wechat_appid'],
'mch_id' => $data['wechat_mch_id'], 'mch_id' => $data['wechat_mch_id'],
'mch_key' => $data['wechat_mch_key'], 'mch_key' => $data['wechat_mch_key'],
'ssl_key' => $key1['file'], 'ssl_key' => $local->path($file1),
'ssl_cer' => $key2['file'], 'ssl_cer' => $local->path($file2),
'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat', 'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat',
]; ];
} }