From 4301d7e4ab61ac3015510b7d884b9272d3264b21 Mon Sep 17 00:00:00 2001 From: Tokumeikoi Date: Thu, 9 Apr 2020 13:35:27 +0800 Subject: [PATCH] update refund process --- app/Console/Commands/CheckOrder.php | 48 ++++++---- app/Http/Controllers/User/OrderController.php | 91 ++++++++----------- app/Services/CouponService.php | 48 ++++++++++ app/Services/OrderService.php | 5 + database/update.sql | 5 + 5 files changed, 125 insertions(+), 72 deletions(-) create mode 100644 app/Services/CouponService.php diff --git a/app/Console/Commands/CheckOrder.php b/app/Console/Commands/CheckOrder.php index 75144c11..4a0e3043 100755 --- a/app/Console/Commands/CheckOrder.php +++ b/app/Console/Commands/CheckOrder.php @@ -9,6 +9,7 @@ use App\Models\User; use App\Models\Plan; use App\Utils\Helper; use App\Models\Coupon; +use Illuminate\Support\Facades\DB; class CheckOrder extends Command { @@ -65,10 +66,37 @@ class CheckOrder extends Command { $user = User::find($order->user_id); $plan = Plan::find($order->plan_id); - if ((string)$order->cycle === 'onetime_price') { - return $this->buyByOneTime($order, $user, $plan); + + if ($order->refund_amount) { + $user->balance = $user->balance + $order->refund_amount; } - return $this->buyByCycle($order, $user, $plan); + DB::beginTransaction(); + if ($order->surplus_order_ids) { + try { + Order::whereIn('id', json_decode($order->surplus_order_ids))->update([ + 'status' => 4 + ]); + } catch (\Exception $e) { + DB::rollback(); + abort(500, '开通失败'); + } + } + if ((string)$order->cycle === 'onetime_price') { + $this->buyByOneTime($order, $user, $plan); + } else { + $this->buyByCycle($order, $user, $plan); + } + if (!$user->save()) { + DB::rollBack(); + abort(500, '开通失败'); + } + $order->status = 3; + if (!$order->save()) { + DB::rollBack(); + abort(500, '开通失败'); + } + + DB::commit(); } private function buyByCycle(Order $order, User $user, Plan $plan) @@ -77,9 +105,6 @@ class CheckOrder extends Command if ((int)$order->type === 3) { $user->expired_at = time(); } - if ($order->refund_amount) { - $user->balance = $user->balance + $order->refund_amount; - } $user->transfer_enable = $plan->transfer_enable * 1073741824; if ((int)config('v2board.renew_reset_traffic_enable', 1)) { $user->u = 0; @@ -88,27 +113,16 @@ class CheckOrder extends Command $user->plan_id = $plan->id; $user->group_id = $plan->group_id; $user->expired_at = $this->getTime($order->cycle, $user->expired_at); - if ($user->save()) { - $order->status = 3; - $order->save(); - } } private function buyByOneTime(Order $order, User $user, Plan $plan) { - if ($order->refund_amount) { - $user->balance = $user->balance + $order->refund_amount; - } $user->transfer_enable = $plan->transfer_enable * 1073741824; $user->u = 0; $user->d = 0; $user->plan_id = $plan->id; $user->group_id = $plan->group_id; $user->expired_at = NULL; - if ($user->save()) { - $order->status = 3; - $order->save(); - } } private function getTime($str, $timestamp) diff --git a/app/Http/Controllers/User/OrderController.php b/app/Http/Controllers/User/OrderController.php index c182fd21..36a61c4c 100755 --- a/app/Http/Controllers/User/OrderController.php +++ b/app/Http/Controllers/User/OrderController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\User; use App\Http\Controllers\Controller; use App\Http\Requests\User\OrderSave; +use App\Services\CouponService; use App\Services\OrderService; use App\Services\UserService; use Illuminate\Http\Request; @@ -74,14 +75,15 @@ class OrderController extends Controller } // surplus value - private function getSurplusValue(User $user) + private function getSurplusValue(User $user, Order $order) { - $plan = Plan::find($user->plan_id); - if ($user->expired_at === NULL) { - return $this->getSurplusValueByOneTime($user, $plan); - } else { - return $this->getSurplusValueByCycle($user, $plan); - } +// $plan = Plan::find($user->plan_id); +// if ($user->expired_at === NULL) { +// $this->getSurplusValueByOneTime($user, $plan); +// } else { +// $this->getSurplusValueByCycle($user, $order); +// } + $this->getSurplusValueByCycle($user, $order); } private function getSurplusValueByOneTime(User $user, Plan $plan) @@ -95,25 +97,29 @@ class OrderController extends Controller return $result > 0 ? $result : 0; } - private function getSurplusValueByCycle(User $user, Plan $plan) + private function getSurplusValueByCycle(User $user, Order $order) { - $price = 0; - if ($plan->month_price) { - $price = $plan->month_price / (31536000 / 12); - } else if ($plan->quarter_price) { - $price = $plan->quarter_price / (31536000 / 4); - } else if ($plan->half_year_price) { - $price = $plan->half_year_price / (31536000 / 2); - } else if ($plan->year_price) { - $price = $plan->year_price / 31536000; + $strToMonth = [ + 'month_price' => 1, + 'quarter_price' => 3, + 'half_year_price' => 6, + 'year_price' => 12 + ]; + $orderModel = Order::where('user_id', $user->id)->where('status', 3); + + $totalValue = $orderModel->sum('total_amount') + $orderModel->sum('balance_amount'); + info('剩余价值' . $totalValue); + $totalMonth = 0; + foreach ($orderModel->get() as $item) { + $totalMonth = $totalMonth + $strToMonth[$item->cycle]; } - // exclude discount - if ($user->discount && $price) { - $price = $price - ($price * $user->discount / 100); - } - $remainingDay = $user->expired_at - time(); - $result = $remainingDay * $price; - return $result > 0 ? $result : 0; + info('剩余月份' . $totalMonth); + $unitPrice = $totalValue / $totalMonth; + info('单价' . $unitPrice); + $remainingMonth = ($user->expired_at - time()) / 2678400; + $result = $unitPrice * $remainingMonth; + $order->surplus_amount = $result > 0 ? $result : 0; + $order->surplus_order_ids = json_encode(array_map(function ($v) { return $v['id'];}, $orderModel->get()->toArray())); } public function save(OrderSave $request) @@ -141,21 +147,6 @@ class OrderController extends Controller abort(500, '该订阅周期无法进行购买,请选择其他周期'); } - if ($request->input('coupon_code')) { - $coupon = Coupon::where('code', $request->input('coupon_code'))->first(); - if (!$coupon) { - abort(500, '优惠券无效'); - } - if ($coupon->limit_use <= 0 && $coupon->limit_use !== NULL) { - abort(500, '优惠券已无可用次数'); - } - if (time() < $coupon->started_at) { - abort(500, '优惠券还未到可用时间'); - } - if (time() > $coupon->ended_at) { - abort(500, '优惠券已过期'); - } - } DB::beginTransaction(); $order = new Order(); @@ -165,21 +156,11 @@ class OrderController extends Controller $order->trade_no = Helper::guid(); $order->total_amount = $plan[$request->input('cycle')]; // coupon start - if (isset($coupon)) { - switch ($coupon->type) { - case 1: - $order->discount_amount = $coupon->value; - break; - case 2: - $order->discount_amount = $order->total_amount * ($coupon->value / 100); - break; - } - if ($coupon->limit_use !== NULL) { - $coupon->limit_use = $coupon->limit_use - 1; - if (!$coupon->save()) { - DB::rollback(); - abort(500, '优惠券使用失败'); - } + if ($request->input('coupon_code')) { + $couponService = new CouponService($request->input('coupon_code')); + if (!$couponService->use($order)) { + DB::rollBack(); + abort(500, '优惠券使用失败'); } } // coupon complete @@ -193,7 +174,7 @@ class OrderController extends Controller if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) { if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单'); $order->type = 3; - $order->surplus_amount = $this->getSurplusValue($user); + $this->getSurplusValue($user, $order); if ($order->surplus_amount >= $order->total_amount) { $order->refund_amount = $order->surplus_amount - $order->total_amount; $order->total_amount = 0; diff --git a/app/Services/CouponService.php b/app/Services/CouponService.php new file mode 100644 index 00000000..aa8888b7 --- /dev/null +++ b/app/Services/CouponService.php @@ -0,0 +1,48 @@ +coupon = Coupon::where('code', $code)->first(); + if (!$this->coupon) { + abort(500, '优惠券无效'); + } + if ($this->coupon->limit_use <= 0 && $this->coupon->limit_use !== NULL) { + abort(500, '优惠券已无可用次数'); + } + if (time() < $this->coupon->started_at) { + abort(500, '优惠券还未到可用时间'); + } + if (time() > $this->coupon->ended_at) { + abort(500, '优惠券已过期'); + } + } + + public function use(Order $order) + { + switch ($this->coupon->type) { + case 1: + $order->discount_amount = $this->coupon->value; + break; + case 2: + $order->discount_amount = $order->total_amount * ($this->coupon->value / 100); + break; + } + if ($this->coupon->limit_use !== NULL) { + $this->coupon->limit_use = $this->coupon->limit_use - 1; + if (!$this->coupon->save()) { + return false; + } + } + return true; + } +} diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 6bacdca8..55acd56d 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -33,4 +33,9 @@ class OrderService DB::commit(); return true; } + + public function create() + { + + } } diff --git a/database/update.sql b/database/update.sql index e69de29b..90884a4c 100644 --- a/database/update.sql +++ b/database/update.sql @@ -0,0 +1,5 @@ +ALTER TABLE `v2_order` +CHANGE `surplus_order_ids` `surplus_order_ids` varchar(255) NULL COMMENT '折抵订单' AFTER `balance_amount`; + +ALTER TABLE `v2_order` +CHANGE `status` `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0待支付1开通中2已取消3已完成4已折抵' AFTER `surplus_order_ids`;