VipController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Http\Controllers\V1\User;
  3. use App\Http\Controllers\V1\Controller;
  4. use App\Models\Pay;
  5. use App\Models\Setting;
  6. use App\Models\UserVipRecord;
  7. use App\Models\VipCombo;
  8. use Carbon\Carbon;
  9. use Illuminate\Database\QueryException;
  10. class VipController extends Controller
  11. {
  12. public function setting()
  13. {
  14. $lists = VipCombo::select(['id', 'name', 'price', 'valid_day', 'desc'])->where('status', 1)->get();
  15. return $this->success($lists);
  16. }
  17. public function createOrder()
  18. {
  19. try {
  20. /* @var Setting $setting */
  21. $setting = Setting::first();
  22. if ($setting->is_review) {
  23. throw new \Exception('暂不支持,请联系管理员');
  24. }
  25. \DB::beginTransaction();
  26. $comboId = \request()->input('id');
  27. $app = $this->getUniFactory(\user()->info->platform);
  28. $combo = VipCombo::find($comboId);
  29. // 避免重复创建订单 一个小时内有未支付的订单直接使用
  30. $vip = UserVipRecord::where('user_id', \user()->id)
  31. ->where('combo_id', $combo->id)
  32. ->where('status', 0)
  33. ->where('created_at', '>=', Carbon::now()->subHour()->toDateTimeString())
  34. ->first();
  35. if ($vip) {
  36. $pay = Pay::find($vip->pay_id);
  37. if (3 == \user()->info->platform) {
  38. $res = $app->payment()->jssdk->bridgeConfig($pay->prepay_id);
  39. $res = json_decode($res, true);
  40. } else {
  41. $res = [
  42. 'order_id' => $pay->prepay_id,
  43. 'order_token' => $pay->token,
  44. ];
  45. }
  46. } else {
  47. $res = app(Pay::class)->create($app, $combo->price, Pay::SOURCE_BUY_VIP);
  48. $vip = new UserVipRecord();
  49. $vip->user_id = \user()->id;
  50. $vip->combo_id = $combo->id;
  51. $vip->valid_day = $combo->valid_day;
  52. $vip->pay_id = $res['pay_id'];
  53. $vip->status = 0;
  54. $vip->save();
  55. if (3 == \user()->info->platform) {
  56. $pay = Pay::find($res['pay_id']);
  57. $res = $app->payment()->jssdk->bridgeConfig($pay->prepay_id);
  58. $res = json_decode($res, true);
  59. }
  60. }
  61. $res['pay_id'] = $vip->pay_id;
  62. \DB::commit();
  63. return $this->success($res);
  64. } catch (QueryException $e) {
  65. \DB::rollBack();
  66. return $this->error('下单失败');
  67. } catch (\Exception $e) {
  68. \DB::rollBack();
  69. return $this->error($e->getMessage());
  70. }
  71. }
  72. }