PaymentController.php 8.9 KB

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