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 | 96 +++++++++++++++++++++++++++++++++-------------- 1 files changed, 67 insertions(+), 29 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 0f61df1..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,6 @@ 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; @@ -14,22 +15,27 @@ 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.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.util.Date; +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; @@ -41,12 +47,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 +81,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,28 +106,35 @@ @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) { + logger.error("鍒涘缓寰俊鏀粯鍑洪敊", e); throw new OrderPayException(OrderPayException.CODE_CREATE_PAY_ORDER_FAIL, e.getMessage()); } } @@ -125,15 +143,23 @@ @Override public String getAlipayForm(String formId) { - return redisManager.getCommonString(formId); + Jedis jedis = jedisPool.getResource(); + try { + return jedis.get(formId); + } finally { + jedis.close(); + } } @Override @Transactional(rollbackFor = Exception.class) - public VipOrder checkOrderPayState(Long id) { + public VipOrder checkOrderPayState(Long id) throws VipOrderException, OrderPayException { VipOrder order = vipOrderMapper.selectByPrimaryKey(id); - if (order == null || order.getState() == VipOrder.STATE_PAY) { + if (order == null) { + throw new VipOrderException(VipOrderException.CODE_NOT_EXIST, "璁㈠崟涓嶅瓨鍦�"); + } + if (order.getState() == VipOrder.STATE_PAY) { return order; } switch (order.getPayWay()) { @@ -141,31 +167,43 @@ case "alipay": AlipayTradeQueryResponse res = null; try { - res = AlipayH5PayUtil.queryOrder(PayUtil.getAlipayApp(), id + "", null); + res = AlipayH5PayUtil.queryOrder(PayUtil.getAlipayApp(), order.getThreeOrderId(), null); //鏀粯鎴愬姛 if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) { - try { - return paySuccess(order); - } catch (Exception e) { - e.printStackTrace(); - } + return paySuccess(order); + } else { + throw new OrderPayException(OrderPayException.CODE_PAY_FAIL, "鏀粯瀹濅粯娆炬湭鎴愬姛"); } } catch (AlipayApiException e) { - e.printStackTrace(); + throw new OrderPayException(OrderPayException.CODE_API_ERROR, "鏀粯瀹濇帴鍙h姹傚嚭閿欙細" + e.getErrMsg()); } //寰俊 case "weChat": + WXPayOrderInfoV3 info = null; try { - WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo(id + "", PayUtil.getWXAPP()); - if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) { - paySuccess(order); - return order; - } + info = WXPayV3Util.getPayOrderInfo(order.getThreeOrderId(), PayUtil.getWXAPP()); } catch (Exception e) { - e.printStackTrace(); + 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, "浠樻鏂瑰紡涓嶅瓨鍦�"); } - 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()); } -- Gitblit v1.8.0