This commit is contained in:
root 2020-01-01 17:55:16 +08:00
parent aa8f5bfa79
commit 1bf03b28c7
2 changed files with 32 additions and 5 deletions

View File

@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Log;
use App\Models\Order; use App\Models\Order;
use App\Models\Plan; use App\Models\Plan;
use App\Models\User; use App\Models\User;
use App\Models\Coupon;
use App\Utils\Helper; use App\Utils\Helper;
use Omnipay\Omnipay; use Omnipay\Omnipay;
use Stripe\Stripe; use Stripe\Stripe;
@ -72,15 +73,26 @@ class OrderController extends Controller
abort(500, '该订阅周期无法进行购买,请选择其他周期'); abort(500, '该订阅周期无法进行购买,请选择其他周期');
} }
if ($request->input('coupon_code')) {
$coupon = Coupon::where('code', $request->input('coupon_code'))->first();
if (!$coupon) {
abort(500, '优惠券无效');
}
if (time() < $coupon->started_at) {
abort(500, '优惠券还未到可用时间');
}
if (time() > $coupon->ended_at) {
abort(500, '优惠券已过期');
}
}
$order = new Order(); $order = new Order();
$order->user_id = $request->session()->get('id'); $order->user_id = $request->session()->get('id');
$order->plan_id = $plan->id; $order->plan_id = $plan->id;
$order->cycle = $request->input('cycle'); $order->cycle = $request->input('cycle');
$order->trade_no = Helper::guid(); $order->trade_no = Helper::guid();
$order->total_amount = $plan[$request->input('cycle')]; $order->total_amount = $plan[$request->input('cycle')];
if ($order->total_amount == 0) { // renew and change subscribe process
$order->status = 1;
}
if ($user->expired_at > time() && $order->plan_id !== $user->plan_id) { if ($user->expired_at > time() && $order->plan_id !== $user->plan_id) {
$order->type = 3; $order->type = 3;
if (!(int)config('v2board.plan_is_update', 1)) abort(500, '目前不允许更改订阅,请联系管理员'); if (!(int)config('v2board.plan_is_update', 1)) abort(500, '目前不允许更改订阅,请联系管理员');
@ -90,6 +102,7 @@ class OrderController extends Controller
} else { } else {
$order->type = 1; $order->type = 1;
} }
// invite process
if ($user->invite_user_id) { if ($user->invite_user_id) {
$order->invite_user_id = $user->invite_user_id; $order->invite_user_id = $user->invite_user_id;
$inviter = User::find($user->invite_user_id); $inviter = User::find($user->invite_user_id);
@ -99,10 +112,21 @@ class OrderController extends Controller
$order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100); $order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
} }
} }
// coupon process
if (isset($coupon)) {
switch ($coupon->type) {
case 1: $order->discount_amount = $order->total_amount - $coupon->value;
case 1: $order->discount_amount = $order->total_amount * ($coupon->value / 100);
}
$order->amount_total = $order->amount_total - $order->discount_amount;
}
// free process
if ($order->total_amount <= 0) {
$order->status = 1;
}
if (!$order->save()) { if (!$order->save()) {
abort(500, '订单创建失败'); abort(500, '订单创建失败');
} }
return response([ return response([
'data' => $order->trade_no 'data' => $order->trade_no
]); ]);

View File

@ -114,3 +114,6 @@ CREATE TABLE `v2_coupon_log` (
`created_at` int(11) NOT NULL, `created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL `updated_at` int(11) NOT NULL
); );
ALTER TABLE `v2_order`
ADD `discount_amount` int(11) NULL AFTER `total_amount`;