mirror of
https://github.com/v2board/v2board.git
synced 2024-11-10 09:39:10 +08:00
update refund process
This commit is contained in:
parent
2523253637
commit
4301d7e4ab
@ -9,6 +9,7 @@ use App\Models\User;
|
|||||||
use App\Models\Plan;
|
use App\Models\Plan;
|
||||||
use App\Utils\Helper;
|
use App\Utils\Helper;
|
||||||
use App\Models\Coupon;
|
use App\Models\Coupon;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class CheckOrder extends Command
|
class CheckOrder extends Command
|
||||||
{
|
{
|
||||||
@ -65,10 +66,37 @@ class CheckOrder extends Command
|
|||||||
{
|
{
|
||||||
$user = User::find($order->user_id);
|
$user = User::find($order->user_id);
|
||||||
$plan = Plan::find($order->plan_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)
|
private function buyByCycle(Order $order, User $user, Plan $plan)
|
||||||
@ -77,9 +105,6 @@ class CheckOrder extends Command
|
|||||||
if ((int)$order->type === 3) {
|
if ((int)$order->type === 3) {
|
||||||
$user->expired_at = time();
|
$user->expired_at = time();
|
||||||
}
|
}
|
||||||
if ($order->refund_amount) {
|
|
||||||
$user->balance = $user->balance + $order->refund_amount;
|
|
||||||
}
|
|
||||||
$user->transfer_enable = $plan->transfer_enable * 1073741824;
|
$user->transfer_enable = $plan->transfer_enable * 1073741824;
|
||||||
if ((int)config('v2board.renew_reset_traffic_enable', 1)) {
|
if ((int)config('v2board.renew_reset_traffic_enable', 1)) {
|
||||||
$user->u = 0;
|
$user->u = 0;
|
||||||
@ -88,27 +113,16 @@ class CheckOrder extends Command
|
|||||||
$user->plan_id = $plan->id;
|
$user->plan_id = $plan->id;
|
||||||
$user->group_id = $plan->group_id;
|
$user->group_id = $plan->group_id;
|
||||||
$user->expired_at = $this->getTime($order->cycle, $user->expired_at);
|
$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)
|
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->transfer_enable = $plan->transfer_enable * 1073741824;
|
||||||
$user->u = 0;
|
$user->u = 0;
|
||||||
$user->d = 0;
|
$user->d = 0;
|
||||||
$user->plan_id = $plan->id;
|
$user->plan_id = $plan->id;
|
||||||
$user->group_id = $plan->group_id;
|
$user->group_id = $plan->group_id;
|
||||||
$user->expired_at = NULL;
|
$user->expired_at = NULL;
|
||||||
if ($user->save()) {
|
|
||||||
$order->status = 3;
|
|
||||||
$order->save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTime($str, $timestamp)
|
private function getTime($str, $timestamp)
|
||||||
|
@ -4,6 +4,7 @@ namespace App\Http\Controllers\User;
|
|||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\User\OrderSave;
|
use App\Http\Requests\User\OrderSave;
|
||||||
|
use App\Services\CouponService;
|
||||||
use App\Services\OrderService;
|
use App\Services\OrderService;
|
||||||
use App\Services\UserService;
|
use App\Services\UserService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@ -74,14 +75,15 @@ class OrderController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
// surplus value
|
// surplus value
|
||||||
private function getSurplusValue(User $user)
|
private function getSurplusValue(User $user, Order $order)
|
||||||
{
|
{
|
||||||
$plan = Plan::find($user->plan_id);
|
// $plan = Plan::find($user->plan_id);
|
||||||
if ($user->expired_at === NULL) {
|
// if ($user->expired_at === NULL) {
|
||||||
return $this->getSurplusValueByOneTime($user, $plan);
|
// $this->getSurplusValueByOneTime($user, $plan);
|
||||||
} else {
|
// } else {
|
||||||
return $this->getSurplusValueByCycle($user, $plan);
|
// $this->getSurplusValueByCycle($user, $order);
|
||||||
}
|
// }
|
||||||
|
$this->getSurplusValueByCycle($user, $order);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getSurplusValueByOneTime(User $user, Plan $plan)
|
private function getSurplusValueByOneTime(User $user, Plan $plan)
|
||||||
@ -95,25 +97,29 @@ class OrderController extends Controller
|
|||||||
return $result > 0 ? $result : 0;
|
return $result > 0 ? $result : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getSurplusValueByCycle(User $user, Plan $plan)
|
private function getSurplusValueByCycle(User $user, Order $order)
|
||||||
{
|
{
|
||||||
$price = 0;
|
$strToMonth = [
|
||||||
if ($plan->month_price) {
|
'month_price' => 1,
|
||||||
$price = $plan->month_price / (31536000 / 12);
|
'quarter_price' => 3,
|
||||||
} else if ($plan->quarter_price) {
|
'half_year_price' => 6,
|
||||||
$price = $plan->quarter_price / (31536000 / 4);
|
'year_price' => 12
|
||||||
} else if ($plan->half_year_price) {
|
];
|
||||||
$price = $plan->half_year_price / (31536000 / 2);
|
$orderModel = Order::where('user_id', $user->id)->where('status', 3);
|
||||||
} else if ($plan->year_price) {
|
|
||||||
$price = $plan->year_price / 31536000;
|
$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
|
info('剩余月份' . $totalMonth);
|
||||||
if ($user->discount && $price) {
|
$unitPrice = $totalValue / $totalMonth;
|
||||||
$price = $price - ($price * $user->discount / 100);
|
info('单价' . $unitPrice);
|
||||||
}
|
$remainingMonth = ($user->expired_at - time()) / 2678400;
|
||||||
$remainingDay = $user->expired_at - time();
|
$result = $unitPrice * $remainingMonth;
|
||||||
$result = $remainingDay * $price;
|
$order->surplus_amount = $result > 0 ? $result : 0;
|
||||||
return $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)
|
public function save(OrderSave $request)
|
||||||
@ -141,21 +147,6 @@ 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 ($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();
|
DB::beginTransaction();
|
||||||
$order = new Order();
|
$order = new Order();
|
||||||
@ -165,21 +156,11 @@ class OrderController extends Controller
|
|||||||
$order->trade_no = Helper::guid();
|
$order->trade_no = Helper::guid();
|
||||||
$order->total_amount = $plan[$request->input('cycle')];
|
$order->total_amount = $plan[$request->input('cycle')];
|
||||||
// coupon start
|
// coupon start
|
||||||
if (isset($coupon)) {
|
if ($request->input('coupon_code')) {
|
||||||
switch ($coupon->type) {
|
$couponService = new CouponService($request->input('coupon_code'));
|
||||||
case 1:
|
if (!$couponService->use($order)) {
|
||||||
$order->discount_amount = $coupon->value;
|
DB::rollBack();
|
||||||
break;
|
abort(500, '优惠券使用失败');
|
||||||
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, '优惠券使用失败');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// coupon complete
|
// coupon complete
|
||||||
@ -193,7 +174,7 @@ class OrderController extends Controller
|
|||||||
if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) {
|
if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) {
|
||||||
if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单');
|
if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单');
|
||||||
$order->type = 3;
|
$order->type = 3;
|
||||||
$order->surplus_amount = $this->getSurplusValue($user);
|
$this->getSurplusValue($user, $order);
|
||||||
if ($order->surplus_amount >= $order->total_amount) {
|
if ($order->surplus_amount >= $order->total_amount) {
|
||||||
$order->refund_amount = $order->surplus_amount - $order->total_amount;
|
$order->refund_amount = $order->surplus_amount - $order->total_amount;
|
||||||
$order->total_amount = 0;
|
$order->total_amount = 0;
|
||||||
|
48
app/Services/CouponService.php
Normal file
48
app/Services/CouponService.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -33,4 +33,9 @@ class OrderService
|
|||||||
DB::commit();
|
DB::commit();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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`;
|
Loading…
Reference in New Issue
Block a user