PaymentController.php 8.4 KB

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