admin
2021-06-09 a2944f47783f03f30c86768b7577b9378c862d6a
service-vip/src/main/java/com/ks/vip/service/remote/VipOrderPayServiceImpl.java
@@ -14,19 +14,24 @@
import com.ks.vip.service.VipCenterService;
import com.ks.vip.service.VipComboService;
import com.ks.vip.service.VipOrederPayService;
import com.ks.vip.service.manager.RedisManager;
import com.ks.vip.util.Constant;
import com.ks.vip.util.PayUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.alipay.AlipayH5PayUtil;
import org.yeshi.utils.entity.wx.WXPayOrderInfoV3;
import org.yeshi.utils.wx.WXPayV3Util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Service(version = "1.0")
public class VipOrderPayServiceImpl implements VipOrederPayService {
@@ -41,12 +46,12 @@
    private VipCenterService vipCenterService;
    @Resource
    private RedisManager redisManager;
    private JedisPool jedisPool;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayWayInfoDTO addOrderWithPay(VipOrderDTO t, String notifyUrl, String returnUrl) throws VipOrderException {
    public PayWayInfoDTO addOrderWithPay(VipOrderDTO t, String notifyUrl, String returnUrl, String ip) throws VipOrderException {
        if (t.getComboId() == null || t.getComboId() <= 0) {
            throw new VipOrderException(1, "套餐ID不能为空");
        }
@@ -75,17 +80,22 @@
        // 插入订单信息
        VipOrder newOrder = new VipOrder();
        newOrder.setState(VipOrder.STATE_INIT);
        newOrder.setComboId(t.getComboId());
        newOrder.setUid(t.getUid());
        newOrder.setThreeOrderId(t.getThreeOrderId());
        newOrder.setPayWay(t.getPayWay().name());
        newOrder.setType(orderType);
        newOrder.setPayMoney(t.getPayMoney());
        newOrder.setCreateTime(new Date());
        vipOrderMapper.insertSelective(newOrder);
        if (returnUrl != null) {
            returnUrl += "?id=" + newOrder.getId();
        }
        // 支付方式
        try {
            return payOrder(newOrder, notifyUrl, returnUrl);
            return payOrder(newOrder, notifyUrl, returnUrl, ip);
        } catch (OrderPayException e) {
            e.printStackTrace();
            throw new VipOrderException(VipOrderException.CODE_CREATE_PAY_ORDER_FAIL, e.getMsg());
@@ -95,27 +105,33 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayWayInfoDTO payOrder(VipOrder order, String notifyUrl, String returnUrl) throws OrderPayException {
    public PayWayInfoDTO payOrder(VipOrder order, String notifyUrl, String returnUrl, String ip) throws OrderPayException {
        if (order.getState() == VipOrder.STATE_PAY) {
            throw new OrderPayException(OrderPayException.CODE_ALREADY_PAY, "已经完成支付");
        }
        String goodsTitle = Constant.ORDER_PAY_TITLE;
        String orderId = order.getId() + "";
        String orderId = order.getThreeOrderId() + "";
        switch (order.getPayWay()) {
            //生成支付宝支付订单
            case "alipay":
                String form = PayUtil.getVipChargeAlipayForm(orderId, goodsTitle, orderId, order.getPayMoney(), notifyUrl, returnUrl);
                String form = PayUtil.getVipChargeAlipayForm(goodsTitle, orderId, order.getPayMoney(), notifyUrl, returnUrl);
                if (StringUtil.isNullOrEmpty(form)) {
                    throw new OrderPayException(OrderPayException.CODE_CREATE_PAY_ORDER_FAIL, "创建支付宝支付订单失败");
                }
                //暂存2分钟
                String id = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
                redisManager.cacheCommonString(id, form, 120);
                return new PayWayInfoDTO(PayWayEnum.alipay.getWay(), null, id);
                Jedis jedis = jedisPool.getResource();
                try {
                    jedis.set(id, form);
                    jedis.expire(id, 120);
                } finally {
                    jedis.close();
                }
                return new PayWayInfoDTO(PayWayEnum.alipay, null, id);
            case "weChat":
                try {
                    String payUrl = PayUtil.createWXOrder(null, orderId, order.getPayMoney(), goodsTitle, notifyUrl, returnUrl);
                    return new PayWayInfoDTO(PayWayEnum.weChat.getWay(), payUrl, null);
                    String payUrl = PayUtil.createWXOrder(ip, orderId, order.getPayMoney(), goodsTitle, notifyUrl, returnUrl);
                    return new PayWayInfoDTO(PayWayEnum.weChat, payUrl, null);
                } catch (Exception e) {
                    throw new OrderPayException(OrderPayException.CODE_CREATE_PAY_ORDER_FAIL, e.getMessage());
                }
@@ -125,7 +141,12 @@
    @Override
    public String getAlipayForm(String formId) {
        return redisManager.getCommonString(formId);
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.get(formId);
        } finally {
            jedis.close();
        }
    }
@@ -168,6 +189,16 @@
        return null;
    }
    @Override
    public String getWXPaySuccessOutOrderNo(String notifyContent) throws Exception {
        return WXPayV3Util.getPaySuccessOutOrderNo(notifyContent, PayUtil.getWXAPP());
    }
    @Override
    public String getAlipayPaySuccessOutOrderNo(Map<String, String> params) throws Exception {
        return AlipayH5PayUtil.getPaySuccessNotifyOutOrderNo(params, PayUtil.getAlipayApp());
    }
    /**
     * 支付成功更新处理