yuhongqi 1 vecka sedan
förälder
incheckning
a97dcb1e51

+ 8 - 0
fs-service-system/src/main/java/com/fs/live/service/ILiveOrderService.java

@@ -147,6 +147,14 @@ public interface ILiveOrderService {
 
 //    R aliPayment(FsUserLiveOrderPayUParam param);
 
+    R aliPayment(FsUserLiveOrderPayUParam param);
+
+    R H5WxPayment(FsUserLiveOrderPayUParam param);
+
+    R appWxPayment(FsUserLiveOrderPayUParam param);
+
+    R H5AliPayment(FsUserLiveOrderPayUParam param);
+
     String payConfirm(Integer type,Long orderId,String payCode,String tradeNo,String bankTransactionId,String bankSerialNo);
 
     R wxPayment(FsUserLiveOrderPayUParam param);

+ 157 - 2
fs-service-system/src/main/java/com/fs/live/service/impl/LiveOrderServiceImpl.java

@@ -566,12 +566,13 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
                 }
             } else if (type.equals(2)) {
                 order = baseMapper.selectLiveOrderByOrderId(String.valueOf(orderId));
-                if (!order.getStatus().equals(OrderInfoEnum.STATUS_0.getValue())) {
+                // LiveOrder 待支付状态为1,FsStoreOrder 待支付为0,兼容两种
+                if (!order.getStatus().equals(OrderInfoEnum.STATUS_0.getValue()) && !order.getStatus().equals(1)) {
                     TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                     return "";
                 }
             }
-            if (order.getStatus() != 1) {
+            if (order.getStatus() != 1 && !order.getStatus().equals(OrderInfoEnum.STATUS_0.getValue())) {
                 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                 return "";
             }
@@ -684,6 +685,160 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
         return R.ok();
     }
 
+    /**
+     * 统一支付创建逻辑,按 wxPayment 的实现方式:创建支付单后走 PaymentHandler.createLiveOrder
+     * @param order 直播订单
+     * @param user 用户
+     * @param payTypeCode 支付类型码 alipay/weixin
+     * @param tradeType 汇付交易类型 T_MINIAPP/T_JSAPI/A_NATIVE(仅汇付使用)
+     * @param openId 公众号mpOpenId或小程序maOpenId,支付宝可传null
+     */
+    private R doCreateLivePayment(LiveOrder order, FsUser user, String payTypeCode,
+                                   String tradeType, String openId) {
+        String json = configService.selectConfigByKey("store.pay");
+        FsPayConfig fsPayConfig = JSON.parseObject(json, FsPayConfig.class);
+        String payCode = SnowflakeUtils.nextId();
+        if (StringUtils.isEmpty(payCode)) {
+            return R.error("订单生成失败,请重试");
+        }
+        LiveOrderPayment storePayment = new LiveOrderPayment();
+        storePayment.setStatus(0);
+        storePayment.setPayMode(fsPayConfig.getType());
+        storePayment.setBusinessCode(order.getOrderCode());
+        storePayment.setPayCode(payCode);
+        storePayment.setPayMoney(order.getPayMoney());
+        storePayment.setCreateTime(new Date());
+        storePayment.setPayTypeCode(payTypeCode);
+        storePayment.setBusinessType(5);
+        storePayment.setRemark("直播商品订单支付");
+        if (openId != null) {
+            storePayment.setOpenId(openId);
+        }
+        storePayment.setUserId(user.getUserId());
+        storePayment.setBusinessId(order.getOrderId().toString());
+        if (liveOrderPaymentMapper.insertLiveOrderPayment(storePayment) > 0) {
+            LiveOrder newOrder = new LiveOrder();
+            newOrder.setOrderId(order.getOrderId());
+            newOrder.setPayPrice(order.getPayMoney());
+            baseMapper.updateLiveOrder(newOrder);
+            PayProcessContext processContext = new PayProcessContext();
+            processContext.setOrderId(order.getOrderId());
+            processContext.setPayType("alipay".equals(payTypeCode) ? 3 : 1);
+            processContext.setPaymentId(storePayment.getPaymentId());
+            processContext.setPayCode(storePayment.getPayCode());
+            processContext.setFsPayConfig(fsPayConfig);
+            processContext.setUserId(Long.valueOf(order.getUserId()));
+            processContext.setGoodsInfo("直播商品订单支付");
+            processContext.setOrderPrefix("live-");
+            processContext.setTradeType(tradeType);
+            processContext.setOpenId(openId);
+            PaymentHandler payment = PaymentHandlerHolder.findBest(fsPayConfig.getType());
+            if (ObjectUtil.isNull(payment)) {
+                throw new CustomException("支付方式不存在");
+            }
+            R process = payment.createLiveOrder(processContext);
+            if ("200".equals(String.valueOf(process.get("code")))) {
+                redisCache.setCacheObject("isPaying:" + order.getOrderId(), order.getOrderId().toString(), 1, TimeUnit.MINUTES);
+                return process;
+            }
+            return process;
+        } else {
+            return R.error("支付记录创建失败");
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
+    public R aliPayment(FsUserLiveOrderPayUParam param) {
+        LiveOrder order = baseMapper.selectLiveOrderByOrderId(String.valueOf(param.getOrderId()));
+        if (order == null) {
+            return R.error("订单不存在");
+        }
+        if (order.getStatus() != 1) {
+            return R.error("非法操作");
+        }
+        FsUser user = userMapper.selectFsUserByUserId(param.getUserId());
+        if (user == null) {
+            return R.error("用户不存在");
+        }
+        if (order.getPayMoney().compareTo(BigDecimal.ZERO) <= 0) {
+            payConfirm(2, param.getOrderId(), null, null, null, null);
+            return R.ok().put("isPay", 1);
+        }
+        return doCreateLivePayment(order, user, "alipay", "A_NATIVE", null);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
+    public R H5WxPayment(FsUserLiveOrderPayUParam param) {
+        LiveOrder order = baseMapper.selectLiveOrderByOrderId(String.valueOf(param.getOrderId()));
+        if (order == null) {
+            return R.error("订单不存在");
+        }
+        if (order.getStatus() != 1) {
+            return R.error("非法操作");
+        }
+        String orderIdCache = redisCache.getCacheObject("isPaying:" + order.getOrderId());
+        if (StringUtils.isNotEmpty(orderIdCache) && orderIdCache.equals(order.getOrderId().toString())) {
+            return R.error("正在支付中...");
+        }
+        FsUser user = userMapper.selectFsUserByUserId(param.getUserId());
+        if (user == null || StringUtils.isEmpty(user.getMpOpenId())) {
+            return R.error(401, "用户OPENID不存在");
+        }
+        if (order.getPayMoney().compareTo(BigDecimal.ZERO) <= 0) {
+            payConfirm(2, param.getOrderId(), null, null, null, null);
+            return R.ok().put("isPay", 1);
+        }
+        return doCreateLivePayment(order, user, "weixin", "T_JSAPI", user.getMpOpenId());
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
+    public R appWxPayment(FsUserLiveOrderPayUParam param) {
+        LiveOrder order = baseMapper.selectLiveOrderByOrderId(String.valueOf(param.getOrderId()));
+        if (order == null) {
+            return R.error("订单不存在");
+        }
+        if (order.getStatus() != 1) {
+            return R.error("非法操作");
+        }
+        String orderIdCache = redisCache.getCacheObject("isPaying:" + order.getOrderId());
+        if (StringUtils.isNotEmpty(orderIdCache) && orderIdCache.equals(order.getOrderId().toString())) {
+            return R.error("正在支付中...");
+        }
+        FsUser user = userMapper.selectFsUserByUserId(param.getUserId());
+        if (user == null || StringUtils.isEmpty(user.getMaOpenId())) {
+            return R.error(401, "用户OPENID不存在");
+        }
+        if (order.getPayMoney().compareTo(BigDecimal.ZERO) <= 0) {
+            payConfirm(2, param.getOrderId(), null, null, null, null);
+            return R.ok().put("isPay", 1);
+        }
+        return doCreateLivePayment(order, user, "weixin", "T_MINIAPP", user.getMaOpenId());
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
+    public R H5AliPayment(FsUserLiveOrderPayUParam param) {
+        LiveOrder order = baseMapper.selectLiveOrderByOrderId(String.valueOf(param.getOrderId()));
+        if (order == null) {
+            return R.error("订单不存在");
+        }
+        if (order.getStatus() != 1) {
+            return R.error("非法操作");
+        }
+        FsUser user = userMapper.selectFsUserByUserId(param.getUserId());
+        if (user == null) {
+            return R.error("用户不存在");
+        }
+        if (order.getPayMoney().compareTo(BigDecimal.ZERO) <= 0) {
+            payConfirm(2, param.getOrderId(), null, null, null, null);
+            return R.ok().put("isPay", 1);
+        }
+        return doCreateLivePayment(order, user, "alipay", "A_NATIVE", null);
+    }
+
     @Override
     public int tuiOrder(Long orderId) {
         LiveOrder liveOrder = new LiveOrder();

+ 16 - 4
fs-service-system/src/main/java/com/fs/store/service/channel/HfPaymentHandler.java

@@ -36,8 +36,12 @@ public class HfPaymentHandler extends PaymentHandler{
     @Override
     protected R doCreateOrder(PayProcessContext context) {
         HuiFuCreateOrder o = new HuiFuCreateOrder();
-        o.setTradeType("T_MINIAPP");
-        o.setOpenid(user.getMaOpenId());
+        String tradeType = context.getTradeType();
+        o.setTradeType(tradeType != null ? tradeType : "T_MINIAPP");
+        if (!"A_NATIVE".equals(tradeType) && !"A_JSAPI".equals(tradeType)) {
+            String openId = context.getOpenId();
+            o.setOpenid(openId != null ? openId : user.getMaOpenId());
+        }
         o.setReqSeqId(context.getOrderPrefix()+ this.fsStorePayment.getPayCode());
         o.setTransAmt(this.fsStorePayment.getPayMoney().toString());
         o.setGoodsDesc(context.getGoodsInfo());
@@ -79,8 +83,16 @@ public class HfPaymentHandler extends PaymentHandler{
     @Override
     protected R doCreateLiveOrder(PayProcessContext context) {
         HuiFuCreateOrder o = new HuiFuCreateOrder();
-        o.setTradeType("T_MINIAPP");
-        o.setOpenid(user.getMaOpenId());
+        String tradeType = context.getTradeType();
+        o.setTradeType(tradeType != null ? tradeType : "T_MINIAPP");
+        if (!"A_NATIVE".equals(tradeType) && !"A_JSAPI".equals(tradeType)) {
+            String openId = context.getOpenId();
+            if (openId != null) {
+                o.setOpenid(openId);
+            } else if (user != null && user.getMaOpenId() != null) {
+                o.setOpenid(user.getMaOpenId());
+            }
+        }
         o.setReqSeqId(context.getOrderPrefix()+ this.liveOrderPayment.getPayCode());
         o.setTransAmt(this.liveOrderPayment.getPayMoney().toString());
         o.setGoodsDesc(context.getGoodsInfo());

+ 10 - 0
fs-service-system/src/main/java/com/fs/store/service/channel/param/PayProcessContext.java

@@ -43,4 +43,14 @@ public class PayProcessContext {
      * 货物信息
      */
     private String goodsInfo;
+
+    /**
+     * 汇付交易类型:T_MINIAPP-微信小程序, T_JSAPI-微信公众号, A_NATIVE-支付宝正扫
+     */
+    private String tradeType;
+
+    /**
+     * 支付用OpenId,不传时默认用maOpenId
+     */
+    private String openId;
 }

+ 32 - 2
fs-user-app/src/main/java/com/fs/app/controller/LiveOrderController.java

@@ -322,10 +322,40 @@ public class LiveOrderController extends AppBaseController
     @ApiOperation("支付宝支付")
     @PostMapping("/aliPayment")
     @RepeatSubmit
-    public R aliPayment(@Validated @RequestBody FsUserLiveOrderPayUParam param)
+    public R aliPayment(HttpServletRequest request, @Validated @RequestBody FsUserLiveOrderPayUParam param)
     {
         param.setUserId(Long.parseLong(getUserId()));
-        return R.ok();
+        return liveOrderService.aliPayment(param);
+    }
+
+    @Login
+    @RepeatSubmit
+    @ApiOperation("H5环境下的微信支付")
+    @PostMapping("/H5WxPayment")
+    public R H5WxPayment(HttpServletRequest request, @Validated @RequestBody FsUserLiveOrderPayUParam param)
+    {
+        param.setUserId(Long.parseLong(getUserId()));
+        return liveOrderService.H5WxPayment(param);
+    }
+
+    @Login
+    @RepeatSubmit
+    @ApiOperation("APP环境下的微信支付")
+    @PostMapping("/appWxPayment")
+    public R appWxPayment(HttpServletRequest request, @Validated @RequestBody FsUserLiveOrderPayUParam param)
+    {
+        param.setUserId(Long.parseLong(getUserId()));
+        return liveOrderService.appWxPayment(param);
+    }
+
+    @Login
+    @RepeatSubmit
+    @ApiOperation("H5环境下的支付宝支付")
+    @PostMapping("/H5AliPayment")
+    public R H5AliPayment(HttpServletRequest request, @Validated @RequestBody FsUserLiveOrderPayUParam param)
+    {
+        param.setUserId(Long.parseLong(getUserId()));
+        return liveOrderService.H5AliPayment(param);
     }
 
     @Login