作者 谢百川

提现脚本完成

... ... @@ -80,7 +80,7 @@ class Money extends Api
]
})
*/
public function getMoneyLog()
public function listMoneyLog()
{
$userId = $this->auth->id;
$userId = 1;
... ... @@ -146,6 +146,7 @@ class Money extends Api
'money' => $money,
'createtime' => time(),
'updatetime' => time(),
'order_id' => date("YmdHis", time()) . mt_rand(100000, 999999)
];
$res = $withdrawLog->addOne($insertData);
if(!$res) {
... ...
... ... @@ -20,4 +20,15 @@ class WithdrawLog extends Model
{
return $this->insert($data);
}
// 获取需要支付的提现
public function listNeedPay($size)
{
return $this->where(['pay_switch' => ['=', 1], 'status' => ['=', 0]])->useSoftDelete($this->deleteTime)->limit(30)->order('id', 'asc')->select();
}
public function updateOne($id, $data)
{
return $this->where(['id' => ['=', $id]])->update($data);
}
}
\ No newline at end of file
... ...
... ... @@ -17,4 +17,5 @@ return [
'app\admin\command\Min',
'app\admin\command\Addon',
'app\admin\command\Api',
'app\command\PayToUser',
];
... ...
<?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\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();
foreach ($withdrawLogList as $v) {
$thirdInfo = $third->getOneByUserId($v['user_id']);
$order = [
'partner_trade_no' => $v['order_id'],
'open_id' => $thirdInfo['openid'],
'check_name' => 'NO_CHECK',
'amount' => $v['money'],
'desc' => '提现到零钱',
];
$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')) {
$withdrawLog->updateOne($v['id'], ['status' => 1, 'updatetime' => time()]);
} else {
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' => 2, 'updatetime' => time()]);
if(!$res) {
Db::rollback();
} else {
Db::commit();
}
}
} catch (\Exception $e) {
Db::rollback();
Log::write('提现失败,返还到用户账户失败', 'debug');
}
}
}
}
}
\ No newline at end of file
... ...