PaymentController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\Helpers;
  4. use App\Models\Coupon;
  5. use App\Models\Goods;
  6. use App\Models\Order;
  7. use App\Models\Payment;
  8. use App\Payments\CodePay;
  9. use App\Payments\EPay;
  10. use App\Payments\F2Fpay;
  11. use App\Payments\Local;
  12. use App\Payments\Manual;
  13. use App\Payments\PayBeaver;
  14. use App\Payments\PayJs;
  15. use App\Payments\PayPal;
  16. use App\Payments\Stripe;
  17. use App\Payments\THeadPay;
  18. use App\Services\CouponService;
  19. use Auth;
  20. use Exception;
  21. use Illuminate\Http\JsonResponse;
  22. use Illuminate\Http\Request;
  23. use Log;
  24. use Response;
  25. class PaymentController extends Controller
  26. {
  27. public static $method;
  28. public static function notify(Request $request)
  29. {
  30. self::$method = $request->query('method') ?: $request->input('method');
  31. Log::notice(self::$method.'回调接口:'.self::$method.var_export($request->all(), true));
  32. return self::getClient()->notify($request);
  33. }
  34. public static function getClient()
  35. {
  36. switch (self::$method) {
  37. case 'credit':
  38. return new Local();
  39. case 'f2fpay':
  40. return new F2Fpay();
  41. case 'codepay':
  42. return new Codepay();
  43. case 'payjs':
  44. return new PayJs();
  45. case 'paypal':
  46. return new PayPal();
  47. case 'epay':
  48. return new EPay();
  49. case 'stripe':
  50. return new Stripe();
  51. case 'paybeaver':
  52. return new PayBeaver();
  53. case 'theadpay':
  54. return new THeadPay();
  55. case 'manual':
  56. return new Manual();
  57. default:
  58. Log::emergency('未知支付:'.self::$method);
  59. exit;
  60. }
  61. }
  62. public static function getStatus(Request $request): JsonResponse
  63. {
  64. $payment = Payment::whereTradeNo($request->input('trade_no'))->first();
  65. if ($payment) {
  66. if ($payment->status === 1) {
  67. return Response::json(['status' => 'success', 'message' => '支付成功']);
  68. }
  69. if ($payment->status === -1) {
  70. return Response::json(['status' => 'error', 'message' => '订单超时未支付,已自动关闭']);
  71. }
  72. return Response::json(['status' => 'fail', 'message' => '等待支付']);
  73. }
  74. return Response::json(['status' => 'error', 'message' => '未知订单']);
  75. }
  76. public function purchase(Request $request) // 创建支付订单
  77. {
  78. $goods_id = $request->input('goods_id');
  79. $coupon_sn = $request->input('coupon_sn');
  80. self::$method = $request->input('method');
  81. $credit = $request->input('amount');
  82. $pay_type = $request->input('pay_type');
  83. $amount = 0;
  84. // 充值余额
  85. if ($credit) {
  86. if (! is_numeric($credit) || $credit <= 0) {
  87. return Response::json(['status' => 'fail', 'message' => trans('user.payment.error')]);
  88. }
  89. $amount = $credit;
  90. } elseif ($goods_id && self::$method) { // 购买服务
  91. $goods = Goods::find($goods_id);
  92. if (! $goods || ! $goods->status) {
  93. return Response::json(['status' => 'fail', 'message' => '订单创建失败:商品已下架']);
  94. }
  95. $amount = $goods->price;
  96. // 是否有生效的套餐
  97. $activePlan = Order::userActivePlan()->doesntExist();
  98. // 无生效套餐,禁止购买加油包
  99. if ($goods->type === 1 && $activePlan) {
  100. return Response::json(['status' => 'fail', 'message' => '购买加油包前,请先购买套餐']);
  101. }
  102. // 单个商品限购
  103. if ($goods->limit_num) {
  104. $count = Order::uid()->where('status', '>=', 0)->whereGoodsId($goods_id)->count();
  105. if ($count >= $goods->limit_num) {
  106. return Response::json(['status' => 'fail', 'message' => '此商品限购'.$goods->limit_num.'次,您已购买'.$count.'次']);
  107. }
  108. }
  109. // 使用优惠券
  110. if ($coupon_sn) {
  111. $ret = (new CouponService($coupon_sn))->search($goods); // 检查券合规性
  112. if (! $ret instanceof Coupon) {
  113. return $ret;
  114. }
  115. $coupon = $ret;
  116. // 计算实际应支付总价
  117. $amount = $coupon->type === 2 ? $goods->price * $coupon->value / 100 : $goods->price - $coupon->value;
  118. $amount = $amount > 0 ? round($amount, 2) : 0; // 四舍五入保留2位小数,避免无法正常创建订单
  119. }
  120. //非余额付款下,检查在线支付是否开启
  121. if (self::$method !== 'credit') {
  122. // 判断是否开启在线支付
  123. if (! sysConfig('is_onlinePay') && ! sysConfig('wechat_qrcode') && ! sysConfig('alipay_qrcode')) {
  124. return Response::json(['status' => 'fail', 'message' => '订单创建失败:系统并未开启在线支付功能']);
  125. }
  126. // 判断是否存在同个商品的未支付订单
  127. if (Order::uid()->whereStatus(0)->exists()) {
  128. return Response::json(['status' => 'fail', 'message' => '订单创建失败:尚有未支付的订单,请先去支付']);
  129. }
  130. } elseif (Auth::getUser()->credit < $amount) { // 验证账号余额是否充足
  131. return Response::json(['status' => 'fail', 'message' => '您的余额不足,请先充值']);
  132. }
  133. // 价格异常判断
  134. if ($amount < 0) {
  135. return Response::json(['status' => 'fail', 'message' => '订单创建失败:订单总价异常']);
  136. }
  137. if ($amount === 0 && self::$method !== 'credit') {
  138. return Response::json(['status' => 'fail', 'message' => '订单创建失败:订单总价为0,无需使用在线支付']);
  139. }
  140. }
  141. // 生成订单
  142. try {
  143. $newOrder = Order::create([
  144. 'sn' => date('ymdHis').random_int(100000, 999999),
  145. 'user_id' => auth()->id(),
  146. 'goods_id' => $credit ? null : $goods_id,
  147. 'coupon_id' => $coupon->id ?? null,
  148. 'origin_amount' => $credit ?: ($goods->price ?? 0),
  149. 'amount' => $amount,
  150. 'pay_type' => $pay_type,
  151. 'pay_way' => self::$method,
  152. ]);
  153. // 使用优惠券,减少可使用次数
  154. if (! empty($coupon)) {
  155. if ($coupon->usable_times > 0) {
  156. $coupon->decrement('usable_times');
  157. }
  158. Helpers::addCouponLog('订单支付使用', $coupon->id, $goods_id, $newOrder->id);
  159. }
  160. $request->merge(['id' => $newOrder->id, 'type' => $pay_type, 'amount' => $amount]);
  161. // 生成支付单
  162. return self::getClient()->purchase($request);
  163. } catch (Exception $e) {
  164. Log::emergency('订单生成错误:'.$e->getMessage());
  165. }
  166. return Response::json(['status' => 'fail', 'message' => '订单创建失败']);
  167. }
  168. public function close(Order $order): JsonResponse
  169. {
  170. if (! $order->close()) {
  171. return Response::json(['status' => 'fail', 'message' => '关闭订单失败']);
  172. }
  173. return Response::json(['status' => 'success', 'message' => '关闭订单成功']);
  174. }
  175. public function detail($trade_no) // 支付单详情
  176. {
  177. $payment = Payment::uid()->with(['order', 'order.goods'])->whereTradeNo($trade_no)->firstOrFail();
  178. $goods = $payment->order->goods;
  179. return view('user.components.payment.default', [
  180. 'payment' => $payment,
  181. 'name' => $goods->name ?? trans('user.recharge_credit'),
  182. 'days' => $goods->days ?? 0,
  183. 'pay_type' => $payment->order->pay_type_label ?: 0,
  184. 'pay_type_icon' => $payment->order->pay_type_icon,
  185. ]);
  186. }
  187. }