计划 自动检查账单

This commit is contained in:
iVampireSP.com 2022-09-18 14:28:29 +08:00
parent 86316342f2
commit d48a665dc4
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 66 additions and 7 deletions

View File

@ -6,6 +6,7 @@
use App\Console\Commands\CalcModule;
use App\Console\Commands\SuspendUserAllHosts;
use App\Console\Commands\UnbanUser;
use App\Jobs\CheckAndChargeBalance;
use App\Jobs\HostCost;
use App\Jobs\ClearTasks;
use App\Jobs\DeleteHost;
@ -48,5 +49,7 @@ protected function schedule(Schedule $schedule)
$schedule->job(new ClearTasks())->weekly();
$schedule->job(new DeleteHost())->hourly();
$schedule->job(new CheckAndChargeBalance())->hourly();
}
}

View File

@ -78,6 +78,11 @@ public function show(Request $request, Balance $balance)
return $this->error('订单已支付');
}
if (now()->diffInDays($balance->created_at) > 1) {
return $this->error('订单已失效');
}
try {
$result = AlipayFactory::payment()->page()->pay("支付", $balance->order_id, $balance->amount, route('balances.return'));
@ -115,7 +120,7 @@ public function return(Request $request)
return $this->success('订单已支付');
}
if ($this->checkAndCharge($request->out_trade_no, $balance)) {
if ($this->checkAndCharge($balance)) {
return view('pay_success');
} else {
return view('pay_error');
@ -139,16 +144,16 @@ public function notify(Request $request)
return $this->success('订单已支付');
}
if ($this->checkAndCharge($request->out_trade_no, $balance)) {
if ($this->checkAndCharge($balance)) {
return $this->success();
} else {
return $this->error();
}
}
public function checkAndCharge($out_trade_no, Balance $balance)
public function checkAndCharge(Balance $balance)
{
$trade = AlipayFactory::payment()->common()->query($out_trade_no);
$trade = AlipayFactory::payment()->common()->query($balance->order_id);
if ($trade->code == "10000" && $trade->tradeStatus == "TRADE_SUCCESS") {
$balance->paid_at = now();
@ -160,13 +165,13 @@ public function checkAndCharge($out_trade_no, Balance $balance)
try {
$balance->user->increment('balance', $trade->totalAmount);
$description = '充值 ' . $trade->totalAmount . ' 元,对端订单号: ' . $out_trade_no;
$description = '充值 ' . $trade->totalAmount . ' 元';
$transaction->addIncomeBalance($balance->user_id, 'alipay', $trade->totalAmount, $description);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
AlipayFactory::payment()->common()->refund($out_trade_no, $trade->totalAmount);
AlipayFactory::payment()->common()->refund($balance->order_id, $trade->totalAmount);
return $this->error($e->getMessage());
}
@ -185,7 +190,8 @@ public function checkAndCharge($out_trade_no, Balance $balance)
// }
public function transactions() {
public function transactions()
{
$transactions = Transaction::thisUser()->latest()->simplePaginate(30);
return $this->success($transactions);

View File

@ -0,0 +1,50 @@
<?php
namespace App\Jobs;
use App\Http\Controllers\User\BalanceController;
use App\Models\User\Balance;
class CheckAndChargeBalance extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
$bc = new BalanceController();
// 查找今天未支付的订单
Balance::where('paid_at', null)->chunk(100, function ($balances) use ($bc) {
foreach ($balances as $balance) {
if (!$bc->checkAndCharge($balance)) {
if (now()->diffInDays($balance->created_at) > 1) {
$balance->delete();
}
}
}
});
// Balance::chunk(100, function ($balances) use ($bc) {
// foreach ($balances as $balance) {
// $bc->checkAndCharge($balance);
// }
// });
}
}