PayToUser.php
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Created by PhpStorm.
* User: DELL
* Date: 2019/5/31
* Time: 9:25
*/
/**
* Created by PhpStorm.
* User: DELL
* Date: 2019/4/9
* Time: 10:58
*/
namespace app\command;
use app\api\model\Third;
use app\api\model\User;
use app\api\model\UserMoneyLog;
use app\api\model\WithdrawLog;
use think\console\Input;
use think\console\Output;
use think\console\Command;
use addons\epay\library\Service;
use think\Db;
use think\Log;
use Yansongda\Pay\Pay;
class PayToUser extends Command{
protected function configure()
{
$this->setName('PayToUser')->setDescription('企业微信付款到用户,每1分钟执行,执行必须带锁防止同时出现两个脚本');
}
protected function execute(Input $input, Output $output)
{
$config = Service::getConfig('wechat');
$pay = new Pay($config);
$withdrawLog = new WithdrawLog();
$withdrawLogList = $withdrawLog->listNeedPay(30);
$third = new Third();
$userMoneyLog = new UserMoneyLog();
try {
foreach ($withdrawLogList as $v) {
$thirdInfo = $third->getOneByUserId($v['user_id']);
$order = [
'partner_trade_no' => $v['order_id'],
'openid' => $thirdInfo['openid'],
'check_name' => 'NO_CHECK',
'amount' => bcmul($v['money'], 10000, 0),
'desc' => '提现到零钱',
'mchid' => '1591095591',
'mch_appid' => 'wxf06ef1898e5e6d55',
'nonce_str' => uniqid(),
];
$res = $pay->driver('wechat')->gateway('transfer')->pay($order);
if(isset($res['return_code']) && ($res['return_code'] == 'SUCCESS') && isset($res['result_code']) && ($res['result_code'] == 'SUCCESS')) {
Db::startTrans();
try {
$insertData = [
'user_id' => $v['user_id'],
'money' => $v['money'],
'memo' => '提现到账',
'createtime' => time(),
];
$res = $userMoneyLog->addOne($insertData);
if(!$res) {
Db::rollback();
} else {
$res = $withdrawLog->updateOne($v['id'], ['status' => 1, 'updatetime' => time(), 'deletetime' => time()]);
if(!$res) {
Db::rollback();
} else {
Db::commit();
}
}
} catch (\Exception $e) {
Db::rollback();
}
} else {
$user = new User();
Db::startTrans();
try {
$userInfo = $user->infoAndLockById($v['user_id']);
if(!$userInfo) {
Db::rollback();
} else {
$insertData = [
'user_id' => $v['user_id'],
'money' => $v['money'],
'memo' => '提现失败,全额返还',
'createtime' => time(),
];
$res = $userMoneyLog->addOne($insertData);
if(!$res) {
Db::rollback();
} else {
$res = $withdrawLog->updateOne($v['id'], ['status' => 2, 'updatetime' => time()]);
if(!$res) {
Db::rollback();
} else { // 返还
$res = $user->updateOne($v['user_id'], ['money' => bcadd($userInfo['money'], $v['money'], 0)]);
if(!$res) {
Db::rollback();
} else {
Db::commit();
}
}
}
}
} catch (\Exception $e) {
Db::rollback();
}
}
}
} catch (\Exception $e) {
Log::write($e, '错误');
}
}
}