v2board/app/Services/OrderService.php

312 lines
10 KiB
PHP
Raw Normal View History

2020-03-17 19:00:33 +08:00
<?php
namespace App\Services;
2021-09-04 15:31:25 +08:00
use App\Jobs\OrderHandleJob;
2020-03-17 19:00:33 +08:00
use App\Models\Order;
2020-04-22 16:57:00 +08:00
use App\Models\Plan;
use App\Models\User;
2020-03-17 20:00:46 +08:00
use Illuminate\Support\Facades\DB;
2020-03-17 19:00:33 +08:00
class OrderService
{
2020-10-11 01:22:43 +08:00
CONST STR_TO_TIME = [
2020-09-16 20:31:30 +08:00
'month_price' => 1,
'quarter_price' => 3,
'half_year_price' => 6,
'year_price' => 12,
'two_year_price' => 24,
'three_year_price' => 36
];
2020-03-17 19:00:33 +08:00
public $order;
2021-07-01 21:04:22 +08:00
public $user;
2020-03-17 19:00:33 +08:00
public function __construct(Order $order)
{
$this->order = $order;
}
2020-10-04 14:21:09 +08:00
public function open()
{
$order = $this->order;
2021-07-01 21:04:22 +08:00
$this->user = User::find($order->user_id);
2020-10-04 14:21:09 +08:00
$plan = Plan::find($order->plan_id);
if ($order->refund_amount) {
2021-07-01 21:04:22 +08:00
$this->user->balance = $this->user->balance + $order->refund_amount;
2020-10-04 14:21:09 +08:00
}
DB::beginTransaction();
if ($order->surplus_order_ids) {
try {
2021-08-06 00:43:01 +08:00
Order::whereIn('id', $order->surplus_order_ids)->update([
2020-10-04 14:21:09 +08:00
'status' => 4
]);
} catch (\Exception $e) {
DB::rollback();
abort(500, '开通失败');
}
}
switch ((string)$order->cycle) {
case 'onetime_price':
2021-07-01 21:04:22 +08:00
$this->buyByOneTime($plan);
2020-10-04 14:21:09 +08:00
break;
case 'reset_price':
2021-07-01 21:04:22 +08:00
$this->buyByResetTraffic();
2020-10-04 14:21:09 +08:00
break;
default:
2021-07-01 21:04:22 +08:00
$this->buyByCycle($order, $plan);
2020-10-04 14:21:09 +08:00
}
2020-10-11 00:28:09 +08:00
2021-07-01 21:04:22 +08:00
switch ((int)$order->type) {
case 1:
$this->openEvent(config('v2board.new_order_event_id', 0));
break;
case 2:
$this->openEvent(config('v2board.renew_order_event_id', 0));
break;
case 3:
$this->openEvent(config('v2board.change_order_event_id', 0));
break;
}
2020-10-11 00:28:09 +08:00
2021-07-01 21:04:22 +08:00
if (!$this->user->save()) {
2020-10-04 14:21:09 +08:00
DB::rollBack();
abort(500, '开通失败');
}
$order->status = 3;
if (!$order->save()) {
DB::rollBack();
abort(500, '开通失败');
}
DB::commit();
}
2020-04-09 13:35:27 +08:00
2020-04-22 16:57:00 +08:00
public function setOrderType(User $user)
{
$order = $this->order;
2020-04-25 19:44:47 +08:00
if ($order->cycle === 'reset_price') {
$order->type = 4;
2020-09-10 13:01:55 +08:00
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id && ($user->expired_at > time() || $user->expired_at === NULL)) {
2020-04-25 19:44:47 +08:00
if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
2020-04-22 16:57:00 +08:00
$order->type = 3;
if ((int)config('v2board.surplus_enable', 1)) $this->getSurplusValue($user, $order);
2020-04-22 16:57:00 +08:00
if ($order->surplus_amount >= $order->total_amount) {
$order->refund_amount = $order->surplus_amount - $order->total_amount;
$order->total_amount = 0;
} else {
$order->total_amount = $order->total_amount - $order->surplus_amount;
}
2020-07-13 17:09:13 +08:00
} else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) { // 用户订阅未过期且购买订阅与当前订阅相同 === 续费
2020-04-22 16:57:00 +08:00
$order->type = 2;
2020-07-13 17:09:13 +08:00
} else { // 新购
2020-04-22 16:57:00 +08:00
$order->type = 1;
}
}
public function setVipDiscount(User $user)
{
$order = $this->order;
if ($user->discount) {
$order->discount_amount = $order->discount_amount + ($order->total_amount * ($user->discount / 100));
}
$order->total_amount = $order->total_amount - $order->discount_amount;
}
public function setInvite(User $user):void
2020-04-22 16:57:00 +08:00
{
$order = $this->order;
if ($user->invite_user_id && $order->total_amount > 0) {
$order->invite_user_id = $user->invite_user_id;
$isCommission = false;
switch ((int)$user->commission_type) {
case 0:
$commissionFirstTime = (int)config('v2board.commission_first_time_enable', 1);
$isCommission = (!$commissionFirstTime || ($commissionFirstTime && !$this->haveValidOrder($user)));
break;
case 1:
$isCommission = true;
break;
case 2:
$isCommission = !$this->haveValidOrder($user);
break;
}
if ($isCommission) {
2020-04-22 16:57:00 +08:00
$inviter = User::find($user->invite_user_id);
if ($inviter && $inviter->commission_rate) {
$order->commission_balance = $order->total_amount * ($inviter->commission_rate / 100);
} else {
$order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
}
}
}
}
2020-11-17 16:34:10 +08:00
private function haveValidOrder(User $user)
{
return Order::where('user_id', $user->id)
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2])
2020-11-17 16:34:10 +08:00
->first();
}
2020-04-22 16:57:00 +08:00
private function getSurplusValue(User $user, Order $order)
{
if ($user->expired_at === NULL) {
$this->getSurplusValueByOneTime($user, $order);
} else {
$this->getSurplusValueByCycle($user, $order);
}
}
private function getSurplusValueByOneTime(User $user, Order $order)
{
$plan = Plan::find($user->plan_id);
$trafficUnitPrice = $plan->onetime_price / $plan->transfer_enable;
if ($user->discount && $trafficUnitPrice) {
$trafficUnitPrice = $trafficUnitPrice - ($trafficUnitPrice * $user->discount / 100);
}
2020-09-11 01:02:31 +08:00
$notUsedTraffic = $plan->transfer_enable - (($user->u + $user->d) / 1073741824);
$result = $trafficUnitPrice * $notUsedTraffic;
2020-04-25 19:44:47 +08:00
$orderModel = Order::where('user_id', $user->id)->where('cycle', '!=', 'reset_price')->where('status', 3);
2020-04-22 16:57:00 +08:00
$order->surplus_amount = $result > 0 ? $result : 0;
2021-08-06 00:43:01 +08:00
$order->surplus_order_ids = array_column($orderModel->get()->toArray(), 'id');
2020-04-22 16:57:00 +08:00
}
2020-09-16 20:31:30 +08:00
private function orderIsUsed(Order $order):bool
{
2020-10-11 01:22:43 +08:00
$month = self::STR_TO_TIME[$order->cycle];
2021-08-01 22:56:11 +08:00
$orderExpireDay = strtotime('+' . $month . ' month', $order->created_at);
2020-09-16 20:31:30 +08:00
if ($orderExpireDay < time()) return true;
return false;
}
2020-04-22 16:57:00 +08:00
private function getSurplusValueByCycle(User $user, Order $order)
{
2020-04-30 16:08:26 +08:00
$orderModel = Order::where('user_id', $user->id)
->where('cycle', '!=', 'reset_price')
->where('status', 3);
2020-09-16 20:31:30 +08:00
$orders = $orderModel->get();
2020-06-11 20:47:02 +08:00
$orderSurplusMonth = 0;
$orderSurplusAmount = 0;
$userSurplusMonth = ($user->expired_at - time()) / 2678400;
2020-09-16 20:31:30 +08:00
foreach ($orders as $k => $item) {
2020-06-02 03:11:31 +08:00
// 兼容历史余留问题
if ($item->cycle === 'onetime_price') continue;
2020-09-16 20:44:50 +08:00
if ($this->orderIsUsed($item)) continue;
2020-10-11 01:22:43 +08:00
$orderSurplusMonth = $orderSurplusMonth + self::STR_TO_TIME[$item->cycle];
2021-07-18 21:27:23 +08:00
$orderSurplusAmount = $orderSurplusAmount + ($item['total_amount'] + $item['balance_amount'] + $item['surplus_amount'] - $item['refund_amount']);
2020-06-11 20:47:02 +08:00
}
2020-06-12 00:40:37 +08:00
if (!$orderSurplusMonth || !$orderSurplusAmount) return;
2020-06-11 20:47:02 +08:00
$monthUnitPrice = $orderSurplusAmount / $orderSurplusMonth;
// 如果用户过期月大于订单过期月
if ($userSurplusMonth > $orderSurplusMonth) {
$orderSurplusAmount = $orderSurplusMonth * $monthUnitPrice;
} else {
$orderSurplusAmount = $userSurplusMonth * $monthUnitPrice;
2020-04-22 16:57:00 +08:00
}
2020-06-11 20:47:02 +08:00
if (!$orderSurplusAmount) {
2020-05-02 14:29:04 +08:00
return;
}
2020-06-11 20:47:02 +08:00
$order->surplus_amount = $orderSurplusAmount > 0 ? $orderSurplusAmount : 0;
2021-08-06 00:43:01 +08:00
$order->surplus_order_ids = array_column($orders->toArray(), 'id');
2020-04-22 16:57:00 +08:00
}
2020-06-04 18:00:33 +08:00
2021-08-28 14:11:59 +08:00
public function paid(string $callbackNo)
2020-06-04 18:00:33 +08:00
{
$order = $this->order;
2021-08-28 14:11:59 +08:00
if ($order->status !== 0) return true;
2020-06-04 18:00:33 +08:00
$order->status = 1;
2021-07-31 01:21:49 +08:00
$order->paid_at = time();
2020-06-04 18:00:33 +08:00
$order->callback_no = $callbackNo;
2021-09-04 15:31:25 +08:00
if (!$order->save()) return false;
OrderHandleJob::dispatch($order->trade_no);
return true;
2020-06-04 18:00:33 +08:00
}
2020-10-04 14:21:09 +08:00
2021-08-28 14:11:59 +08:00
public function cancel():bool
{
$order = $this->order;
DB::beginTransaction();
$order->status = 2;
if (!$order->save()) {
DB::rollBack();
return false;
}
if ($order->balance_amount) {
$userService = new UserService();
if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
DB::rollBack();
return false;
}
}
DB::commit();
return true;
}
2020-10-04 14:21:09 +08:00
2021-07-01 21:04:22 +08:00
private function buyByResetTraffic()
2020-10-04 14:21:09 +08:00
{
2021-07-01 21:04:22 +08:00
$this->user->u = 0;
$this->user->d = 0;
2020-10-04 14:21:09 +08:00
}
2021-07-01 21:04:22 +08:00
private function buyByCycle(Order $order, Plan $plan)
2020-10-04 14:21:09 +08:00
{
// change plan process
if ((int)$order->type === 3) {
2021-07-01 21:04:22 +08:00
$this->user->expired_at = time();
2020-10-04 14:21:09 +08:00
}
2021-07-01 21:04:22 +08:00
$this->user->transfer_enable = $plan->transfer_enable * 1073741824;
2020-10-11 00:28:09 +08:00
// 从一次性转换到循环
2021-07-01 21:04:22 +08:00
if ($this->user->expired_at === NULL) $this->buyByResetTraffic();
2020-10-04 14:21:09 +08:00
// 新购
2021-07-01 21:04:22 +08:00
if ($order->type === 1) $this->buyByResetTraffic();
$this->user->plan_id = $plan->id;
$this->user->group_id = $plan->group_id;
$this->user->expired_at = $this->getTime($order->cycle, $this->user->expired_at);
2020-10-04 14:21:09 +08:00
}
2021-07-01 21:04:22 +08:00
private function buyByOneTime(Plan $plan)
2020-10-04 14:21:09 +08:00
{
2021-07-01 21:04:22 +08:00
$this->buyByResetTraffic();
$this->user->transfer_enable = $plan->transfer_enable * 1073741824;
$this->user->plan_id = $plan->id;
$this->user->group_id = $plan->group_id;
$this->user->expired_at = NULL;
2020-10-04 14:21:09 +08:00
}
private function getTime($str, $timestamp)
{
if ($timestamp < time()) {
$timestamp = time();
}
switch ($str) {
case 'month_price':
return strtotime('+1 month', $timestamp);
case 'quarter_price':
return strtotime('+3 month', $timestamp);
case 'half_year_price':
return strtotime('+6 month', $timestamp);
case 'year_price':
return strtotime('+12 month', $timestamp);
case 'two_year_price':
return strtotime('+24 month', $timestamp);
case 'three_year_price':
return strtotime('+36 month', $timestamp);
}
}
2021-07-01 21:04:22 +08:00
private function openEvent($eventId)
{
switch ((int) $eventId) {
case 0:
break;
case 1:
$this->buyByResetTraffic();
break;
}
}
2020-03-17 19:00:33 +08:00
}