PaymentController.php 8.3 KB

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