v2board/app/Http/Controllers/User/OrderController.php

258 lines
8.8 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
2020-01-29 16:08:50 +08:00
namespace App\Http\Controllers\User;
2019-10-29 15:33:36 +08:00
use App\Http\Controllers\Controller;
2020-01-29 16:22:39 +08:00
use App\Http\Requests\User\OrderSave;
2022-02-07 14:54:13 +08:00
use App\Models\CommissionLog;
2021-04-28 16:56:08 +08:00
use App\Models\Payment;
2020-04-09 13:35:27 +08:00
use App\Services\CouponService;
2020-03-17 20:16:55 +08:00
use App\Services\OrderService;
2021-04-28 16:56:08 +08:00
use App\Services\PaymentService;
2020-03-17 20:00:46 +08:00
use App\Services\UserService;
2020-01-01 23:40:14 +08:00
use Illuminate\Http\Request;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
2020-01-01 23:40:14 +08:00
use Illuminate\Support\Facades\DB;
2019-10-29 15:33:36 +08:00
use App\Models\Order;
use App\Models\Plan;
use App\Models\User;
use App\Utils\Helper;
use Omnipay\Omnipay;
use Stripe\Stripe;
use Stripe\Source;
2019-12-27 01:50:26 +08:00
use Library\BitpayX;
2020-07-20 19:56:39 +08:00
use Library\MGate;
use Library\Epay;
2019-10-29 15:33:36 +08:00
class OrderController extends Controller
{
2020-01-11 13:36:52 +08:00
public function fetch(Request $request)
{
2020-01-27 15:49:44 +08:00
$model = Order::where('user_id', $request->session()->get('id'))
->orderBy('created_at', 'DESC');
2020-01-29 16:08:50 +08:00
if ($request->input('status') !== null) {
2020-01-27 15:49:44 +08:00
$model->where('status', $request->input('status'));
}
$order = $model->get();
2019-10-29 15:33:36 +08:00
$plan = Plan::get();
2020-01-11 13:36:52 +08:00
for ($i = 0; $i < count($order); $i++) {
for ($x = 0; $x < count($plan); $x++) {
2019-10-29 15:33:36 +08:00
if ($order[$i]['plan_id'] === $plan[$x]['id']) {
$order[$i]['plan'] = $plan[$x];
}
}
}
return response([
2021-04-15 21:41:29 +08:00
'data' => $order->makeHidden(['id', 'user_id'])
2019-10-29 15:33:36 +08:00
]);
}
2020-01-11 13:36:52 +08:00
2022-02-07 15:47:06 +08:00
public function detail(Request $request)
2020-01-11 13:36:52 +08:00
{
2019-10-29 15:33:36 +08:00
$order = Order::where('user_id', $request->session()->get('id'))
->where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
2021-06-12 00:56:39 +08:00
abort(500, __('Order does not exist or has been paid'));
2019-10-29 15:33:36 +08:00
}
$order['plan'] = Plan::find($order->plan_id);
2020-01-12 02:08:06 +08:00
$order['try_out_plan_id'] = (int)config('v2board.try_out_plan_id');
2019-10-29 15:33:36 +08:00
if (!$order['plan']) {
2021-06-12 00:56:39 +08:00
abort(500, __('Subscription plan does not exist'));
2019-10-29 15:33:36 +08:00
}
2022-02-07 15:47:06 +08:00
if ($order->surplus_order_ids) {
2022-02-09 02:39:19 +08:00
$order['surplus_orders'] = Order::whereIn('id', $order->surplus_order_ids)->get();
2022-02-07 15:47:06 +08:00
}
2019-10-29 15:33:36 +08:00
return response([
'data' => $order
]);
}
2020-01-02 00:05:54 +08:00
2020-01-11 13:36:52 +08:00
public function save(OrderSave $request)
{
2020-04-22 16:57:00 +08:00
$userService = new UserService();
if ($userService->isNotCompleteOrderByUserId($request->session()->get('id'))) {
2021-06-12 00:56:39 +08:00
abort(500, __('You have an unpaid or pending order, please try again later or cancel it'));
2020-01-02 00:05:54 +08:00
}
2020-01-02 00:07:30 +08:00
2019-10-29 15:33:36 +08:00
$plan = Plan::find($request->input('plan_id'));
$user = User::find($request->session()->get('id'));
2020-01-11 13:36:52 +08:00
2019-10-29 15:33:36 +08:00
if (!$plan) {
2021-06-12 00:56:39 +08:00
abort(500, __('Subscription plan does not exist'));
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
2021-12-31 01:48:21 +08:00
if ($plan[$request->input('period')] === NULL) {
abort(500, __('This payment period cannot be purchased, please choose another period'));
}
if ($request->input('period') === 'reset_price') {
if ($user->expired_at <= time() || !$user->plan_id) {
abort(500, __('Subscription has expired or no active subscription, unable to purchase Data Reset Package'));
}
}
2020-02-11 14:03:23 +08:00
if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
2021-12-28 01:49:33 +08:00
if ($request->input('period') !== 'reset_price') {
2021-06-12 00:56:39 +08:00
abort(500, __('This subscription has been sold out, please choose another subscription'));
2020-05-03 19:42:46 +08:00
}
2019-10-29 15:33:36 +08:00
}
2021-12-28 01:49:33 +08:00
if (!$plan->renew && $user->plan_id == $plan->id && $request->input('period') !== 'reset_price') {
2021-06-12 00:56:39 +08:00
abort(500, __('This subscription cannot be renewed, please change to another subscription'));
2019-10-29 15:33:36 +08:00
}
2019-11-25 18:56:00 +08:00
2020-07-07 14:11:15 +08:00
2020-11-07 17:23:47 +08:00
if (!$plan->show && $plan->renew && !$userService->isAvailable($user)) {
2021-06-12 00:56:39 +08:00
abort(500, __('This subscription has expired, please change to another subscription'));
2020-11-07 17:23:47 +08:00
}
2020-01-01 23:40:14 +08:00
DB::beginTransaction();
2019-10-29 15:33:36 +08:00
$order = new Order();
2020-04-22 16:57:00 +08:00
$orderService = new OrderService($order);
2019-10-29 15:33:36 +08:00
$order->user_id = $request->session()->get('id');
$order->plan_id = $plan->id;
2021-12-28 01:49:33 +08:00
$order->period = $request->input('period');
$order->trade_no = Helper::generateOrderNo();
2021-12-28 01:49:33 +08:00
$order->total_amount = $plan[$request->input('period')];
2020-04-22 16:57:00 +08:00
2020-04-09 13:35:27 +08:00
if ($request->input('coupon_code')) {
$couponService = new CouponService($request->input('coupon_code'));
if (!$couponService->use($order)) {
DB::rollBack();
2021-06-12 00:56:39 +08:00
abort(500, __('Coupon failed'));
2020-01-01 23:48:23 +08:00
}
2020-10-24 23:08:14 +08:00
$order->coupon_id = $couponService->getId();
2020-01-01 17:55:16 +08:00
}
2020-04-22 16:57:00 +08:00
$orderService->setVipDiscount($user);
$orderService->setOrderType($user);
$orderService->setInvite($user);
2020-03-17 20:00:46 +08:00
if ($user->balance && $order->total_amount > 0) {
$remainingBalance = $user->balance - $order->total_amount;
$userService = new UserService();
if ($remainingBalance > 0) {
2020-03-17 20:04:00 +08:00
if (!$userService->addBalance($order->user_id, - $order->total_amount)) {
2020-03-17 20:00:46 +08:00
DB::rollBack();
2021-06-12 00:56:39 +08:00
abort(500, __('Insufficient balance'));
2020-03-17 20:00:46 +08:00
}
$order->balance_amount = $order->total_amount;
$order->total_amount = 0;
} else {
2020-03-17 20:04:00 +08:00
if (!$userService->addBalance($order->user_id, - $user->balance)) {
2020-03-17 20:00:46 +08:00
DB::rollBack();
2021-06-12 00:56:39 +08:00
abort(500, __('Insufficient balance'));
2020-03-17 20:00:46 +08:00
}
$order->balance_amount = $user->balance;
$order->total_amount = $order->total_amount - $user->balance;
}
}
2019-10-29 15:33:36 +08:00
if (!$order->save()) {
2020-01-01 23:40:14 +08:00
DB::rollback();
2021-06-12 00:56:39 +08:00
abort(500, __('Failed to create order'));
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
2020-01-01 23:40:14 +08:00
DB::commit();
2019-10-29 15:33:36 +08:00
return response([
'data' => $order->trade_no
]);
}
2020-01-11 13:36:52 +08:00
public function checkout(Request $request)
{
2019-10-29 15:33:36 +08:00
$tradeNo = $request->input('trade_no');
$method = $request->input('method');
$order = Order::where('trade_no', $tradeNo)
->where('user_id', $request->session()->get('id'))
->where('status', 0)
->first();
if (!$order) {
2021-06-12 00:56:39 +08:00
abort(500, __('Order does not exist or has been paid'));
2019-10-29 15:33:36 +08:00
}
2020-02-17 01:27:07 +08:00
// free process
if ($order->total_amount <= 0) {
2021-09-04 15:31:25 +08:00
$orderService = new OrderService($order);
if (!$orderService->paid($order->trade_no)) abort(500, '');
2020-05-12 23:53:48 +08:00
return response([
'type' => -1,
'data' => true
]);
2020-02-17 01:27:07 +08:00
}
2021-04-28 16:56:08 +08:00
$payment = Payment::find($method);
2021-06-12 00:56:39 +08:00
if (!$payment || $payment->enable !== 1) abort(500, __('Payment method is not available'));
2021-04-28 16:56:08 +08:00
$paymentService = new PaymentService($payment->payment, $payment->id);
2022-03-17 14:50:02 +08:00
if ($order->total_amount > 0 && ($payment->handling_fee_fixed || $payment->handling_fee_percent)) {
$order->handling_amount = ($order->total_amount * ($payment->handling_fee_percent / 100)) + $payment->handling_fee_fixed;
}
$order->payment_id = $method;
if (!$order->save()) abort(500, __('Request failed, please try again later'));
2021-04-28 16:56:08 +08:00
$result = $paymentService->pay([
'trade_no' => $tradeNo,
2022-03-17 14:50:02 +08:00
'total_amount' => isset($order->handling_amount) ? ($order->total_amount + $order->handling_amount) : $order->total_amount,
2021-04-28 16:56:08 +08:00
'user_id' => $order->user_id,
'stripe_token' => $request->input('token')
]);
return response([
'type' => $result['type'],
'data' => $result['data']
]);
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
public function check(Request $request)
{
2019-10-29 15:33:36 +08:00
$tradeNo = $request->input('trade_no');
$order = Order::where('trade_no', $tradeNo)
->where('user_id', $request->session()->get('id'))
->first();
if (!$order) {
2021-06-12 00:56:39 +08:00
abort(500, __('Order does not exist'));
2019-10-29 15:33:36 +08:00
}
return response([
'data' => $order->status
]);
}
2020-01-11 13:36:52 +08:00
public function getPaymentMethod()
{
2021-04-28 16:56:08 +08:00
$methods = Payment::select([
'id',
'name',
2021-12-13 14:24:53 +08:00
'payment',
2022-03-17 14:50:02 +08:00
'icon',
'handling_fee_fixed',
'handling_fee_percent'
2021-04-28 16:56:08 +08:00
])
->where('enable', 1)->get();
2020-07-20 19:56:39 +08:00
2019-10-29 15:33:36 +08:00
return response([
2021-04-28 16:56:08 +08:00
'data' => $methods
2019-10-29 15:33:36 +08:00
]);
}
2020-01-11 13:36:52 +08:00
public function cancel(Request $request)
{
2020-01-02 00:05:54 +08:00
if (empty($request->input('trade_no'))) {
2021-06-12 00:56:39 +08:00
abort(500, __('Invalid parameter'));
2020-01-02 00:05:54 +08:00
}
$order = Order::where('trade_no', $request->input('trade_no'))
->where('user_id', $request->session()->get('id'))
->first();
if (!$order) {
2021-06-12 00:56:39 +08:00
abort(500, __('Order does not exist'));
2020-01-02 00:05:54 +08:00
}
2020-01-02 00:21:39 +08:00
if ($order->status !== 0) {
2021-06-12 00:56:39 +08:00
abort(500, __('You can only cancel pending orders'));
2020-01-02 00:19:04 +08:00
}
2020-03-17 20:16:55 +08:00
$orderService = new OrderService($order);
if (!$orderService->cancel()) {
2021-06-12 00:56:39 +08:00
abort(500, __('Cancel failed'));
2020-01-02 00:05:54 +08:00
}
return response([
'data' => true
]);
}
2019-10-29 15:33:36 +08:00
}