PaymentController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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\Models\Coupon;
  12. use App\Models\Goods;
  13. use App\Models\Order;
  14. use App\Models\Payment;
  15. use App\Models\PaymentCallback;
  16. use Auth;
  17. use Illuminate\Http\JsonResponse;
  18. use Illuminate\Http\Request;
  19. use Log;
  20. use Response;
  21. /**
  22. * 支付控制器
  23. *
  24. * Class PaymentController
  25. *
  26. * @package App\Http\Controllers
  27. */
  28. class PaymentController extends Controller
  29. {
  30. private static $method;
  31. public static function notify(Request $request): int
  32. {
  33. self::$method = $request->input('method');
  34. Log::info(
  35. self::$method . "回调接口[POST]:" . self::$method . var_export(
  36. $request->all(),
  37. true
  38. )
  39. );
  40. self::getClient()->notify($request);
  41. return 0;
  42. }
  43. public static function getClient()
  44. {
  45. switch (self::$method) {
  46. case 'credit':
  47. return new Local();
  48. case 'f2fpay':
  49. return new F2Fpay();
  50. case 'codepay':
  51. return new Codepay();
  52. case 'payjs':
  53. return new PayJs();
  54. case 'bitpayx':
  55. return new BitpayX();
  56. case 'paypal':
  57. return new PayPal();
  58. case 'epay':
  59. return new EPay();
  60. default:
  61. Log::error("未知支付:" . self::$method);
  62. return false;
  63. }
  64. }
  65. public static function getStatus(Request $request): JsonResponse
  66. {
  67. $payment = Payment::whereTradeNo($request->input('trade_no'))->first();
  68. if ($payment) {
  69. if ($payment->status == 1) {
  70. return Response::json(
  71. ['status' => 'success', 'message' => '支付成功']
  72. );
  73. }
  74. if ($payment->status == -1) {
  75. return Response::json(
  76. ['status' => 'error', 'message' => '订单超时未支付,已自动关闭']
  77. );
  78. }
  79. return Response::json(['status' => 'fail', 'message' => '等待支付']);
  80. }
  81. return Response::json(['status' => 'error', 'message' => '未知订单']);
  82. }
  83. // 创建支付订单
  84. public function purchase(Request $request)
  85. {
  86. $goods_id = $request->input('goods_id');
  87. $coupon_sn = $request->input('coupon_sn');
  88. self::$method = $request->input('method');
  89. $credit = $request->input('amount');
  90. $pay_type = $request->input('pay_type');
  91. $amount = 0;
  92. $goods = Goods::find($goods_id);
  93. // 充值余额
  94. if ($credit) {
  95. if ( ! is_numeric($credit) || $credit <= 0) {
  96. return Response::json(
  97. ['status' => 'fail', 'message' => '充值余额不合规']
  98. );
  99. }
  100. $amount = $credit;
  101. // 购买服务
  102. } elseif ($goods_id && self::$method) {
  103. if ( ! $goods || ! $goods->status) {
  104. return Response::json(
  105. ['status' => 'fail', 'message' => '订单创建失败:商品已下架']
  106. );
  107. }
  108. $amount = $goods->price;
  109. // 是否有生效的套餐
  110. $activePlan = Order::userActivePlan()->doesntExist();
  111. // 无生效套餐,禁止购买加油包
  112. if ($goods->type == 1 && $activePlan) {
  113. return Response::json(
  114. ['status' => 'fail', 'message' => '购买加油包前,请先购买套餐']
  115. );
  116. }
  117. //非余额付款下,检查在线支付是否开启
  118. if (self::$method !== 'credit') {
  119. // 判断是否开启在线支付
  120. if ( ! sysConfig('is_onlinePay')) {
  121. return Response::json(
  122. ['status' => 'fail', 'message' => '订单创建失败:系统并未开启在线支付功能']
  123. );
  124. }
  125. // 判断是否存在同个商品的未支付订单
  126. if (Order::uid()->whereStatus(0)->exists()) {
  127. return Response::json(
  128. [
  129. 'status' => 'fail',
  130. 'message' => '订单创建失败:尚有未支付的订单,请先去支付',
  131. ]
  132. );
  133. }
  134. } elseif (Auth::getUser()->credit < $amount) { // 验证账号余额是否充足
  135. return Response::json(
  136. ['status' => 'fail', 'message' => '您的余额不足,请先充值']
  137. );
  138. }
  139. // 单个商品限购
  140. if ($goods->limit_num) {
  141. $count = Order::uid()->where('status', '>=', 0)->whereGoodsId(
  142. $goods_id
  143. )->count();
  144. if ($count >= $goods->limit_num) {
  145. return Response::json(
  146. [
  147. 'status' => 'fail',
  148. 'message' => '此商品限购' . $goods->limit_num . '次,您已购买' . $count . '次',
  149. ]
  150. );
  151. }
  152. }
  153. // 使用优惠券 TODO 代码整合至 CouponService
  154. if ($coupon_sn) {
  155. $coupon = Coupon::whereStatus(0)
  156. ->whereIn('type', [1, 2])
  157. ->whereSn($coupon_sn)
  158. ->first();
  159. if ( ! $coupon) {
  160. return Response::json(
  161. ['status' => 'fail', 'message' => '订单创建失败:优惠券不存在']
  162. );
  163. }
  164. // 计算实际应支付总价
  165. $amount = $coupon->type == 2 ? $goods->price * $coupon->value / 100 : $goods->price - $coupon->value;
  166. $amount = $amount > 0 ? round(
  167. $amount,
  168. 2
  169. ) : 0; // 四舍五入保留2位小数,避免无法正常创建订单
  170. }
  171. // 价格异常判断
  172. if ($amount < 0) {
  173. return Response::json(
  174. ['status' => 'fail', 'message' => '订单创建失败:订单总价异常']
  175. );
  176. }
  177. if ($amount == 0 && self::$method !== 'credit') {
  178. return Response::json(
  179. ['status' => 'fail', 'message' => '订单创建失败:订单总价为0,无需使用在线支付']
  180. );
  181. }
  182. }
  183. $orderSn = date('ymdHis') . random_int(100000, 999999);
  184. // 生成订单
  185. $order = new Order();
  186. $order->order_sn = $orderSn;
  187. $order->user_id = Auth::id();
  188. $order->goods_id = $credit ? 0 : $goods_id;
  189. $order->coupon_id = ! empty($coupon) ? $coupon->id : 0;
  190. $order->origin_amount = $credit ?: $goods->price;
  191. $order->amount = $amount;
  192. $order->is_expire = 0;
  193. $order->pay_type = $pay_type;
  194. $order->pay_way = self::$method;
  195. $order->status = 0;
  196. $order->save();
  197. // 使用优惠券,减少可使用次数
  198. if ( ! empty($coupon)) {
  199. if ($coupon->usable_times > 0) {
  200. Coupon::whereId($coupon->id)->decrement('usable_times', 1);
  201. }
  202. Helpers::addCouponLog('订单支付使用', $coupon->id, $goods_id, $order->id);
  203. }
  204. $request->merge(
  205. ['id' => $order->id, 'type' => $pay_type, 'amount' => $amount]
  206. );
  207. // 生成支付单
  208. return self::getClient()->purchase($request);
  209. }
  210. public function close(Request $request): JsonResponse
  211. {
  212. $order = Order::find($request->input('id'));
  213. if ($order) {
  214. if ( ! $order->update(['status' => -1])) {
  215. return Response::json(
  216. ['status' => 'fail', 'message' => '关闭订单失败']
  217. );
  218. }
  219. } else {
  220. return Response::json(['status' => 'fail', 'message' => '未找到订单']);
  221. }
  222. return Response::json(['status' => 'success', 'message' => '关闭订单成功']);
  223. }
  224. // 支付单详情
  225. public function detail($trade_no)
  226. {
  227. $payment = Payment::uid()
  228. ->with(['order', 'order.goods'])
  229. ->whereTradeNo(
  230. $trade_no
  231. )
  232. ->firstOrFail();
  233. $view['payment'] = $payment;
  234. $goods = $payment->order->goods;
  235. $view['name'] = $goods ? $goods->name : '余额充值';
  236. $view['days'] = $goods ? $goods->days : 0;
  237. $view['pay_type'] = $payment->order->pay_type_label ?: 0;
  238. $view['pay_type_icon'] = $payment->order->pay_type_icon;
  239. return view('user.payment', $view);
  240. }
  241. // 回调日志
  242. public function callbackList(Request $request)
  243. {
  244. $status = $request->input('status', 0);
  245. $query = PaymentCallback::query();
  246. if (isset($status)) {
  247. $query->whereStatus($status);
  248. }
  249. $view['list'] = $query->latest()->paginate(10)->appends(
  250. $request->except('page')
  251. );
  252. return view('admin.logs.callbackList', $view);
  253. }
  254. }