OrderService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Order;
  4. use App\Models\Plan;
  5. use App\Models\User;
  6. use Illuminate\Support\Facades\DB;
  7. class OrderService
  8. {
  9. CONST STR_TO_TIME = [
  10. 'month_price' => 1,
  11. 'quarter_price' => 3,
  12. 'half_year_price' => 6,
  13. 'year_price' => 12,
  14. 'two_year_price' => 24,
  15. 'three_year_price' => 36
  16. ];
  17. public $order;
  18. public function __construct(Order $order)
  19. {
  20. $this->order = $order;
  21. }
  22. public function open()
  23. {
  24. $order = $this->order;
  25. $user = User::find($order->user_id);
  26. $plan = Plan::find($order->plan_id);
  27. if ($order->refund_amount) {
  28. $user->balance = $user->balance + $order->refund_amount;
  29. }
  30. DB::beginTransaction();
  31. if ($order->surplus_order_ids) {
  32. try {
  33. Order::whereIn('id', json_decode($order->surplus_order_ids))->update([
  34. 'status' => 4
  35. ]);
  36. } catch (\Exception $e) {
  37. DB::rollback();
  38. abort(500, '开通失败');
  39. }
  40. }
  41. switch ((string)$order->cycle) {
  42. case 'onetime_price':
  43. $this->buyByOneTime($user, $plan);
  44. break;
  45. case 'reset_price':
  46. $this->buyByResetTraffic($user);
  47. break;
  48. default:
  49. $this->buyByCycle($order, $user, $plan);
  50. }
  51. if ((int)config('v2board.renew_reset_traffic_enable', 0)) $this->buyByResetTraffic($user);
  52. if (!$user->save()) {
  53. DB::rollBack();
  54. abort(500, '开通失败');
  55. }
  56. $order->status = 3;
  57. if (!$order->save()) {
  58. DB::rollBack();
  59. abort(500, '开通失败');
  60. }
  61. DB::commit();
  62. }
  63. public function cancel():bool
  64. {
  65. $order = $this->order;
  66. DB::beginTransaction();
  67. $order->status = 2;
  68. if (!$order->save()) {
  69. DB::rollBack();
  70. return false;
  71. }
  72. if ($order->balance_amount) {
  73. $userService = new UserService();
  74. if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
  75. DB::rollBack();
  76. return false;
  77. }
  78. }
  79. DB::commit();
  80. return true;
  81. }
  82. public function setOrderType(User $user)
  83. {
  84. $order = $this->order;
  85. if ($order->cycle === 'reset_price') {
  86. $order->type = 4;
  87. } else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id && ($user->expired_at > time() || $user->expired_at === NULL)) {
  88. if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
  89. $order->type = 3;
  90. if ((int)config('v2board.surplus_enable', 1)) $this->getSurplusValue($user, $order);
  91. if ($order->surplus_amount >= $order->total_amount) {
  92. $order->refund_amount = $order->surplus_amount - $order->total_amount;
  93. $order->total_amount = 0;
  94. } else {
  95. $order->total_amount = $order->total_amount - $order->surplus_amount;
  96. }
  97. } else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) { // 用户订阅未过期且购买订阅与当前订阅相同 === 续费
  98. $order->type = 2;
  99. } else { // 新购
  100. $order->type = 1;
  101. }
  102. }
  103. public function setVipDiscount(User $user)
  104. {
  105. $order = $this->order;
  106. if ($user->discount) {
  107. $order->discount_amount = $order->discount_amount + ($order->total_amount * ($user->discount / 100));
  108. }
  109. $order->total_amount = $order->total_amount - $order->discount_amount;
  110. }
  111. public function setInvite(User $user)
  112. {
  113. $order = $this->order;
  114. if ($user->invite_user_id && $order->total_amount > 0) {
  115. $order->invite_user_id = $user->invite_user_id;
  116. $commissionFirstTime = (int)config('v2board.commission_first_time_enable', 1);
  117. if (!$commissionFirstTime || ($commissionFirstTime && !$this->haveValidOrder($user))) {
  118. $inviter = User::find($user->invite_user_id);
  119. if ($inviter && $inviter->commission_rate) {
  120. $order->commission_balance = $order->total_amount * ($inviter->commission_rate / 100);
  121. } else {
  122. $order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
  123. }
  124. }
  125. }
  126. }
  127. private function haveValidOrder(User $user)
  128. {
  129. return Order::where('user_id', $user->id)
  130. ->whereNotIn('status', [0, 2])
  131. ->first();
  132. }
  133. private function getSurplusValue(User $user, Order $order)
  134. {
  135. if ($user->expired_at === NULL) {
  136. $this->getSurplusValueByOneTime($user, $order);
  137. } else {
  138. $this->getSurplusValueByCycle($user, $order);
  139. }
  140. }
  141. private function getSurplusValueByOneTime(User $user, Order $order)
  142. {
  143. $plan = Plan::find($user->plan_id);
  144. $trafficUnitPrice = $plan->onetime_price / $plan->transfer_enable;
  145. if ($user->discount && $trafficUnitPrice) {
  146. $trafficUnitPrice = $trafficUnitPrice - ($trafficUnitPrice * $user->discount / 100);
  147. }
  148. $notUsedTraffic = $plan->transfer_enable - (($user->u + $user->d) / 1073741824);
  149. $result = $trafficUnitPrice * $notUsedTraffic;
  150. $orderModel = Order::where('user_id', $user->id)->where('cycle', '!=', 'reset_price')->where('status', 3);
  151. $order->surplus_amount = $result > 0 ? $result : 0;
  152. $order->surplus_order_ids = json_encode(array_column($orderModel->get()->toArray(), 'id'));
  153. }
  154. private function orderIsUsed(Order $order):bool
  155. {
  156. $month = self::STR_TO_TIME[$order->cycle];
  157. $orderExpireDay = strtotime('+' . $month . ' month', $order->created_at->timestamp);
  158. if ($orderExpireDay < time()) return true;
  159. return false;
  160. }
  161. private function getSurplusValueByCycle(User $user, Order $order)
  162. {
  163. $orderModel = Order::where('user_id', $user->id)
  164. ->where('cycle', '!=', 'reset_price')
  165. ->where('status', 3);
  166. $orders = $orderModel->get();
  167. $orderSurplusMonth = 0;
  168. $orderSurplusAmount = 0;
  169. $userSurplusMonth = ($user->expired_at - time()) / 2678400;
  170. foreach ($orders as $k => $item) {
  171. // 兼容历史余留问题
  172. if ($item->cycle === 'onetime_price') continue;
  173. if ($this->orderIsUsed($item)) continue;
  174. $orderSurplusMonth = $orderSurplusMonth + self::STR_TO_TIME[$item->cycle];
  175. $orderSurplusAmount = $orderSurplusAmount + ($item['total_amount'] + $item['balance_amount']);
  176. }
  177. if (!$orderSurplusMonth || !$orderSurplusAmount) return;
  178. $monthUnitPrice = $orderSurplusAmount / $orderSurplusMonth;
  179. // 如果用户过期月大于订单过期月
  180. if ($userSurplusMonth > $orderSurplusMonth) {
  181. $orderSurplusAmount = $orderSurplusMonth * $monthUnitPrice;
  182. } else {
  183. $orderSurplusAmount = $userSurplusMonth * $monthUnitPrice;
  184. }
  185. if (!$orderSurplusAmount) {
  186. return;
  187. }
  188. $order->surplus_amount = $orderSurplusAmount > 0 ? $orderSurplusAmount : 0;
  189. $order->surplus_order_ids = json_encode(array_column($orders->toArray(), 'id'));
  190. }
  191. public function success(string $callbackNo)
  192. {
  193. $order = $this->order;
  194. if ($order->status !== 0) {
  195. return true;
  196. }
  197. $order->status = 1;
  198. $order->callback_no = $callbackNo;
  199. return $order->save();
  200. }
  201. private function buyByResetTraffic(User $user)
  202. {
  203. $user->u = 0;
  204. $user->d = 0;
  205. }
  206. private function buyByCycle(Order $order, User $user, Plan $plan)
  207. {
  208. // change plan process
  209. if ((int)$order->type === 3) {
  210. $user->expired_at = time();
  211. }
  212. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  213. // 从一次性转换到循环
  214. if ($user->expired_at === NULL) $this->buyByResetTraffic($user);
  215. // 新购
  216. if ($order->type === 1) $this->buyByResetTraffic($user);
  217. $user->plan_id = $plan->id;
  218. $user->group_id = $plan->group_id;
  219. $user->expired_at = $this->getTime($order->cycle, $user->expired_at);
  220. }
  221. private function buyByOneTime(User $user, Plan $plan)
  222. {
  223. $this->buyByResetTraffic($user);
  224. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  225. $user->plan_id = $plan->id;
  226. $user->group_id = $plan->group_id;
  227. $user->expired_at = NULL;
  228. }
  229. private function getTime($str, $timestamp)
  230. {
  231. if ($timestamp < time()) {
  232. $timestamp = time();
  233. }
  234. switch ($str) {
  235. case 'month_price':
  236. return strtotime('+1 month', $timestamp);
  237. case 'quarter_price':
  238. return strtotime('+3 month', $timestamp);
  239. case 'half_year_price':
  240. return strtotime('+6 month', $timestamp);
  241. case 'year_price':
  242. return strtotime('+12 month', $timestamp);
  243. case 'two_year_price':
  244. return strtotime('+24 month', $timestamp);
  245. case 'three_year_price':
  246. return strtotime('+36 month', $timestamp);
  247. }
  248. }
  249. }