OrderService.php 9.3 KB

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