1
0

PaymentController.php 10 KB

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