PaymentController.php 8.6 KB

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