feature: shadowsocks and more

This commit is contained in:
Tokumeikoi
2020-10-04 14:21:09 +08:00
parent ac48f90678
commit ba2e0a6b66
23 changed files with 657 additions and 150 deletions

View File

@ -24,6 +24,49 @@ class OrderService
$this->order = $order;
}
public function open()
{
$order = $this->order;
$user = User::find($order->user_id);
$plan = Plan::find($order->plan_id);
if ($order->refund_amount) {
$user->balance = $user->balance + $order->refund_amount;
}
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, '开通失败');
}
}
switch ((string)$order->cycle) {
case 'onetime_price':
$this->buyByOneTime($user, $plan);
break;
case 'reset_price':
$this->buyByResetTraffic($user);
break;
default:
$this->buyByCycle($order, $user, $plan);
}
if (!$user->save()) {
DB::rollBack();
abort(500, '开通失败');
}
$order->status = 3;
if (!$order->save()) {
DB::rollBack();
abort(500, '开通失败');
}
DB::commit();
}
public function cancel():bool
{
$order = $this->order;
@ -165,4 +208,61 @@ class OrderService
$order->callback_no = $callbackNo;
return $order->save();
}
private function buyByResetTraffic(User $user)
{
$user->u = 0;
$user->d = 0;
}
private function buyByCycle(Order $order, User $user, Plan $plan)
{
// change plan process
if ((int)$order->type === 3) {
$user->expired_at = time();
}
$user->transfer_enable = $plan->transfer_enable * 1073741824;
// 续费重置&类型=续费
if ((int)config('v2board.renew_reset_traffic_enable', 1) && $order->type === 2) $this->buyByResetTraffic($user);
// 购买前用户过期为NULL一次性
if ($user->expired_at === NULL) $this->buyByResetTraffic($user);
// 新购
if ($order->type === 1) $this->buyByResetTraffic($user);
$user->plan_id = $plan->id;
$user->group_id = $plan->group_id;
$user->expired_at = $this->getTime($order->cycle, $user->expired_at);
}
private function buyByOneTime(User $user, Plan $plan)
{
$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;
}
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);
}
}
}