From c9fdff7d45ae118eac10397d8f34661e9f2e968a Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期三, 12 一月 2022 10:28:18 +0800
Subject: [PATCH] 礼金bug修复

---
 service-vip/src/main/java/com/ks/vip/service/remote/VipOrderPayServiceImpl.java |  337 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 183 insertions(+), 154 deletions(-)

diff --git a/service-vip/src/main/java/com/ks/vip/service/remote/VipOrderPayServiceImpl.java b/service-vip/src/main/java/com/ks/vip/service/remote/VipOrderPayServiceImpl.java
index ad7fcda..e66b4ae 100644
--- a/service-vip/src/main/java/com/ks/vip/service/remote/VipOrderPayServiceImpl.java
+++ b/service-vip/src/main/java/com/ks/vip/service/remote/VipOrderPayServiceImpl.java
@@ -1,5 +1,9 @@
 package com.ks.vip.service.remote;
 
+import org.apache.dubbo.config.annotation.Service;
+import com.alipay.api.AlipayApiException;
+import com.alipay.api.response.AlipayTradeQueryResponse;
+import com.ks.vip.exception.OrderPayException;
 import com.ks.vip.exception.VipOrderException;
 import com.ks.vip.mapper.VipOrderMapper;
 import com.ks.vip.pojo.DO.VipCombo;
@@ -8,30 +12,30 @@
 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;
-import org.apache.dubbo.config.annotation.Service;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 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.math.BigDecimal;
-import java.math.RoundingMode;
 import java.util.Date;
-import java.util.List;
+import java.util.Map;
 import java.util.UUID;
 
 @Service(version = "1.0")
 public class VipOrderPayServiceImpl implements VipOrederPayService {
+
+    Logger logger = LoggerFactory.getLogger(VipOrederPayService.class);
 
     @Resource
     private VipOrderMapper vipOrderMapper;
@@ -43,174 +47,199 @@
     private VipCenterService vipCenterService;
 
     @Resource
-    private RedisManager redisManager;
+    private JedisPool jedisPool;
 
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public PayWayInfoDTO payOrder(VipOrder order) {
-        String title = Constant.ORDER_PAY_TITLE;
-        String orderId = order.getId() + "";
+    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涓嶈兘涓虹┖");
+        }
+        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.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, ip);
+        } catch (OrderPayException e) {
+            e.printStackTrace();
+            throw new VipOrderException(VipOrderException.CODE_CREATE_PAY_ORDER_FAIL, e.getMsg());
+        }
+    }
+
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    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.getThreeOrderId() + "";
         switch (order.getPayWay()) {
+            //鐢熸垚鏀粯瀹濇敮浠樿鍗�
             case "alipay":
-                String formAlipay = PayUtil.getVipChargeAlipayForm(orderId, title, orderId, order.getPayMoney());
+                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, formAlipay, 120);
-                String payUrl = Constant.HOST + "/BuWan/alipay/printPayForm?formId=" + id;
-                return new PayWayInfoDTO(PayWayEnum.alipay.getWay(), payUrl);
+                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 formWX = PayUtil.createWXOrder(orderId, null, orderId,order.getPayMoney() ,title);
-                    //鏆傚瓨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);
+                try {
+                    String payUrl = PayUtil.createWXOrder(ip, orderId, order.getPayMoney(), goodsTitle, notifyUrl, returnUrl);
+                    return new PayWayInfoDTO(PayWayEnum.weChat, payUrl, null);
                 } catch (Exception e) {
-
+                    logger.error("鍒涘缓寰俊鏀粯鍑洪敊", e);
+                    throw new OrderPayException(OrderPayException.CODE_CREATE_PAY_ORDER_FAIL, e.getMessage());
                 }
         }
         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;
+    public String getAlipayForm(String formId) {
+        Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.get(formId);
+        } finally {
+            jedis.close();
         }
-
-
-
-//        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 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 (){
-//                }
-//        }
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public VipOrder checkOrderPayState(Long id) throws VipOrderException, OrderPayException {
+        VipOrder order = vipOrderMapper.selectByPrimaryKey(id);
+        if (order == null) {
+            throw new VipOrderException(VipOrderException.CODE_NOT_EXIST, "璁㈠崟涓嶅瓨鍦�");
+        }
+        if (order.getState() == VipOrder.STATE_PAY) {
+            return order;
+        }
+        switch (order.getPayWay()) {
+            //鏀粯瀹�
+            case "alipay":
+                AlipayTradeQueryResponse res = null;
+                try {
+                    res = AlipayH5PayUtil.queryOrder(PayUtil.getAlipayApp(), order.getThreeOrderId(), null);
+                    //鏀粯鎴愬姛
+                    if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) {
+                        return paySuccess(order);
+                    } else {
+                        throw new OrderPayException(OrderPayException.CODE_PAY_FAIL, "鏀粯瀹濅粯娆炬湭鎴愬姛");
+                    }
+                } catch (AlipayApiException e) {
+                    throw new OrderPayException(OrderPayException.CODE_API_ERROR, "鏀粯瀹濇帴鍙h姹傚嚭閿欙細" + e.getErrMsg());
+                }
+                //寰俊
+            case "weChat":
+                WXPayOrderInfoV3 info = null;
+                try {
+                    info = WXPayV3Util.getPayOrderInfo(order.getThreeOrderId(), PayUtil.getWXAPP());
+                } catch (Exception e) {
+                    throw new OrderPayException(OrderPayException.CODE_API_ERROR, "寰俊鏀粯鎺ュ彛璇锋眰鍑洪敊锛�" + e.getMessage());
+                }
+                if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) {
+                    paySuccess(order);
+                    return order;
+                } else {
+                    throw new OrderPayException(OrderPayException.CODE_PAY_FAIL, "寰俊浠樻鏈垚鍔�");
+                }
+            default:
+                throw new OrderPayException(OrderPayException.CODE_PAY_WAY_NOT_EXIST, "浠樻鏂瑰紡涓嶅瓨鍦�");
+        }
+    }
+
+    @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());
+    }
 
 
+    /**
+     * 鏀粯鎴愬姛鏇存柊澶勭悊
+     *
+     * @param order
+     * @return
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public VipOrder paySuccess(VipOrder order) throws VipOrderException {
 
-//        @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;
-//
-//        }
+        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;
+        }
+
+        // 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;
+    }
+
 }

--
Gitblit v1.8.0