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;
|
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
|
|
|
|
|
|
|
public function details(Request $request)
|
|
|
|
{
|
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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.details.order_not_exist'));
|
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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.details.plan_not_exist'));
|
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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.exist_open_order'));
|
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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.plan_not_exist'));
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2020-02-11 14:03:23 +08:00
|
|
|
if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
|
2020-05-03 19:42:46 +08:00
|
|
|
if ($request->input('cycle') !== 'reset_price') {
|
2021-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.plan_stop_sell'));
|
2020-05-03 19:42:46 +08:00
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
|
|
|
|
2020-10-04 14:21:09 +08:00
|
|
|
if (!$plan->renew && $user->plan_id == $plan->id && $request->input('cycle') !== 'reset_price') {
|
2021-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.plan_stop_renew'));
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2019-11-25 18:56:00 +08:00
|
|
|
|
2019-12-31 15:58:53 +08:00
|
|
|
if ($plan[$request->input('cycle')] === NULL) {
|
2021-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.plan_stop'));
|
2019-11-25 18:56:00 +08:00
|
|
|
}
|
2020-01-01 17:55:16 +08:00
|
|
|
|
2020-09-10 13:08:12 +08:00
|
|
|
if ($request->input('cycle') === 'reset_price') {
|
2020-10-26 15:50:37 +08:00
|
|
|
if ($user->expired_at <= time() || !$user->plan_id) {
|
2021-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.plan_exist_not_buy_package'));
|
2020-10-26 15:39:06 +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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.plan_expired'));
|
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;
|
|
|
|
$order->cycle = $request->input('cycle');
|
|
|
|
$order->trade_no = Helper::guid();
|
|
|
|
$order->total_amount = $plan[$request->input('cycle')];
|
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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.coupon_use_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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.save.order_create_failed'));
|
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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.checkout.order_not_exist_or_paid'));
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2020-02-17 01:27:07 +08:00
|
|
|
// free process
|
|
|
|
if ($order->total_amount <= 0) {
|
|
|
|
$order->total_amount = 0;
|
|
|
|
$order->status = 1;
|
|
|
|
$order->save();
|
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);
|
|
|
|
if (!$payment || $payment->enable !== 1) abort(500, __('user.order.checkout.pay_method_not_use'));
|
|
|
|
$paymentService = new PaymentService($payment->payment, $payment->id);
|
|
|
|
$result = $paymentService->pay([
|
|
|
|
'trade_no' => $tradeNo,
|
|
|
|
'total_amount' => $order->total_amount,
|
|
|
|
'user_id' => $order->user_id,
|
|
|
|
'stripe_token' => $request->input('token')
|
|
|
|
]);
|
|
|
|
$order->update(['payment_id' => $method]);
|
|
|
|
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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.check.order_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',
|
|
|
|
'payment'
|
|
|
|
])
|
|
|
|
->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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.cancel.params_wrong'));
|
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-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.cancel.order_not_exist'));
|
2020-01-02 00:05:54 +08:00
|
|
|
}
|
2020-01-02 00:21:39 +08:00
|
|
|
if ($order->status !== 0) {
|
2021-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.cancel.only_cancel_pending_order'));
|
2020-01-02 00:19:04 +08:00
|
|
|
}
|
2020-03-17 20:16:55 +08:00
|
|
|
$orderService = new OrderService($order);
|
|
|
|
if (!$orderService->cancel()) {
|
2021-01-20 18:51:55 +08:00
|
|
|
abort(500, __('user.order.cancel.cancel_failed'));
|
2020-01-02 00:05:54 +08:00
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|