RechargeController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Http\Controllers\V1\User;
  3. use App\Auth;
  4. use App\Http\Controllers\V1\Controller;
  5. use App\libs\helpers\Helper;
  6. use App\libs\helpers\LogHelper;
  7. use App\libs\helpers\Response;
  8. use App\libs\wechat\pay\Wechat;
  9. use App\Models\Order;
  10. use App\Models\Pay;
  11. use App\Models\PaymentConfig;
  12. use App\Models\RechargeCombo;
  13. use App\Models\Setting;
  14. use App\Models\User;
  15. use App\Models\UserRechargeRecord;
  16. use Carbon\Carbon;
  17. // use Dingo\Api\Http\Request;
  18. use Illuminate\Database\Eloquent\Builder;
  19. use Illuminate\Database\QueryException;
  20. use Illuminate\Http\Request;
  21. class RechargeController extends Controller
  22. {
  23. public function record(Request $request)
  24. {
  25. $limit = $request->input('limit', 10);
  26. $page = $request->input('page', 1);
  27. $offset = ($page - 1) * 10;
  28. $date = $request->input('date');
  29. $date = $date ?? Carbon::now()->format('Y-m');
  30. $lists = UserRechargeRecord::filterUser()
  31. ->when($date, function ($query, $date) {
  32. /* @var Builder $query */
  33. return $query->where('created_at', 'like', "$date%");
  34. })
  35. ->where('status', 1)
  36. ->orderByDesc('id')
  37. ->limit($limit)
  38. ->offset($offset)
  39. ->get();
  40. return $this->success($lists);
  41. }
  42. public function pay(Request $request)
  43. {
  44. try {
  45. $id = $request->post('id', 1);
  46. $payConfig = PaymentConfig::query()->where('state', 1)->find($id);
  47. if (!$payConfig) {
  48. return $this->error('非法请求');
  49. }
  50. if ($payConfig->is_new) {
  51. $isOrder = Order::query()->where('user_id', Auth::$userId)->where('state', 1)->first();
  52. if ($isOrder) {
  53. return Response::fail('抱歉,此套餐只能新用户专享');
  54. }
  55. }
  56. $order = [
  57. 'user_id' => Auth::$userId,
  58. 'order' => Helper::generateNonceStr(32),
  59. 'amount' => $payConfig->amount,
  60. 'diamond' => $payConfig->diamond,
  61. 'config_id' => $payConfig->id,
  62. ];
  63. $result = Order::query()->create($order);
  64. if (!$result) {
  65. return $this->error('订单提交失败');
  66. }
  67. // User::query()->where('id', Auth::$userId)->increment('diamond', $payConfig->diamond);
  68. $ret = Wechat::addOrder($result, Auth::$user);
  69. $result->pay = json_encode($ret);
  70. $result->save();
  71. $config = [];
  72. if (isset($ret['return_code'], $ret['result_code'])) {
  73. if ('SUCCESS' == $ret['result_code'] && 'SUCCESS' == $ret['return_code']) {
  74. $config = $this->getJsApiParameters($ret['prepay_id']);
  75. } else {
  76. return $this->error($ret['err_code_des']);
  77. }
  78. } else {
  79. return $this->error('参数异常,请稍后再试' . json_encode($ret, 256));
  80. }
  81. $code = 0;
  82. return $this->success(compact('config', 'code'));
  83. } catch (\Exception $exception) {
  84. LogHelper::exceptionLog($exception, '_pay');
  85. return Response::fail($exception->getMessage() . $exception->getFile() . $exception->getLine());
  86. }
  87. }
  88. /**
  89. * 获取 JSAPI 支付,前端所需参数数据.
  90. *
  91. * @param string $prepayId 统一下单成功返回的id
  92. *
  93. * @return array
  94. */
  95. public function getJsApiParameters($prepayId)
  96. {
  97. $arr = config('swdz.wechatPay');
  98. $data = [
  99. 'appId' => $arr['appid'],
  100. 'timeStamp' => (string) time(),
  101. 'nonceStr' => Helper::generateNonceStr(),
  102. 'package' => 'prepay_id=' . $prepayId,
  103. 'signType' => 'MD5',
  104. ];
  105. $data['paySign'] = Helper::makeSign($data, $arr['key']);
  106. return $data;
  107. }
  108. public function createOrder()
  109. {
  110. try {
  111. /* @var Setting $setting */
  112. $setting = Setting::first();
  113. if ($setting->is_review) {
  114. throw new \Exception('暂不支持,请联系管理员');
  115. }
  116. \DB::beginTransaction();
  117. $comboId = \request()->input('id');
  118. $app = $this->getUniFactory(\user()->info->platform);
  119. $combo = RechargeCombo::find($comboId);
  120. // 避免重复创建订单 一个小时内有未支付的订单直接使用
  121. $recharge = UserRechargeRecord::where('user_id', \user()->id)
  122. ->where('combo_id', $combo->id)
  123. ->where('status', 0)
  124. ->where('created_at', '>=', Carbon::now()->subHour()->toDateTimeString())
  125. ->first();
  126. if ($recharge) {
  127. $pay = Pay::find($recharge->pay_id);
  128. if (3 == \user()->info->platform) {
  129. $res = $app->payment()->jssdk->bridgeConfig($pay->prepay_id);
  130. $res = json_decode($res, true);
  131. } else {
  132. $res = [
  133. 'order_id' => $pay->prepay_id,
  134. 'order_token' => $pay->token,
  135. ];
  136. }
  137. } else {
  138. $res = app(Pay::class)->create($app, $combo->price, Pay::SOURCE_RECHARGE);
  139. $recharge = new UserRechargeRecord();
  140. $recharge->user_id = \user()->id;
  141. $recharge->combo_id = $combo->id;
  142. $recharge->price = $combo->price;
  143. $recharge->gold = $combo->gold;
  144. $recharge->gift = $combo->gift;
  145. $recharge->pay_id = $res['pay_id'];
  146. $recharge->status = 0;
  147. $recharge->save();
  148. if (3 == \user()->info->platform) {
  149. $pay = Pay::find($res['pay_id']);
  150. $res = $app->payment()->jssdk->bridgeConfig($pay->prepay_id);
  151. $res = json_decode($res, true);
  152. }
  153. }
  154. $res['pay_id'] = $recharge->pay_id;
  155. \DB::commit();
  156. return $this->success($res);
  157. } catch (QueryException $e) {
  158. \DB::rollBack();
  159. return $this->error('下单失败');
  160. } catch (\Exception $e) {
  161. \DB::rollBack();
  162. return $this->error($e->getMessage());
  163. }
  164. }
  165. }