yujian
2021-05-29 b4a195f07fc3c0c417d9bb2f06acf76f5a62f29e
service-vip/src/main/java/com/ks/vip/service/remote/VipOrderPayServiceImpl.java
@@ -1,5 +1,7 @@
package com.ks.vip.service.remote;
import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.ks.vip.exception.VipOrderException;
import com.ks.vip.mapper.VipOrderMapper;
import com.ks.vip.pojo.DO.VipCombo;
@@ -8,11 +10,9 @@
import com.ks.vip.pojo.DTO.VipOrderDTO;
import com.ks.vip.pojo.Enums.OrderTypeEnum;
import com.ks.vip.pojo.Enums.PayWayEnum;
import com.ks.vip.query.VipOrderQuery;
import com.ks.vip.service.VipCenterService;
import com.ks.vip.service.VipComboService;
import com.ks.vip.service.VipOrederPayService;
import com.ks.vip.service.VipOrederService;
import com.ks.vip.service.manager.RedisManager;
import com.ks.vip.util.Constant;
import com.ks.vip.util.PayUtil;
@@ -24,10 +24,7 @@
import org.yeshi.utils.wx.WXPayV3Util;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service(version = "1.0")
@@ -48,25 +45,76 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayWayInfoDTO addOrderByBuy(VipOrderDTO t) throws VipOrderException {
        if (t.getComboId() == null || t.getComboId() <= 0) {
            throw new VipOrderException(1, "套餐ID不能为空");
        }
        if (StringUtil.isNullOrEmpty(t.getUid())) {
            throw new VipOrderException(1, "用户ID不能为空");
        }
        if (t.getPayWay() == null) {
            throw new VipOrderException(1, "付款方式不能为空");
        }
        if (StringUtil.isNullOrEmpty(t.getThreeOrderId())) {
            throw new VipOrderException(1, "付款订单号不能为空");
        }
        // 检查是否已记录
        VipOrder order = vipOrderMapper.getByThreeOrderId(t.getThreeOrderId());
        if (order != null) {
            throw new VipOrderException(1, "该付款订单号已存在");
        }
        // 判断订单类型: 首单、续费
        OrderTypeEnum orderType = OrderTypeEnum.buyNewly;
        long count = vipOrderMapper.countByTypeAndUid(t.getUid(), orderType);
        if (count > 0) {
            orderType = OrderTypeEnum.buyRenew;
        }
        // 插入订单信息
        VipOrder newOrder = new VipOrder();
        newOrder.setComboId(t.getComboId());
        newOrder.setUid(t.getUid());
        newOrder.setThreeOrderId(t.getThreeOrderId());
        newOrder.setPayWay(t.getPayWay().name());
        newOrder.setType(orderType);
        newOrder.setCreateTime(new Date());
        vipOrderMapper.insertSelective(newOrder);
        // 支付方式
        return payOrder(newOrder);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayWayInfoDTO payOrder(VipOrder order) {
        String title = Constant.ORDER_PAY_TITLE;
        String goodsTitle = Constant.ORDER_PAY_TITLE;
        String orderId = order.getId() + "";
        switch (order.getPayWay()) {
            case "alipay":
                String formAlipay = PayUtil.getVipChargeAlipayForm(orderId, title, orderId, order.getPayMoney());
            case "alipay":  //生成支付宝支付订单
                if (order.getState() == VipOrder.STATE_PAY) {
                    return new PayWayInfoDTO(PayWayEnum.alipay.getWay(), PayUtil.getPaySuccessUrl(orderId));
                }
                String form = PayUtil.getVipChargeAlipayForm(orderId, goodsTitle, orderId, order.getPayMoney());
                //暂存2分钟
                String id = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
                redisManager.cacheCommonString(id, formAlipay, 120);
                redisManager.cacheCommonString(id, form, 120);
                String payUrl = Constant.HOST + "/BuWan/alipay/printPayForm?formId=" + id;
                return new PayWayInfoDTO(PayWayEnum.alipay.getWay(), payUrl);
            case "weChat":
                try{
                    String formWX = PayUtil.createWXOrder(orderId, null, orderId,order.getPayMoney() ,title);
                try {
                    if (order.getState() == VipOrder.STATE_PAY) {
                        return new PayWayInfoDTO(PayWayEnum.weChat.getWay(), PayUtil.getPaySuccessUrl(orderId));
                    }
                    String formWX = PayUtil.createWXOrder(orderId, null, orderId, order.getPayMoney(), goodsTitle);
                    //暂存2分钟
                    String idwx = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
                    redisManager.cacheCommonString(idwx, formWX, 120);
                    String payUrlwx = Constant.HOST + "/BuWan/alipay/printPayForm?formId=" + idwx;
                    return new PayWayInfoDTO(PayWayEnum.weChat.getWay(), payUrlwx);
                    String idWX = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
                    redisManager.cacheCommonString(formWX, formWX, 120);
                    String payUrlWX = Constant.HOST + "/BuWan/alipay/printPayForm?formId=" + idWX;
                    return new PayWayInfoDTO(PayWayEnum.weChat.getWay(), payUrlWX);
                } catch (Exception e) {
                }
@@ -74,143 +122,78 @@
        return null;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public VipOrder checkOrderPayState(Long id) {
        VipOrder vipOrder = vipOrderMapper.selectByPrimaryKey(id);
        if (vipOrder == null || vipOrder.getState() == VipOrder.STATE_PAY) {
            return vipOrder;
        VipOrder order = vipOrderMapper.selectByPrimaryKey(id);
        if (order == null || order.getState() == VipOrder.STATE_PAY) {
            return order;
        }
//        OrderRecord record = vipOrderRecordDao.find(OrderRecord.class, id);
//            if (record.getState() == OrderRecord.STATE_PAY)
//                return record;
//
//            switch (record.getPayWay()) {
//                case OrderRecord.PAY_WAY_ALIPAY_GOLDCORN:
//                case OrderRecord.PAY_WAY_ALIPAY: {
//                    //支付宝
//                    AlipayTradeQueryResponse res = null;
//                    try {
//                        res = AlipayH5PayUtil.queryOrder(VipUtil.getAlipayApp(), VIPOrderUtil.getOutOrderNo(record.getOrderType(), id), null);
//                        //支付成功
//                        if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) {
//                            try {
//                                return paySuccess(id, OrderRecord.PAY_WAY_ALIPAY, new BigDecimal(res.getTotalAmount()), new Date());
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    } catch (AlipayApiException e) {
//                        e.printStackTrace();
//                    }
//                }
//
//                break;
//                case OrderRecord.PAY_WAY_WX_GOLDCORN:
//                case OrderRecord.PAY_WAY_WX: {
//                    //微信
//                    try {
//                        WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo(VIPOrderUtil.getOutOrderNo(record.getOrderType(), id), VipUtil.getWXAPP());
//                        if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) {
//                            paySuccess(id, record.getPayWay(), new BigDecimal(info.getAmount().getPayer_total()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR), new Date());
//                            return record;
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//                break;
//            }
        switch (order.getPayWay()) {
            case "alipay":  //支付宝
                AlipayTradeQueryResponse res = null;
                try {
                    res = AlipayH5PayUtil.queryOrder(PayUtil.getAlipayApp(), id + "", null);
                    //支付成功
                    if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) {
                        try {
                            return paySuccess(order);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (AlipayApiException e) {
                    e.printStackTrace();
                }
            case "weChat": //微信
                try {
                    WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo(id + "", PayUtil.getWXAPP());
                    if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) {
                        paySuccess(order);
                        return order;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
        return null;
    }
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public PayWayInfoDTO payOrder(OrderRecord record) throws OrderException, GoldCornException, PayException, VIPException, PPTVException, VideoBuyRecordException {
//        String orderNo = VIPOrderUtil.getOutOrderNo(record.getOrderType(), record.getId());
//        switch (record.getPayWay()) {
//            case OrderRecord.PAY_WAY_ALIPAY_GOLDCORN:
//                try {
//                    goldCornManager.consumeGoldCorn(record, record.getGoldCorn());
//                } catch (Exception e) {
//                    throw new GoldCornException(1, "影视豆扣除出错");
//                }
//                paySuccess(record.getId(), OrderRecord.PAY_WAY_GOLDCORN, null, new Date());
//
//            case OrderRecord.PAY_WAY_ALIPAY: {
//                if (record.getMoney() != null && record.getMoney().compareTo(new BigDecimal(0)) == 0) {
//                    return new PayWayInfoDTO(0, VipUtil.getPaySuccessUrl(record.getId()));
//                }
//                //生成支付宝支付订单
//                String form = VipUtil.getVipChargeAlipayForm(record.getId(), record.getOrderType(), orderNo, record.getMoney());
//                //暂存2分钟
//                String id = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
//                redisManager.cacheCommonString(id, form, 120);
//                String payUrl = Constant.HOST + "/BuWan/alipay/printPayForm?formId=" + id;
//                orderLogger.info("订单创建支付宝支付链接成功:id-{} 链接-{}", record.getId(), payUrl);
//                return new PayWayInfoDTO(OrderRecord
//                        .PAY_WAY_ALIPAY, payUrl);
//            }
//            case OrderRecord.PAY_WAY_WX_GOLDCORN:
//                try {
//                    goldCornManager.consumeGoldCorn(record, record.getGoldCorn());
//                } catch (){
//                }
//        }
    /**
     * 支付成功更新处理
     *
     * @param order
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public VipOrder paySuccess(VipOrder order) throws VipOrderException {
        VipCombo vipCombo = vipComboService.selectByPrimaryKey(order.getComboId());
        if (vipCombo == null) {
            throw new VipOrderException(1, "该套餐不存在");
        }
        // 有效天数
        Integer validDays = vipCombo.getValidDays();
        // 判断订单类型
        OrderTypeEnum orderType = OrderTypeEnum.buyNewly;
        long count = vipOrderMapper.countByTypeAndUid(order.getUid(), orderType);
        if (count > 0) {
            orderType = OrderTypeEnum.buyRenew;
        }
//        @Transactional(rollbackFor = Exception.class)
//        @Override
//        public OrderRecord checkOrderPayState(String id) {
//            OrderRecord record = vipOrderRecordDao.find(OrderRecord.class, id);
//            if (record.getState() == OrderRecord.STATE_PAY)
//                return record;
//
//            switch (record.getPayWay()) {
//                case OrderRecord.PAY_WAY_ALIPAY_GOLDCORN:
//                case OrderRecord.PAY_WAY_ALIPAY: {
//                    //支付宝
//                    AlipayTradeQueryResponse res = null;
//                    try {
//                        res = AlipayH5PayUtil.queryOrder(VipUtil.getAlipayApp(), VIPOrderUtil.getOutOrderNo(record.getOrderType(), id), null);
//                        //支付成功
//                        if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) {
//                            try {
//                                return paySuccess(id, OrderRecord.PAY_WAY_ALIPAY, new BigDecimal(res.getTotalAmount()), new Date());
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    } catch (AlipayApiException e) {
//                        e.printStackTrace();
//                    }
//                }
//
//                break;
//                case OrderRecord.PAY_WAY_WX_GOLDCORN:
//                case OrderRecord.PAY_WAY_WX: {
//                    //微信
//                    try {
//                        WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo(VIPOrderUtil.getOutOrderNo(record.getOrderType(), id), VipUtil.getWXAPP());
//                        if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) {
//                            paySuccess(id, record.getPayWay(), new BigDecimal(info.getAmount().getPayer_total()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR), new Date());
//                            return record;
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//                break;
//            }
//            return record;
//
//        }
        // order.setThreeOrderId(); TODO
        order.setState(VipOrder.STATE_PAY);
        order.setPayTime(new Date());
        order.setUpdateTime(new Date());
        vipOrderMapper.updateByPrimaryKeySelective(order);
        // 更新等级
        vipCenterService.saveVipCenter(order.getUid(), vipCombo.getGradeId(), validDays * 24, order.getId());
        return order;
    }
}