PaymentController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\F2Fpay;
  7. use App\Http\Controllers\Gateway\Local;
  8. use App\Http\Controllers\Gateway\PayJs;
  9. use App\Http\Controllers\Gateway\PayPal;
  10. use App\Models\Coupon;
  11. use App\Models\Goods;
  12. use App\Models\Order;
  13. use App\Models\Payment;
  14. use App\Models\PaymentCallback;
  15. use Auth;
  16. use Illuminate\Http\Request;
  17. use Log;
  18. use Response;
  19. /**
  20. * 支付控制器
  21. *
  22. * Class PaymentController
  23. *
  24. * @package App\Http\Controllers
  25. */
  26. class PaymentController extends Controller {
  27. private static $method;
  28. public static function notify(Request $request) {
  29. self::$method = $request->input('method');
  30. Log::info(self::$method."回调接口[POST]:".self::$method.var_export($request->all(), true));
  31. self::getClient()->notify($request);
  32. return 0;
  33. }
  34. public static function getClient() {
  35. switch(self::$method){
  36. case 'credit':
  37. return new Local();
  38. case 'f2fpay':
  39. return new F2Fpay();
  40. case 'codepay':
  41. return new Codepay();
  42. case 'payjs':
  43. return new PayJs();
  44. case 'bitpayx':
  45. return new BitpayX();
  46. case 'paypal':
  47. return new PayPal();
  48. default:
  49. Log::error("未知支付:".self::$method);
  50. return null;
  51. }
  52. }
  53. public static function returnHTML(Request $request) {
  54. return self::getClient()->getReturnHTML($request);
  55. }
  56. public static function purchaseHTML() {
  57. return Response::view('user.components.purchase');
  58. }
  59. public static function getStatus(Request $request) {
  60. $payment = Payment::whereTradeNo($request->input('trade_no'))->first();
  61. if($payment){
  62. if($payment->status == 1){
  63. return Response::json(['status' => 'success', 'message' => '支付成功']);
  64. }elseif($payment->status == -1){
  65. return Response::json(['status' => 'error', 'message' => '订单超时未支付,已自动关闭']);
  66. }else{
  67. return Response::json(['status' => 'fail', 'message' => '等待支付']);
  68. }
  69. }
  70. return Response::json(['status' => 'error', 'message' => '未知订单']);
  71. }
  72. // 创建支付订单
  73. public function purchase(Request $request) {
  74. $goods_id = $request->input('goods_id');
  75. $coupon_sn = $request->input('coupon_sn');
  76. self::$method = $request->input('method');
  77. $credit = $request->input('amount');
  78. $amount = 0;
  79. $goods = Goods::query()->whereStatus(1)->whereId($goods_id)->first();
  80. // 充值余额
  81. if($credit){
  82. if(!is_numeric($credit) || $credit <= 0){
  83. return Response::json(['status' => 'fail', 'message' => '充值余额不合规']);
  84. }
  85. $amount = $credit;
  86. // 购买服务
  87. }elseif($goods_id && self::$method){
  88. if(!$goods){
  89. return Response::json(['status' => 'fail', 'message' => '订单创建失败:商品或服务已下架']);
  90. }
  91. // 是否有生效的套餐
  92. $activePlan = Order::uid()->with(['goods'])->whereHas('goods', function($q) {
  93. $q->whereType(2);
  94. })->whereStatus(2)->whereIsExpire(0)->doesntExist();
  95. //无生效套餐,禁止购买加油包
  96. if($goods->type == 1 && $activePlan){
  97. return Response::json(['status' => 'fail', 'message' => '购买加油包前,请先购买套餐']);
  98. }
  99. //非余额付款下,检查对应的在线支付是否开启
  100. if(self::$method != 'credit'){
  101. // 判断是否开启在线支付
  102. if(!Helpers::systemConfig()['is_onlinePay']){
  103. return Response::json(['status' => 'fail', 'message' => '订单创建失败:系统并未开启在线支付功能']);
  104. }
  105. // 判断是否存在同个商品的未支付订单
  106. $existsOrder = Order::uid()->whereStatus(0)->whereGoodsId($goods_id)->exists();
  107. if($existsOrder){
  108. return Response::json(['status' => 'fail', 'message' => '订单创建失败:尚有未支付的订单,请先去支付']);
  109. }
  110. }
  111. // 单个商品限购
  112. if($goods->limit_num){
  113. $count = Order::uid()->where('status', '>=', 0)->whereGoodsId($goods_id)->count();
  114. if($count >= $goods->limit_num){
  115. return Response::json([
  116. 'status' => 'fail',
  117. 'message' => '此商品/服务限购'.$goods->limit_num.'次,您已购买'.$count.'次'
  118. ]);
  119. }
  120. }
  121. // 使用优惠券
  122. if($coupon_sn){
  123. $coupon = Coupon::query()->whereStatus(0)->whereIn('type', [1, 2])->whereSn($coupon_sn)->first();
  124. if(!$coupon){
  125. return Response::json(['status' => 'fail', 'message' => '订单创建失败:优惠券不存在']);
  126. }
  127. // 计算实际应支付总价
  128. $amount = $coupon->type == 2? $goods->price * $coupon->discount / 10 : $goods->price - $coupon->amount;
  129. $amount = $amount > 0? round($amount, 2) : 0; // 四舍五入保留2位小数,避免无法正常创建订单
  130. }else{
  131. $amount = $goods->price;
  132. }
  133. // 价格异常判断
  134. if($amount < 0){
  135. return Response::json(['status' => 'fail', 'message' => '订单创建失败:订单总价异常']);
  136. }elseif($amount == 0 && self::$method != 'credit'){
  137. return Response::json(['status' => 'fail', 'message' => '订单创建失败:订单总价为0,无需使用在线支付']);
  138. }
  139. // 验证账号余额是否充足
  140. if(self::$method == 'credit' && Auth::getUser()->credit < $amount){
  141. return Response::json(['status' => 'fail', 'message' => '您的余额不足,请先充值']);
  142. }
  143. }
  144. $orderSn = date('ymdHis').mt_rand(100000, 999999);
  145. // 生成订单
  146. $order = new Order();
  147. $order->order_sn = $orderSn;
  148. $order->user_id = Auth::id();
  149. $order->goods_id = $credit? -1 : $goods_id;
  150. $order->coupon_id = !empty($coupon)? $coupon->id : 0;
  151. $order->origin_amount = $credit?: $goods->price;
  152. $order->amount = $amount;
  153. $order->expire_at = $credit? null : date("Y-m-d H:i:s", strtotime("+".$goods->days." days"));
  154. $order->is_expire = 0;
  155. $order->pay_way = self::$method;
  156. $order->status = 0;
  157. $order->save();
  158. // 使用优惠券,减少可使用次数
  159. if(!empty($coupon)){
  160. if($coupon->usage_count > 0){
  161. Coupon::whereId($coupon->id)->decrement('usage_count', 1);
  162. }
  163. Helpers::addCouponLog($coupon->id, $goods_id, $order->oid, '订单支付使用');
  164. }
  165. $request->merge(['oid' => $order->oid, 'amount' => $amount, 'type' => $request->input('pay_type')]);
  166. // 生成支付单
  167. return self::getClient()->purchase($request);
  168. }
  169. // 支付单详情
  170. public function detail($trade_no) {
  171. $payment = Payment::uid()->with(['order', 'order.goods'])->whereTradeNo($trade_no)->first();
  172. $view['payment'] = $payment;
  173. $view['name'] = $payment->order->goods? $payment->order->goods->name : '余额充值';
  174. $view['days'] = $payment->order->goods? $payment->order->goods->days : 0;
  175. return Response::view('user.payment', $view);
  176. }
  177. // 回调日志
  178. public function callbackList(Request $request) {
  179. $status = $request->input('status', 0);
  180. $query = PaymentCallback::query();
  181. if(isset($status)){
  182. $query->whereStatus($status);
  183. }
  184. $view['list'] = $query->orderByDesc('id')->paginate(10)->appends($request->except('page'));
  185. return Response::view('admin.logs.callbackList', $view);
  186. }
  187. }