PaymentController.php 9.1 KB

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