update refund process

This commit is contained in:
Tokumeikoi 2020-04-09 13:35:27 +08:00
parent 2523253637
commit 4301d7e4ab
5 changed files with 125 additions and 72 deletions

View File

@ -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)

View File

@ -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;

View File

@ -0,0 +1,48 @@
<?php
namespace App\Services;
use App\Models\Coupon;
use App\Models\Order;
use Illuminate\Support\Facades\DB;
class CouponService
{
public $order;
public function __construct($code)
{
$this->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;
}
}

View File

@ -33,4 +33,9 @@ class OrderService
DB::commit();
return true;
}
public function create()
{
}
}

View File

@ -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`;