2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2023-12-04 07:40:49 -05:00
|
|
|
use App\Exceptions\ApiException;
|
2023-11-17 01:44:01 -05:00
|
|
|
use App\Jobs\OrderHandleJob;
|
|
|
|
use App\Models\Order;
|
|
|
|
use App\Models\Plan;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class OrderService
|
|
|
|
{
|
2025-01-08 00:52:57 -05:00
|
|
|
const STR_TO_TIME = [
|
|
|
|
Plan::PERIOD_MONTHLY => 1,
|
|
|
|
Plan::PERIOD_QUARTERLY => 3,
|
|
|
|
Plan::PERIOD_HALF_YEARLY => 6,
|
|
|
|
Plan::PERIOD_YEARLY => 12,
|
|
|
|
Plan::PERIOD_TWO_YEARLY => 24,
|
|
|
|
Plan::PERIOD_THREE_YEARLY => 36
|
2023-11-17 01:44:01 -05:00
|
|
|
];
|
|
|
|
public $order;
|
|
|
|
public $user;
|
|
|
|
|
|
|
|
public function __construct(Order $order)
|
|
|
|
{
|
|
|
|
$this->order = $order;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function open()
|
|
|
|
{
|
|
|
|
$order = $this->order;
|
|
|
|
$this->user = User::find($order->user_id);
|
|
|
|
$plan = Plan::find($order->plan_id);
|
|
|
|
|
|
|
|
if ($order->refund_amount) {
|
|
|
|
$this->user->balance = $this->user->balance + $order->refund_amount;
|
|
|
|
}
|
2025-01-08 00:52:57 -05:00
|
|
|
try {
|
2023-12-06 06:00:26 -05:00
|
|
|
DB::beginTransaction();
|
|
|
|
if ($order->surplus_order_ids) {
|
2023-11-17 01:44:01 -05:00
|
|
|
Order::whereIn('id', $order->surplus_order_ids)->update([
|
2024-04-09 12:51:03 -04:00
|
|
|
'status' => Order::STATUS_DISCOUNTED
|
2023-11-17 01:44:01 -05:00
|
|
|
]);
|
|
|
|
}
|
2025-01-08 00:52:57 -05:00
|
|
|
switch ((string) $order->period) {
|
2025-01-12 21:07:11 -05:00
|
|
|
case Plan::PERIOD_ONETIME:
|
2023-12-06 06:00:26 -05:00
|
|
|
$this->buyByOneTime($plan);
|
|
|
|
break;
|
2025-01-12 21:07:11 -05:00
|
|
|
case Plan::PERIOD_RESET_TRAFFIC:
|
2023-12-06 06:00:26 -05:00
|
|
|
$this->buyByResetTraffic();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->buyByPeriod($order, $plan);
|
|
|
|
}
|
2023-11-17 01:44:01 -05:00
|
|
|
|
2025-01-08 00:52:57 -05:00
|
|
|
switch ((int) $order->type) {
|
2025-01-06 12:20:11 -05:00
|
|
|
case Order::STATUS_PROCESSING:
|
2023-12-06 06:00:26 -05:00
|
|
|
$this->openEvent(admin_setting('new_order_event_id', 0));
|
|
|
|
break;
|
2025-01-06 12:20:11 -05:00
|
|
|
case Order::TYPE_RENEWAL:
|
2023-12-06 06:00:26 -05:00
|
|
|
$this->openEvent(admin_setting('renew_order_event_id', 0));
|
|
|
|
break;
|
2025-01-06 12:20:11 -05:00
|
|
|
case Order::TYPE_UPGRADE:
|
2023-12-06 06:00:26 -05:00
|
|
|
$this->openEvent(admin_setting('change_order_event_id', 0));
|
|
|
|
break;
|
|
|
|
}
|
2023-11-17 01:44:01 -05:00
|
|
|
|
2023-12-06 06:00:26 -05:00
|
|
|
$this->setSpeedLimit($plan->speed_limit);
|
2025-01-12 08:10:52 -05:00
|
|
|
$this->setDeviceLimit($plan->device_limit);
|
2023-11-17 01:44:01 -05:00
|
|
|
|
2023-12-06 06:00:26 -05:00
|
|
|
if (!$this->user->save()) {
|
|
|
|
throw new \Exception('用户信息保存失败');
|
|
|
|
}
|
2024-04-09 12:51:03 -04:00
|
|
|
$order->status = Order::STATUS_COMPLETED;
|
2023-12-06 06:00:26 -05:00
|
|
|
if (!$order->save()) {
|
|
|
|
throw new \Exception('订单信息保存失败');
|
|
|
|
}
|
|
|
|
DB::commit();
|
2025-01-08 00:52:57 -05:00
|
|
|
} catch (\Exception $e) {
|
2023-11-17 01:44:01 -05:00
|
|
|
DB::rollBack();
|
2023-12-06 06:00:26 -05:00
|
|
|
\Log::error($e);
|
2023-12-06 15:01:32 -05:00
|
|
|
throw new ApiException('开通失败');
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function setOrderType(User $user)
|
|
|
|
{
|
|
|
|
$order = $this->order;
|
2025-01-12 21:07:11 -05:00
|
|
|
if ($order->period === Plan::PERIOD_RESET_TRAFFIC) {
|
2024-04-09 12:51:03 -04:00
|
|
|
$order->type = Order::TYPE_RESET_TRAFFIC;
|
2023-11-17 01:44:01 -05:00
|
|
|
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id && ($user->expired_at > time() || $user->expired_at === NULL)) {
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!(int) admin_setting('plan_change_enable', 1))
|
|
|
|
throw new ApiException('目前不允许更改订阅,请联系客服或提交工单操作');
|
2024-04-09 12:51:03 -04:00
|
|
|
$order->type = Order::TYPE_UPGRADE;
|
2025-01-08 00:52:57 -05:00
|
|
|
if ((int) admin_setting('surplus_enable', 1))
|
|
|
|
$this->getSurplusValue($user, $order);
|
2023-11-17 01:44:01 -05: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;
|
|
|
|
}
|
|
|
|
} else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) { // 用户订阅未过期且购买订阅与当前订阅相同 === 续费
|
2024-04-09 12:51:03 -04:00
|
|
|
$order->type = Order::TYPE_RENEWAL;
|
2023-11-17 01:44:01 -05:00
|
|
|
} else { // 新购
|
2024-04-09 12:51:03 -04:00
|
|
|
$order->type = Order::TYPE_NEW_PURCHASE;
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-01-08 00:52:57 -05:00
|
|
|
public function setInvite(User $user): void
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
|
|
|
$order = $this->order;
|
2025-01-08 00:52:57 -05:00
|
|
|
if ($user->invite_user_id && ($order->total_amount <= 0))
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$order->invite_user_id = $user->invite_user_id;
|
|
|
|
$inviter = User::find($user->invite_user_id);
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$inviter)
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$isCommission = false;
|
2025-01-08 00:52:57 -05:00
|
|
|
switch ((int) $inviter->commission_type) {
|
2023-11-17 01:44:01 -05:00
|
|
|
case 0:
|
2025-01-08 00:52:57 -05:00
|
|
|
$commissionFirstTime = (int) admin_setting('commission_first_time_enable', 1);
|
2023-11-17 01:44:01 -05:00
|
|
|
$isCommission = (!$commissionFirstTime || ($commissionFirstTime && !$this->haveValidOrder($user)));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
$isCommission = true;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
$isCommission = !$this->haveValidOrder($user);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$isCommission)
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
if ($inviter && $inviter->commission_rate) {
|
|
|
|
$order->commission_balance = $order->total_amount * ($inviter->commission_rate / 100);
|
|
|
|
} else {
|
|
|
|
$order->commission_balance = $order->total_amount * (admin_setting('invite_commission', 10) / 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function haveValidOrder(User $user)
|
|
|
|
{
|
|
|
|
return Order::where('user_id', $user->id)
|
|
|
|
->whereNotIn('status', [0, 2])
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSurplusValue(User $user, Order $order)
|
|
|
|
{
|
|
|
|
if ($user->expired_at === NULL) {
|
|
|
|
$this->getSurplusValueByOneTime($user, $order);
|
|
|
|
} else {
|
|
|
|
$this->getSurplusValueByPeriod($user, $order);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function getSurplusValueByOneTime(User $user, Order $order)
|
|
|
|
{
|
|
|
|
$lastOneTimeOrder = Order::where('user_id', $user->id)
|
2025-01-12 21:07:11 -05:00
|
|
|
->where('period', Plan::PERIOD_ONETIME)
|
2024-04-27 05:06:57 -04:00
|
|
|
->where('status', Order::STATUS_COMPLETED)
|
2023-11-17 01:44:01 -05:00
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->first();
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$lastOneTimeOrder)
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$nowUserTraffic = $user->transfer_enable / 1073741824;
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$nowUserTraffic)
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$paidTotalAmount = ($lastOneTimeOrder->total_amount + $lastOneTimeOrder->balance_amount);
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$paidTotalAmount)
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$trafficUnitPrice = $paidTotalAmount / $nowUserTraffic;
|
|
|
|
$notUsedTraffic = $nowUserTraffic - (($user->u + $user->d) / 1073741824);
|
|
|
|
$result = $trafficUnitPrice * $notUsedTraffic;
|
2025-01-12 21:07:11 -05:00
|
|
|
$orderModel = Order::where('user_id', $user->id)->where('period', '!=', Plan::PERIOD_RESET_TRAFFIC)->where('status', Order::STATUS_COMPLETED);
|
2023-11-17 01:44:01 -05:00
|
|
|
$order->surplus_amount = $result > 0 ? $result : 0;
|
|
|
|
$order->surplus_order_ids = array_column($orderModel->get()->toArray(), 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSurplusValueByPeriod(User $user, Order $order)
|
|
|
|
{
|
|
|
|
$orders = Order::where('user_id', $user->id)
|
2025-01-12 21:07:11 -05:00
|
|
|
->whereNotIn('period', [Plan::PERIOD_RESET_TRAFFIC, Plan::PERIOD_ONETIME])
|
2024-04-27 05:06:57 -04:00
|
|
|
->where('status', Order::STATUS_COMPLETED)
|
2023-11-17 01:44:01 -05:00
|
|
|
->get()
|
|
|
|
->toArray();
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$orders)
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$orderAmountSum = 0;
|
|
|
|
$orderMonthSum = 0;
|
|
|
|
$lastValidateAt = 0;
|
|
|
|
foreach ($orders as $item) {
|
2025-01-08 00:52:57 -05:00
|
|
|
$period = self::STR_TO_TIME[PlanService::getPeriodKey($item['period'])];
|
|
|
|
if (strtotime("+{$period} month", $item['created_at']) < time())
|
|
|
|
continue;
|
2023-11-17 01:44:01 -05:00
|
|
|
$lastValidateAt = $item['created_at'];
|
|
|
|
$orderMonthSum = $period + $orderMonthSum;
|
|
|
|
$orderAmountSum = $orderAmountSum + ($item['total_amount'] + $item['balance_amount'] + $item['surplus_amount'] - $item['refund_amount']);
|
|
|
|
}
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$lastValidateAt)
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$expiredAtByOrder = strtotime("+{$orderMonthSum} month", $lastValidateAt);
|
2025-01-08 00:52:57 -05:00
|
|
|
if ($expiredAtByOrder < time())
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$orderSurplusSecond = $expiredAtByOrder - time();
|
|
|
|
$orderRangeSecond = $expiredAtByOrder - $lastValidateAt;
|
|
|
|
$avgPrice = $orderAmountSum / $orderRangeSecond;
|
|
|
|
$orderSurplusAmount = $avgPrice * $orderSurplusSecond;
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$orderSurplusSecond || !$orderSurplusAmount)
|
|
|
|
return;
|
2023-11-17 01:44:01 -05:00
|
|
|
$order->surplus_amount = $orderSurplusAmount > 0 ? $orderSurplusAmount : 0;
|
|
|
|
$order->surplus_order_ids = array_column($orders, 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paid(string $callbackNo)
|
|
|
|
{
|
|
|
|
$order = $this->order;
|
2025-01-08 00:52:57 -05:00
|
|
|
if ($order->status !== Order::STATUS_PENDING)
|
|
|
|
return true;
|
2024-04-09 12:51:03 -04:00
|
|
|
$order->status = Order::STATUS_PROCESSING;
|
2023-11-17 01:44:01 -05:00
|
|
|
$order->paid_at = time();
|
|
|
|
$order->callback_no = $callbackNo;
|
2025-01-08 00:52:57 -05:00
|
|
|
if (!$order->save())
|
|
|
|
return false;
|
2023-11-17 01:44:01 -05:00
|
|
|
try {
|
|
|
|
OrderHandleJob::dispatchSync($order->trade_no);
|
|
|
|
} catch (\Exception $e) {
|
2025-01-06 12:20:11 -05:00
|
|
|
\Log::error($e);
|
2023-11-17 01:44:01 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2025-01-08 00:52:57 -05:00
|
|
|
public function cancel(): bool
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
|
|
|
$order = $this->order;
|
2023-12-06 06:00:26 -05:00
|
|
|
try {
|
|
|
|
DB::beginTransaction();
|
2024-04-09 12:51:03 -04:00
|
|
|
$order->status = Order::STATUS_CANCELLED;
|
2023-12-06 06:00:26 -05:00
|
|
|
if (!$order->save()) {
|
|
|
|
throw new \Exception('Failed to save order status.');
|
|
|
|
}
|
|
|
|
if ($order->balance_amount) {
|
|
|
|
$userService = new UserService();
|
|
|
|
if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
|
|
|
|
throw new \Exception('Failed to add balance.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return true;
|
2025-01-08 00:52:57 -05:00
|
|
|
} catch (\Exception $e) {
|
2023-11-17 01:44:01 -05:00
|
|
|
DB::rollBack();
|
2023-12-06 06:00:26 -05:00
|
|
|
\Log::error($e);
|
2023-11-17 01:44:01 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setSpeedLimit($speedLimit)
|
|
|
|
{
|
|
|
|
$this->user->speed_limit = $speedLimit;
|
|
|
|
}
|
2025-01-12 21:07:11 -05:00
|
|
|
|
2025-01-12 08:10:52 -05:00
|
|
|
private function setDeviceLimit($deviceLimit)
|
|
|
|
{
|
|
|
|
$this->user->device_limit = $deviceLimit;
|
|
|
|
}
|
2023-11-17 01:44:01 -05:00
|
|
|
|
|
|
|
private function buyByResetTraffic()
|
|
|
|
{
|
|
|
|
$this->user->u = 0;
|
|
|
|
$this->user->d = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buyByPeriod(Order $order, Plan $plan)
|
|
|
|
{
|
|
|
|
// change plan process
|
2025-01-08 00:52:57 -05:00
|
|
|
if ((int) $order->type === Order::TYPE_UPGRADE) {
|
2023-11-17 01:44:01 -05:00
|
|
|
$this->user->expired_at = time();
|
|
|
|
}
|
|
|
|
$this->user->transfer_enable = $plan->transfer_enable * 1073741824;
|
|
|
|
// 从一次性转换到循环
|
2025-01-08 00:52:57 -05:00
|
|
|
if ($this->user->expired_at === NULL)
|
|
|
|
$this->buyByResetTraffic();
|
2023-11-17 01:44:01 -05:00
|
|
|
// 新购
|
2025-01-08 00:52:57 -05:00
|
|
|
if ($order->type === Order::TYPE_NEW_PURCHASE)
|
|
|
|
$this->buyByResetTraffic();
|
2023-11-17 01:44:01 -05:00
|
|
|
$this->user->plan_id = $plan->id;
|
|
|
|
$this->user->group_id = $plan->group_id;
|
|
|
|
$this->user->expired_at = $this->getTime($order->period, $this->user->expired_at);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buyByOneTime(Plan $plan)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTime($str, $timestamp)
|
|
|
|
{
|
|
|
|
if ($timestamp < time()) {
|
|
|
|
$timestamp = time();
|
|
|
|
}
|
2025-01-08 00:52:57 -05:00
|
|
|
$str = PlanService::getPeriodKey($str);
|
2023-11-17 01:44:01 -05:00
|
|
|
switch ($str) {
|
2025-01-08 00:52:57 -05:00
|
|
|
case Plan::PERIOD_MONTHLY:
|
2023-11-17 01:44:01 -05:00
|
|
|
return strtotime('+1 month', $timestamp);
|
2025-01-08 00:52:57 -05:00
|
|
|
case Plan::PERIOD_QUARTERLY:
|
2023-11-17 01:44:01 -05:00
|
|
|
return strtotime('+3 month', $timestamp);
|
2025-01-08 00:52:57 -05:00
|
|
|
case Plan::PERIOD_HALF_YEARLY:
|
2023-11-17 01:44:01 -05:00
|
|
|
return strtotime('+6 month', $timestamp);
|
2025-01-08 00:52:57 -05:00
|
|
|
case Plan::PERIOD_YEARLY:
|
2023-11-17 01:44:01 -05:00
|
|
|
return strtotime('+12 month', $timestamp);
|
2025-01-08 00:52:57 -05:00
|
|
|
case Plan::PERIOD_TWO_YEARLY:
|
2023-11-17 01:44:01 -05:00
|
|
|
return strtotime('+24 month', $timestamp);
|
2025-01-08 00:52:57 -05:00
|
|
|
case Plan::PERIOD_THREE_YEARLY:
|
2023-11-17 01:44:01 -05:00
|
|
|
return strtotime('+36 month', $timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function openEvent($eventId)
|
|
|
|
{
|
|
|
|
switch ((int) $eventId) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
$this->buyByResetTraffic();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|