From c2dc87433bbc86b9e6929ac65b5e293f511873bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BB=91=E7=A8=BD=E5=88=98?= <1073236926@qq.com> Date: Sun, 14 Mar 2021 10:57:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=89=AB=E7=A0=81=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E4=B8=8Eapp=E6=94=AF=E4=BB=98=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../litemall/wx/service/WxOrderService.java | 149 +++++++++++++++++- .../litemall/wx/web/WxOrderController.java | 24 +++ 2 files changed, 172 insertions(+), 1 deletion(-) diff --git a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/service/WxOrderService.java b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/service/WxOrderService.java index d83b51b8..ec0577cc 100644 --- a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/service/WxOrderService.java +++ b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/service/WxOrderService.java @@ -2,13 +2,18 @@ package org.linlinjava.litemall.wx.service; import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; +import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult; import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult; import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; import com.github.binarywang.wxpay.bean.result.BaseWxPayResult; +import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult; +import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.constant.WxPayConstants; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.WxPayService; +import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; + import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -597,7 +602,7 @@ public class WxOrderService { fee = actualPrice.multiply(new BigDecimal(100)).intValue(); orderRequest.setTotalFee(fee); orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request)); - + result = wxPayService.createOrder(orderRequest); } catch (Exception e) { e.printStackTrace(); @@ -609,6 +614,148 @@ public class WxOrderService { } return ResponseUtil.ok(result); } + + /** + * app付款订单的预支付会话标识 + *

+ * 1. 检测当前订单是否能够付款 + * 2. 微信商户平台返回支付订单ID + * 3. 设置订单付款状态 + * + * @param userId 用户ID + * @param body 订单信息,{ orderId:xxx } + * @return 支付订单ID + */ + @Transactional + public Object preapppay(Integer userId, String body, HttpServletRequest request) { + if (userId == null) { + return ResponseUtil.unlogin(); + } + Integer orderId = JacksonUtil.parseInteger(body, "orderId"); + if (orderId == null) { + return ResponseUtil.badArgument(); + } + + LitemallOrder order = orderService.findById(userId, orderId); + if (order == null) { + return ResponseUtil.badArgumentValue(); + } + if (!order.getUserId().equals(userId)) { + return ResponseUtil.badArgumentValue(); + } + + // 检测是否能够取消 + OrderHandleOption handleOption = OrderUtil.build(order); + if (!handleOption.isPay()) { + return ResponseUtil.fail(ORDER_INVALID_OPERATION, "订单不能支付"); + } + WxPayAppOrderResult result = null; + try { + WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest(); + orderRequest.setOutTradeNo(order.getOrderSn()); + orderRequest.setBody("订单:" + order.getOrderSn()); + // 元转成分 + int fee = 0; + BigDecimal actualPrice = order.getActualPrice(); + fee = actualPrice.multiply(new BigDecimal(100)).intValue(); + orderRequest.setTotalFee(fee); + orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request)); + //重新设置支付信息 + WxPayConfig payConfig = new WxPayConfig(); + payConfig.setAppId(""); + payConfig.setMchId(""); + payConfig.setMchKey(""); + payConfig.setNotifyUrl(""); + payConfig.setKeyPath(""); + payConfig.setTradeType("APP"); + payConfig.setSignType("MD5"); + WxPayService wxAppPayService = new WxPayServiceImpl(); + wxAppPayService.setConfig(payConfig); + result = wxAppPayService.createOrder(orderRequest); + } catch (Exception e) { + e.printStackTrace(); + return ResponseUtil.fail(ORDER_PAY_FAIL, "订单不能支付"); + } + + if (orderService.updateWithOptimisticLocker(order) == 0) { + return ResponseUtil.updatedDateExpired(); + } + return ResponseUtil.ok(result); + } + + /** + * 二维码付款订单的预支付会话标识 + *

+ * 1. 检测当前订单是否能够付款 + * 2. 微信商户平台返回支付订单ID + * 3. 设置订单付款状态 + * + * @param userId 用户ID + * @param body 订单信息,{ orderId:xxx } + * @return 支付订单ID + */ + @Transactional + public Object preqrpay(Integer userId, String body, HttpServletRequest request) { + if (userId == null) { + return ResponseUtil.unlogin(); + } + Integer orderId = JacksonUtil.parseInteger(body, "orderId"); + if (orderId == null) { + return ResponseUtil.badArgument(); + } + + LitemallOrder order = orderService.findById(userId, orderId); + if (order == null) { + return ResponseUtil.badArgumentValue(); + } + if (!order.getUserId().equals(userId)) { + return ResponseUtil.badArgumentValue(); + } + + // 检测是否能够取消 + OrderHandleOption handleOption = OrderUtil.build(order); + if (!handleOption.isPay()) { + return ResponseUtil.fail(ORDER_INVALID_OPERATION, "订单不能支付"); + } + + LitemallUser user = userService.findById(userId); + String openid = user.getWeixinOpenid(); + if (openid == null) { + return ResponseUtil.fail(AUTH_OPENID_UNACCESS, "订单不能支付"); + } + WxPayUnifiedOrderResult result = null; + try { + WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest(); + orderRequest.setOutTradeNo(order.getOrderSn()); + orderRequest.setBody("订单:" + order.getOrderSn()); + // 元转成分 + int fee = 0; + BigDecimal actualPrice = order.getActualPrice(); + fee = actualPrice.multiply(new BigDecimal(100)).intValue(); + orderRequest.setTotalFee(fee); + orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request)); + //重新设置支付信息 + WxPayConfig payConfig = new WxPayConfig(); + payConfig.setAppId(""); + payConfig.setMchId(""); + payConfig.setMchKey(""); + payConfig.setNotifyUrl(""); + payConfig.setKeyPath(""); + payConfig.setTradeType("NATIVE"); + payConfig.setSignType("MD5"); + WxPayService wxQrPayService = new WxPayServiceImpl(); + wxQrPayService.setConfig(payConfig); + result = wxQrPayService.createOrder(orderRequest); + } catch (Exception e) { + e.printStackTrace(); + return ResponseUtil.fail(ORDER_PAY_FAIL, "订单不能支付"); + } + + if (orderService.updateWithOptimisticLocker(order) == 0) { + return ResponseUtil.updatedDateExpired(); + } + return ResponseUtil.ok(result); + } /** * 微信H5支付 diff --git a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java index 92fa1d6a..0cf4eace 100644 --- a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java +++ b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java @@ -92,6 +92,30 @@ public class WxOrderController { return wxOrderService.prepay(userId, body, request); } + /** + * 微信扫码支付 + * + * @param userId 用户ID + * @param body 订单信息,{ orderId:xxx } + * @return 支付订单ID + */ + @PostMapping("preqrpay") + public Object pay2code(@LoginUser Integer userId, @RequestBody String body, HttpServletRequest request) { + return wxOrderService.preqrpay(userId, body, request); + } + + /** + * 微信app支付 + * + * @param userId 用户ID + * @param body 订单信息,{ orderId:xxx } + * @return 支付订单ID + */ + @PostMapping("preapppay") + public Object appprepay(@LoginUser Integer userId, @RequestBody String body, HttpServletRequest request) { + return wxOrderService.preapppay(userId, body, request); + } + /** * 微信H5支付 * @param userId -- Gitee