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

497 lines
17 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;
2020-04-09 13:35:27 +08:00
use App\Services\CouponService;
2020-03-17 20:16:55 +08:00
use App\Services\OrderService;
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([
'data' => $order
]);
}
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) {
abort(500, '订单不存在');
}
$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']) {
abort(500, '订阅不存在');
}
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'))) {
2020-05-12 23:57:28 +08:00
abort(500, '您有未付款或开通中的订单,请稍后或取消再试');
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) {
abort(500, '该订阅不存在');
}
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') {
abort(500, '该订阅已售罄');
}
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') {
2019-10-29 15:33:36 +08:00
abort(500, '该订阅无法续费,请更换其他订阅');
}
2019-11-25 18:56:00 +08:00
2019-12-31 15:58:53 +08:00
if ($plan[$request->input('cycle')] === NULL) {
2020-04-26 14:52:57 +08:00
if ($request->input('cycle') === 'reset_price') {
abort(500, '该订阅当前不支持重置流量');
}
2019-11-25 18:56:00 +08:00
abort(500, '该订阅周期无法进行购买,请选择其他周期');
}
2020-01-01 17:55:16 +08:00
2020-09-10 13:08:12 +08:00
if ($request->input('cycle') === 'reset_price') {
if (!($user->expired_at > time() || $user->expired_at === NULL) && !$user->plan_id)
abort(500, '当前无法购买流量重置包');
2020-07-07 14:11:15 +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();
abort(500, '优惠券使用失败');
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();
abort(500, '余额不足');
}
$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();
abort(500, '余额不足');
}
$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();
2019-10-29 15:33:36 +08:00
abort(500, '订单创建失败');
}
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) {
2019-12-27 01:58:21 +08:00
abort(500, '订单不存在或已支付');
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
}
2019-10-29 15:33:36 +08:00
switch ($method) {
2020-06-04 18:00:33 +08:00
// return type => 0: QRCode / 1: URL / 2: No action
2019-10-29 15:33:36 +08:00
case 0:
// alipayF2F
if (!(int)config('v2board.alipay_enable')) {
abort(500, '支付方式不可用');
}
return response([
'type' => 0,
'data' => $this->alipayF2F($tradeNo, $order->total_amount)
]);
case 2:
// stripeAlipay
if (!(int)config('v2board.stripe_alipay_enable')) {
abort(500, '支付方式不可用');
}
return response([
'type' => 1,
'data' => $this->stripeAlipay($order)
]);
case 3:
// stripeWepay
if (!(int)config('v2board.stripe_wepay_enable')) {
abort(500, '支付方式不可用');
}
return response([
'type' => 0,
'data' => $this->stripeWepay($order)
]);
2019-12-27 01:50:26 +08:00
case 4:
// bitpayX
if (!(int)config('v2board.bitpayx_enable')) {
abort(500, '支付方式不可用');
}
return response([
'type' => 1,
'data' => $this->bitpayX($order)
]);
2020-01-14 00:29:59 +08:00
case 5:
2020-07-20 19:56:39 +08:00
if (!(int)config('v2board.mgate_enable')) {
2020-01-14 00:29:59 +08:00
abort(500, '支付方式不可用');
}
return response([
'type' => 1,
2020-07-20 19:56:39 +08:00
'data' => $this->mgate($order)
2020-01-14 00:29:59 +08:00
]);
2020-06-04 18:00:33 +08:00
case 6:
if (!(int)config('v2board.stripe_card_enable')) {
abort(500, '支付方式不可用');
}
return response([
'type' => 2,
'data' => $this->stripeCard($order, $request->input('token'))
]);
2020-07-20 19:56:39 +08:00
case 7:
if (!(int)config('v2board.epay_enable')) {
abort(500, '支付方式不可用');
}
return response([
'type' => 1,
'data' => $this->epay($order)
]);
2019-10-29 15:33:36 +08:00
default:
abort(500, '支付方式不存在');
}
}
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) {
abort(500, '订单不存在');
}
return response([
'data' => $order->status
]);
}
2020-01-11 13:36:52 +08:00
public function getPaymentMethod()
{
2019-10-29 15:33:36 +08:00
$data = [];
if ((int)config('v2board.alipay_enable')) {
$alipayF2F = new \StdClass();
$alipayF2F->name = '支付宝';
$alipayF2F->method = 0;
$alipayF2F->icon = 'alipay';
array_push($data, $alipayF2F);
}
if ((int)config('v2board.stripe_alipay_enable')) {
$stripeAlipay = new \StdClass();
$stripeAlipay->name = '支付宝';
$stripeAlipay->method = 2;
$stripeAlipay->icon = 'alipay';
array_push($data, $stripeAlipay);
}
if ((int)config('v2board.stripe_wepay_enable')) {
$stripeWepay = new \StdClass();
$stripeWepay->name = '微信';
$stripeWepay->method = 3;
$stripeWepay->icon = 'wechat';
array_push($data, $stripeWepay);
}
2019-12-27 01:50:26 +08:00
if ((int)config('v2board.bitpayx_enable')) {
$bitpayX = new \StdClass();
2020-07-01 14:08:51 +08:00
$bitpayX->name = config('v2board.bitpayx_name', '在线支付');
2019-12-27 01:50:26 +08:00
$bitpayX->method = 4;
2020-01-20 17:13:14 +08:00
$bitpayX->icon = 'wallet';
2019-12-27 01:50:26 +08:00
array_push($data, $bitpayX);
}
2020-07-20 19:56:39 +08:00
if ((int)config('v2board.mgate_enable')) {
2020-01-14 00:29:59 +08:00
$obj = new \StdClass();
2020-07-20 19:56:39 +08:00
$obj->name = config('v2board.mgate_name', '在线支付');
2020-01-14 00:29:59 +08:00
$obj->method = 5;
2020-01-20 17:13:14 +08:00
$obj->icon = 'wallet';
2020-01-14 00:29:59 +08:00
array_push($data, $obj);
}
2020-06-04 18:00:33 +08:00
if ((int)config('v2board.stripe_card_enable')) {
$obj = new \StdClass();
$obj->name = '信用卡';
$obj->method = 6;
$obj->icon = 'card';
array_push($data, $obj);
}
2020-07-20 19:56:39 +08:00
if ((int)config('v2board.epay_enable')) {
$obj = new \StdClass();
$obj->name = config('v2board.epay_name', '在线支付');
$obj->method = 7;
$obj->icon = 'wallet';
array_push($data, $obj);
}
2019-10-29 15:33:36 +08:00
return response([
'data' => $data
]);
}
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'))) {
abort(500, '参数有误');
}
$order = Order::where('trade_no', $request->input('trade_no'))
->where('user_id', $request->session()->get('id'))
->first();
if (!$order) {
abort(500, '订单不存在');
}
2020-01-02 00:21:39 +08:00
if ($order->status !== 0) {
2020-01-02 00:19:04 +08:00
abort(500, '只可以取消待支付订单');
}
2020-03-17 20:16:55 +08:00
$orderService = new OrderService($order);
if (!$orderService->cancel()) {
2020-01-02 00:05:54 +08:00
abort(500, '取消失败');
}
return response([
'data' => true
]);
}
2020-01-11 13:36:52 +08:00
private function alipayF2F($tradeNo, $totalAmount)
{
2019-10-29 15:33:36 +08:00
$gateway = Omnipay::create('Alipay_AopF2F');
$gateway->setSignType('RSA2'); //RSA/RSA2
$gateway->setAppId(config('v2board.alipay_appid'));
$gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
$gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
2019-12-10 10:52:09 +08:00
$gateway->setNotifyUrl(url('/api/v1/guest/order/alipayNotify'));
2019-10-29 15:33:36 +08:00
$request = $gateway->purchase();
$request->setBizContent([
2020-01-11 13:36:52 +08:00
'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
2019-10-29 15:33:36 +08:00
'out_trade_no' => $tradeNo,
'total_amount' => $totalAmount / 100
]);
/** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $response */
$response = $request->send();
$result = $response->getAlipayResponse();
if ($result['code'] !== '10000') {
2020-01-11 13:36:52 +08:00
abort(500, $result['sub_msg']);
2019-10-29 15:33:36 +08:00
}
// 获取收款二维码内容
return $response->getQrCode();
}
2020-01-11 13:36:52 +08:00
private function stripeAlipay($order)
{
2020-04-05 15:17:34 +08:00
$currency = config('v2board.stripe_currency', 'hkd');
2020-03-05 00:06:40 +08:00
$exchange = Helper::exchange('CNY', strtoupper($currency));
2019-10-29 15:33:36 +08:00
if (!$exchange) {
abort(500, '货币转换超时,请稍后再试');
}
Stripe::setApiKey(config('v2board.stripe_sk_live'));
$source = Source::create([
'amount' => floor($order->total_amount * $exchange),
2020-03-05 00:06:40 +08:00
'currency' => $currency,
2019-10-29 15:33:36 +08:00
'type' => 'alipay',
2020-03-05 23:41:44 +08:00
'statement_descriptor' => $order->trade_no,
'metadata' => [
'user_id' => $order->user_id,
2020-06-04 18:00:33 +08:00
'out_trade_no' => $order->trade_no,
2020-03-05 23:41:44 +08:00
'identifier' => ''
],
2019-10-29 15:33:36 +08:00
'redirect' => [
2019-12-21 00:01:11 +08:00
'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
2019-10-29 15:33:36 +08:00
]
]);
if (!$source['redirect']['url']) {
abort(500, '支付网关请求失败');
}
return $source['redirect']['url'];
}
2020-01-11 13:36:52 +08:00
private function stripeWepay($order)
{
2020-04-05 15:17:34 +08:00
$currency = config('v2board.stripe_currency', 'hkd');
2020-03-05 00:06:40 +08:00
$exchange = Helper::exchange('CNY', strtoupper($currency));
2019-10-29 15:33:36 +08:00
if (!$exchange) {
abort(500, '货币转换超时,请稍后再试');
}
Stripe::setApiKey(config('v2board.stripe_sk_live'));
$source = Source::create([
'amount' => floor($order->total_amount * $exchange),
2020-03-05 00:06:40 +08:00
'currency' => $currency,
2019-10-29 15:33:36 +08:00
'type' => 'wechat',
2020-03-05 23:41:44 +08:00
'metadata' => [
'user_id' => $order->user_id,
2020-06-04 18:00:33 +08:00
'out_trade_no' => $order->trade_no,
2020-03-05 23:41:44 +08:00
'identifier' => ''
2020-03-07 11:37:39 +08:00
],
2019-10-29 15:33:36 +08:00
'redirect' => [
2019-12-21 00:01:11 +08:00
'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
2019-10-29 15:33:36 +08:00
]
]);
if (!$source['wechat']['qr_code_url']) {
abort(500, '支付网关请求失败');
}
return $source['wechat']['qr_code_url'];
}
2019-12-27 01:50:26 +08:00
2020-06-04 18:00:33 +08:00
private function stripeCard($order, string $token)
{
$currency = config('v2board.stripe_currency', 'hkd');
$exchange = Helper::exchange('CNY', strtoupper($currency));
if (!$exchange) {
abort(500, '货币转换超时,请稍后再试');
}
Stripe::setApiKey(config('v2board.stripe_sk_live'));
try {
$charge = \Stripe\Charge::create([
'amount' => floor($order->total_amount * $exchange),
'currency' => $currency,
'source' => $token,
'metadata' => [
'user_id' => $order->user_id,
'out_trade_no' => $order->trade_no,
'identifier' => ''
]
]);
} catch (\Exception $e) {
abort(500, '遇到了点问题,请刷新页面稍后再试');
}
info($charge);
if (!$charge->paid) {
abort(500, '扣款失败,请检查信用卡信息');
}
return $charge->paid;
}
2020-01-11 13:36:52 +08:00
private function bitpayX($order)
{
2019-12-27 01:50:26 +08:00
$bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
2020-01-11 13:36:52 +08:00
$params = [
2020-01-20 23:34:53 +08:00
'merchant_order_id' => $order->trade_no,
2020-01-11 13:36:52 +08:00
'price_amount' => $order->total_amount / 100,
'price_currency' => 'CNY',
'title' => '支付单号:' . $order->trade_no,
'description' => '充值:' . $order->total_amount / 100 . ' 元',
'callback_url' => url('/api/v1/guest/order/bitpayXNotify'),
'success_url' => config('v2board.app_url', env('APP_URL')) . '/#/order',
'cancel_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
2019-12-27 01:50:26 +08:00
];
$strToSign = $bitpayX->prepareSignId($params['merchant_order_id']);
2020-01-11 13:36:52 +08:00
$params['token'] = $bitpayX->sign($strToSign);
2019-12-27 01:50:26 +08:00
$result = $bitpayX->mprequest($params);
2020-03-07 11:37:39 +08:00
// Log::info('bitpayXSubmit: ' . json_encode($result));
2019-12-27 01:50:26 +08:00
return isset($result['payment_url']) ? $result['payment_url'] : false;
}
2020-01-14 00:29:59 +08:00
2020-07-20 19:56:39 +08:00
private function mgate($order)
2020-01-14 00:29:59 +08:00
{
2020-07-25 00:33:34 +08:00
$mgate = new MGate(config('v2board.mgate_url'), config('v2board.mgate_app_id'), config('v2board.mgate_app_secret'));
2020-07-20 19:56:39 +08:00
$result = $mgate->pay([
'app_id' => config('v2board.mgate_app_id'),
2020-01-14 00:29:59 +08:00
'out_trade_no' => $order->trade_no,
'total_amount' => $order->total_amount,
2020-07-20 19:56:39 +08:00
'notify_url' => url('/api/v1/guest/order/mgateNotify'),
2020-01-14 01:16:16 +08:00
'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
2020-01-14 00:29:59 +08:00
]);
return $result;
}
2020-07-20 19:56:39 +08:00
private function epay($order)
{
$epay = new Epay(config('v2board.epay_url'), config('v2board.epay_pid'), config('v2board.epay_key'));
return $epay->pay([
'money' => $order->total_amount / 100,
'name' => $order->trade_no,
'notify_url' => url('/api/v1/guest/order/epayNotify'),
'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order',
'out_trade_no' => $order->trade_no
]);
}
2019-10-29 15:33:36 +08:00
}