PaymentController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\Helpers;
  4. use App\Components\Yzy;
  5. use App\Components\AlipaySubmit;
  6. use App\Http\Models\Coupon;
  7. use App\Http\Models\Goods;
  8. use App\Http\Models\Order;
  9. use App\Http\Models\Payment;
  10. use App\Http\Models\PaymentCallback;
  11. use Illuminate\Http\Request;
  12. use Payment\Client\Charge;
  13. use Response;
  14. use Redirect;
  15. use Log;
  16. use DB;
  17. use Auth;
  18. /**
  19. * 支付控制器
  20. *
  21. * Class PaymentController
  22. *
  23. * @package App\Http\Controllers
  24. */
  25. class PaymentController extends Controller
  26. {
  27. protected static $systemConfig;
  28. function __construct()
  29. {
  30. self::$systemConfig = Helpers::systemConfig();
  31. }
  32. // 创建支付单
  33. public function create(Request $request)
  34. {
  35. $goods_id = intval($request->get('goods_id'));
  36. $coupon_sn = $request->get('coupon_sn');
  37. $goods = Goods::query()->where('is_del', 0)->where('status', 1)->where('id', $goods_id)->first();
  38. if (!$goods) {
  39. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:商品或服务已下架']);
  40. }
  41. // 判断是否开启有赞云支付
  42. if (!self::$systemConfig['is_youzan'] && !self::$systemConfig['is_alipay'] && !self::$systemConfig['is_f2fpay']) {
  43. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:系统并未开启在线支付功能']);
  44. }
  45. // 判断是否存在同个商品的未支付订单
  46. $existsOrder = Order::query()->where('status', 0)->where('user_id', Auth::user()->id)->where('goods_id', $goods_id)->exists();
  47. if ($existsOrder) {
  48. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:尚有未支付的订单,请先去支付']);
  49. }
  50. // 限购控制
  51. $strategy = self::$systemConfig['goods_purchase_limit_strategy'];
  52. if ($strategy == 'all' || ($strategy == 'package' && $goods->type == 2) || ($strategy == 'free' && $goods->price == 0) || ($strategy == 'package&free' && ($goods->type == 2 || $goods->price == 0))) {
  53. $noneExpireOrderExist = Order::query()->where('status', '>=', 0)->where('is_expire', 0)->where('user_id', Auth::user()->id)->where('goods_id', $goods_id)->exists();
  54. if ($noneExpireOrderExist) {
  55. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:商品不可重复购买']);
  56. }
  57. }
  58. // 单个商品限购
  59. if ($goods->is_limit == 1) {
  60. $noneExpireOrderExist = Order::query()->where('status', '>=', 0)->where('user_id', Auth::user()->id)->where('goods_id', $goods_id)->exists();
  61. if ($noneExpireOrderExist) {
  62. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:此商品每人限购1次']);
  63. }
  64. }
  65. // 使用优惠券
  66. if ($coupon_sn) {
  67. $coupon = Coupon::query()->where('status', 0)->where('is_del', 0)->whereIn('type', [1, 2])->where('sn', $coupon_sn)->first();
  68. if (!$coupon) {
  69. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:优惠券不存在']);
  70. }
  71. // 计算实际应支付总价
  72. $amount = $coupon->type == 2 ? $goods->price * $coupon->discount / 10 : $goods->price - $coupon->amount;
  73. $amount = $amount > 0 ? round($amount, 2) : 0; // 四舍五入保留2位小数,避免无法正常创建订单
  74. } else {
  75. $amount = $goods->price;
  76. }
  77. // 价格异常判断
  78. if ($amount < 0) {
  79. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:订单总价异常']);
  80. } elseif ($amount == 0) {
  81. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:订单总价为0,无需使用在线支付']);
  82. }
  83. // 验证账号是否存在有效期更长的套餐
  84. if ($goods->type == 2) {
  85. $existOrderList = Order::query()
  86. ->with(['goods'])
  87. ->whereHas('goods', function ($q) {
  88. $q->where('type', 2);
  89. })
  90. ->where('user_id', Auth::user()->id)
  91. ->where('is_expire', 0)
  92. ->where('status', 2)
  93. ->get();
  94. foreach ($existOrderList as $vo) {
  95. if ($vo->goods->days > $goods->days) {
  96. return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:您已存在有效期更长的套餐,只能购买流量包']);
  97. }
  98. }
  99. }
  100. DB::beginTransaction();
  101. try {
  102. $orderSn = date('ymdHis') . mt_rand(100000, 999999);
  103. $sn = makeRandStr(12);
  104. // 支付方式
  105. if (self::$systemConfig['is_youzan']) {
  106. $pay_way = 2;
  107. } elseif (self::$systemConfig['is_alipay']) {
  108. $pay_way = 4;
  109. } elseif (self::$systemConfig['is_f2fpay']) {
  110. $pay_way = 5;
  111. }
  112. // 生成订单
  113. $order = new Order();
  114. $order->order_sn = $orderSn;
  115. $order->user_id = Auth::user()->id;
  116. $order->goods_id = $goods_id;
  117. $order->coupon_id = !empty($coupon) ? $coupon->id : 0;
  118. $order->origin_amount = $goods->price;
  119. $order->amount = $amount;
  120. $order->expire_at = date("Y-m-d H:i:s", strtotime("+" . $goods->days . " days"));
  121. $order->is_expire = 0;
  122. $order->pay_way = $pay_way;
  123. $order->status = 0;
  124. $order->save();
  125. // 生成支付单
  126. if (self::$systemConfig['is_youzan']) {
  127. $yzy = new Yzy();
  128. $result = $yzy->createQrCode($goods->name, $amount * 100, $orderSn);
  129. if (isset($result['error_response'])) {
  130. Log::error('【有赞云】创建二维码失败:' . $result['error_response']['msg']);
  131. throw new \Exception($result['error_response']['msg']);
  132. }
  133. } elseif (self::$systemConfig['is_alipay']) {
  134. $parameter = [
  135. "service" => "create_forex_trade", // WAP:create_forex_trade_wap ,即时到帐:create_forex_trade
  136. "partner" => self::$systemConfig['alipay_partner'],
  137. "notify_url" => self::$systemConfig['website_url'] . "/api/alipay", // 异步回调接口
  138. "return_url" => self::$systemConfig['website_url'],
  139. "out_trade_no" => $orderSn, // 订单号
  140. "subject" => "Package", // 订单名称
  141. //"total_fee" => $amount, // 金额
  142. "rmb_fee" => $amount, // 使用RMB标价,不再使用总金额
  143. "body" => "", // 商品描述,可为空
  144. "currency" => self::$systemConfig['alipay_currency'], // 结算币种
  145. "product_code" => "NEW_OVERSEAS_SELLER",
  146. "_input_charset" => "utf-8"
  147. ];
  148. // 建立请求
  149. $alipaySubmit = new AlipaySubmit(self::$systemConfig['alipay_sign_type'], self::$systemConfig['alipay_partner'], self::$systemConfig['alipay_key'], self::$systemConfig['alipay_private_key']);
  150. $result = $alipaySubmit->buildRequestForm($parameter, "post", "确认");
  151. } elseif (self::$systemConfig['is_f2fpay']) {
  152. // TODO:goods表里增加一个字段用于自定义商品付款时展示的商品名称,
  153. // TODO:这里增加一个随机商品列表,根据goods的价格随机取值
  154. $result = Charge::run("ali_qr", [
  155. 'use_sandbox' => false,
  156. "partner" => self::$systemConfig['f2fpay_app_id'],
  157. 'app_id' => self::$systemConfig['f2fpay_app_id'],
  158. 'sign_type' => 'RSA2',
  159. 'ali_public_key' => self::$systemConfig['f2fpay_public_key'],
  160. 'rsa_private_key' => self::$systemConfig['f2fpay_private_key'],
  161. 'notify_url' => self::$systemConfig['website_url'] . "/api/f2fpay", // 异步回调接口
  162. 'return_url' => self::$systemConfig['website_url'],
  163. 'return_raw' => false
  164. ], [
  165. 'body' => '',
  166. 'subject' => self::$systemConfig['f2fpay_subject_name'],
  167. 'order_no' => $orderSn,
  168. 'amount' => $amount,
  169. ]);
  170. }
  171. $payment = new Payment();
  172. $payment->sn = $sn;
  173. $payment->user_id = Auth::user()->id;
  174. $payment->oid = $order->oid;
  175. $payment->order_sn = $orderSn;
  176. $payment->pay_way = 1;
  177. $payment->amount = $amount;
  178. if (self::$systemConfig['is_youzan']) {
  179. $payment->qr_id = $result['response']['qr_id'];
  180. $payment->qr_url = $result['response']['qr_url'];
  181. $payment->qr_code = $result['response']['qr_code'];
  182. $payment->qr_local_url = $this->base64ImageSaver($result['response']['qr_code']);
  183. } elseif (self::$systemConfig['is_alipay']) {
  184. $payment->qr_code = $result;
  185. } elseif (self::$systemConfig['is_f2fpay']) {
  186. $payment->qr_code = $result;
  187. $payment->qr_url = 'http://qr.topscan.com/api.php?text=' . $result . '&bg=ffffff&fg=000000&pt=1c73bd&m=10&w=400&el=1&inpt=1eabfc&logo=https://t.alipayobjects.com/tfscom/T1Z5XfXdxmXXXXXXXX.png';
  188. $payment->qr_local_url = $payment->qr_url;
  189. }
  190. $payment->status = 0;
  191. $payment->save();
  192. // 优惠券置为已使用
  193. if (!empty($coupon)) {
  194. if ($coupon->usage == 1) {
  195. $coupon->status = 1;
  196. $coupon->save();
  197. }
  198. Helpers::addCouponLog($coupon->id, $goods_id, $order->oid, '在线支付使用');
  199. }
  200. DB::commit();
  201. if (self::$systemConfig['is_alipay']) {
  202. // Alipay返回支付信息
  203. return Response::json(['status' => 'success', 'data' => $result, 'message' => '创建订单成功,正在转到付款页面,请稍后']);
  204. } else {
  205. return Response::json(['status' => 'success', 'data' => $sn, 'message' => '创建订单成功,正在转到付款页面,请稍后']);
  206. }
  207. } catch (\Exception $e) {
  208. DB::rollBack();
  209. Log::error('创建支付订单失败:' . $e->getMessage());
  210. return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建订单失败:' . $e->getMessage()]);
  211. }
  212. }
  213. // 支付单详情
  214. public function detail(Request $request, $sn)
  215. {
  216. if (empty($sn)) {
  217. return Redirect::to('services');
  218. }
  219. $payment = Payment::query()->with(['order', 'order.goods'])->where('sn', $sn)->where('user_id', Auth::user()->id)->first();
  220. if (!$payment) {
  221. return Redirect::to('services');
  222. }
  223. $order = Order::query()->where('oid', $payment->oid)->first();
  224. if (!$order) {
  225. \Session::flash('errorMsg', '订单不存在');
  226. return Response::view('payment/' . $sn);
  227. }
  228. $view['payment'] = $payment;
  229. $view['website_logo'] = self::$systemConfig['website_logo'];
  230. $view['website_analytics'] = self::$systemConfig['website_analytics'];
  231. $view['website_customer_service'] = self::$systemConfig['website_customer_service'];
  232. $view['is_alipay'] = self::$systemConfig['is_alipay'];
  233. $view['is_f2fpay'] = self::$systemConfig['is_f2fpay'];
  234. $view['is_youzan'] = self::$systemConfig['is_youzan'];
  235. return Response::view('payment.detail', $view);
  236. }
  237. // 获取订单支付状态
  238. public function getStatus(Request $request)
  239. {
  240. $sn = $request->get('sn');
  241. if (empty($sn)) {
  242. return Response::json(['status' => 'fail', 'data' => '', 'message' => '请求失败']);
  243. }
  244. $payment = Payment::query()->where('sn', $sn)->where('user_id', Auth::user()->id)->first();
  245. if (!$payment) {
  246. return Response::json(['status' => 'error', 'data' => '', 'message' => '支付失败']);
  247. } elseif ($payment->status > 0) {
  248. return Response::json(['status' => 'success', 'data' => '', 'message' => '支付成功']);
  249. } elseif ($payment->status < 0) {
  250. return Response::json(['status' => 'error', 'data' => '', 'message' => '订单超时未支付,已自动关闭']);
  251. } else {
  252. return Response::json(['status' => 'fail', 'data' => '', 'message' => '等待支付']);
  253. }
  254. }
  255. // 有赞云回调日志
  256. public function callbackList(Request $request)
  257. {
  258. $status = $request->get('status', 0);
  259. $query = PaymentCallback::query();
  260. if ($status) {
  261. $query->where('status', $status);
  262. }
  263. $view['list'] = $query->orderBy('id', 'desc')->paginate(10);
  264. return Response::view('payment.callbackList', $view);
  265. }
  266. }