mirror of
https://github.com/v2board/v2board.git
synced 2024-11-10 09:39:10 +08:00
add balance payment
This commit is contained in:
parent
03e0b5d087
commit
111d2720bd
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\OrderService;
|
||||
use Illuminate\Console\Command;
|
||||
use App\Models\Order;
|
||||
use App\Models\User;
|
||||
@ -42,14 +43,14 @@ class CheckOrder extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$order = Order::get();
|
||||
foreach ($order as $item) {
|
||||
$orders = Order::get();
|
||||
foreach ($orders as $item) {
|
||||
switch ($item->status) {
|
||||
// cancel
|
||||
case 0:
|
||||
if (strtotime($item->created_at) <= (time() - 1800)) {
|
||||
$item->status = 2;
|
||||
$item->save();
|
||||
$orderService = new OrderService($item);
|
||||
$orderService->cancel();
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
|
@ -4,6 +4,7 @@ namespace App\Http\Controllers\User;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\User\OrderSave;
|
||||
use App\Services\UserService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@ -162,8 +163,7 @@ class OrderController extends Controller
|
||||
$order->cycle = $request->input('cycle');
|
||||
$order->trade_no = Helper::guid();
|
||||
$order->total_amount = $plan[$request->input('cycle')];
|
||||
// discount start
|
||||
// coupon
|
||||
// coupon start
|
||||
if (isset($coupon)) {
|
||||
switch ($coupon->type) {
|
||||
case 1:
|
||||
@ -181,13 +181,13 @@ class OrderController extends Controller
|
||||
}
|
||||
}
|
||||
}
|
||||
// user
|
||||
// coupon complete
|
||||
// discount start
|
||||
if ($user->discount) {
|
||||
$order->discount_amount = $order->discount_amount + ($order->total_amount * ($user->discount / 100));
|
||||
}
|
||||
// discount complete
|
||||
$order->total_amount = $order->total_amount - $order->discount_amount;
|
||||
// discount end
|
||||
$order->total_amount = $order->total_amount - $order->discount_amount;
|
||||
// renew and change subscribe process
|
||||
if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) {
|
||||
if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单');
|
||||
@ -217,6 +217,27 @@ class OrderController extends Controller
|
||||
}
|
||||
}
|
||||
}
|
||||
// use balance
|
||||
if ($user->balance && $order->total_amount > 0) {
|
||||
$remainingBalance = $user->balance - $order->total_amount;
|
||||
$userService = new UserService();
|
||||
if ($remainingBalance > 0) {
|
||||
if (!$userService->addBalance($order->user_id, $order->total_amount)) {
|
||||
DB::rollBack();
|
||||
abort(500, '余额不足');
|
||||
}
|
||||
$order->balance_amount = $order->total_amount;
|
||||
$order->total_amount = 0;
|
||||
} else {
|
||||
if (!$userService->addBalance($order->user_id, $user->balance)) {
|
||||
DB::rollBack();
|
||||
abort(500, '余额不足');
|
||||
}
|
||||
$order->balance_amount = $user->balance;
|
||||
$order->total_amount = $order->total_amount - $user->balance;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$order->save()) {
|
||||
DB::rollback();
|
||||
abort(500, '订单创建失败');
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Order;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class OrderService
|
||||
{
|
||||
@ -13,8 +14,20 @@ class OrderService
|
||||
$this->order = $order;
|
||||
}
|
||||
|
||||
public function cancel()
|
||||
public function cancel():void
|
||||
{
|
||||
|
||||
$order = $this->order;
|
||||
DB::beginTransaction();
|
||||
$order->status = 2;
|
||||
if (!$order->save()) {
|
||||
DB::rollBack();
|
||||
}
|
||||
if ($order->balance_amount) {
|
||||
$userService = new UserService();
|
||||
if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
|
||||
DB::rollBack();
|
||||
}
|
||||
}
|
||||
DB::commit();
|
||||
}
|
||||
}
|
||||
|
@ -47,4 +47,20 @@ class UserService
|
||||
{
|
||||
return User::all();
|
||||
}
|
||||
|
||||
public function addBalance(int $userId, int $balance):bool
|
||||
{
|
||||
$user = User::find($userId);
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
$user->balance = $user->balance + $balance;
|
||||
if ($user->balance < 0) {
|
||||
return false;
|
||||
}
|
||||
if (!$user->save()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user