From 8dceb015ec8bd9642297d757e73936af84f4aaf1 Mon Sep 17 00:00:00 2001 From: mitao <2763622819@qq.com> Date: 星期三, 14 八月 2024 18:32:19 +0800 Subject: [PATCH] 修改bug --- ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/PaylogServiceImpl.java | 94 ++++++++++++++++++++++++++++++++--------------- 1 files changed, 64 insertions(+), 30 deletions(-) diff --git a/ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/PaylogServiceImpl.java b/ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/PaylogServiceImpl.java index 76dbfc5..07acaaf 100644 --- a/ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/PaylogServiceImpl.java +++ b/ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/PaylogServiceImpl.java @@ -1,5 +1,6 @@ package com.ruoyi.order.service.impl; +import com.alibaba.fastjson2.JSON; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayTradeRefundRequest; @@ -12,9 +13,11 @@ import com.ruoyi.common.core.constant.SecurityConstants; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.enums.BondStatusEnum; +import com.ruoyi.common.core.enums.OrderFromEnum; import com.ruoyi.common.core.enums.OrderStatusEnum; import com.ruoyi.common.core.enums.PaymentMethodEnum; import com.ruoyi.common.core.enums.PointStatusEnum; +import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.order.domain.Paylog; import com.ruoyi.order.mapper.OrderAuctionBondMapper; @@ -29,6 +32,7 @@ import com.ruoyi.order.util.tencent.common.XMLParser; import com.ruoyi.system.api.domain.GoodsGroupPurchase; import com.ruoyi.system.api.domain.GoodsGroupPurchaseInfo; +import com.ruoyi.system.api.domain.GoodsSeckill; import com.ruoyi.system.api.domain.GoodsSku; import com.ruoyi.system.api.domain.Member; import com.ruoyi.system.api.domain.Order; @@ -51,6 +55,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.SortedMap; @@ -63,6 +68,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; /** @@ -73,6 +79,7 @@ * @author mitao * @since 2024-05-22 */ +@Slf4j @Service public class PaylogServiceImpl extends ServiceImpl<PaylogMapper, Paylog> implements IPaylogService { @@ -99,8 +106,11 @@ @Override public R<Map<String, Object>> getPayInfo(Long uid, Integer type, String orderNO, String openId, HttpServletRequest request) { + PaymentMethodEnum paymentMethodEnum = + type == 1 ? PaymentMethodEnum.ALIPAY : PaymentMethodEnum.WECHAT; Member member = memberClient.getMembeOne(uid, SecurityConstants.INNER).getData(); openId = member.getMiniOpenid(); + String zfbOpenid = member.getZfbOpenid(); Double price = 0.0; String subject; String body; @@ -111,14 +121,10 @@ wrapper1.eq(OrderAuctionBond::getOrderNo,orderNO); wrapper1.eq(OrderAuctionBond::getDelFlag,0); OrderAuctionBond one1 = orderAuctionBondMapper.selectOne(wrapper1); - one1.setBoundStatus(BondStatusEnum.PAID); - one1.setPaymentMethod(PaymentMethodEnum.WECHAT); - orderAuctionBondMapper.updateById(one1); - - if (one1.getBond()==null||one1.getBond().compareTo(new BigDecimal(0))==0){ one1.setBoundStatus(BondStatusEnum.PAID); Map<String, Object> returnMap = new HashMap<>(); + // Type 1是否需要支付 returnMap.put("Type",1); return R.ok(returnMap); } @@ -129,17 +135,44 @@ wrapper1.eq(Order::getOrderNo,orderNO); wrapper1.eq(Order::getDelFlag,0); Order page1 = OrderMapper.selectOne(wrapper1); - page1.setOrderStatus(OrderStatusEnum.TO_BE_SHIPPED); - page1.setPaymentMethod(PaymentMethodEnum.WECHAT); - page1.setPayTime(LocalDateTime.now()); - OrderMapper.updateById(page1); + // 判断商品库存 + Long goodsSkuId = page1.getGoodsSkuId(); + OrderFromEnum orderFrom = page1.getOrderFrom(); + switch (orderFrom) { + case COMMODITY_ORDER: + GoodsSku goods = goodsSkuClient.getGoodsSkuOne(goodsSkuId, + SecurityConstants.INNER).getData(); + if (Objects.isNull(goods)) { + throw new ServiceException("该商品已下架"); + } + if (goods.getStock() <= 0) { + throw new ServiceException("该商品已售罄"); + } + if (goods.getStock() < page1.getGoodsQuantity()) { + throw new ServiceException("该商品库存不足"); + } + break; + case SNAP_ORDERS: + GoodsSeckill data = goodsSkuClient.getGoodsSeckillOne(goodsSkuId, + SecurityConstants.INNER).getData(); + if (Objects.isNull(data)) { + throw new ServiceException("该商品已下架"); + } + if (data.getSeckillStock() <= 0) { + throw new ServiceException("该商品已售罄"); + } + if (data.getSeckillStock() < page1.getGoodsQuantity()) { + throw new ServiceException("该商品库存不足"); + } + break; + default: + break; + } if (page1.getTotalAmount().compareTo(new BigDecimal(0))==0){ Map<String, Object> returnMap = new HashMap<>(); returnMap.put("Type",1); return R.ok(returnMap); } - OrderMapper.updateById(page1); - if (page1.getPoints()!=0){ MemberPointsDTO memberPointsDTO=new MemberPointsDTO(); memberPointsDTO.setMemberId(page1.getMemberId()); @@ -169,7 +202,7 @@ subject = " 订单支付"; if (type == 1) { // 支付宝预下单 - return this.alipay(orderNO, subject, body, price, request); + return this.alipay(zfbOpenid, orderNO, subject, body, price, request); } if (type == 2){ // 微信预下单 return this.wxpay(2 ,orderNO, body, openId, price, request); @@ -185,7 +218,7 @@ Map<String, Object> map = new HashMap<String, Object>(); if (type == 1) { // 支付宝预下单 - return this.alipay(orderNO, subject, body, price, request); + return this.alipay(zfbOpenid, orderNO, subject, body, price, request); } if (type == 2) { // 微信预下单 return this.wxpay(2, orderNO, body,openId, price, request); @@ -194,7 +227,7 @@ } } catch (Exception e) { - e.printStackTrace(); + log.info("获取异常", e); } return R.fail("获取异常"); } @@ -208,6 +241,7 @@ */ @Override public void notifyUrl(HttpServletRequest request, HttpServletResponse res) { + log.info("=============================支付宝回调============================="); HttpServletResponse response = (HttpServletResponse) res; response.setContentType("text/html;charset=UTF-8"); PrintWriter out; @@ -320,7 +354,7 @@ log.debug("AlipayController.notifyUrl__回调逻辑代码处理异常!fail"); // 返回失败 out.println("fail"); - e.printStackTrace(); + log.error("AlipayController.notifyUrl__回调逻辑代码处理异常!fail", e); } // //////////////////////////////////////////////////////////////////////////////////////// } else {// 验证失败 @@ -330,7 +364,7 @@ } catch ( IOException e) { log.debug("AlipayController.notifyUrl__支付宝服务器异步通知数据处理失败!"); - e.printStackTrace(); + log.error("AlipayController.notifyUrl__支付宝服务器异步通知数据处理失败!", e); } } @@ -508,6 +542,7 @@ + "\", " + "\"refund_amount\":" + refund_amount + ", " + "\"refund_reason\":\"正常退款\"," + "\"out_request_no\":\"HZ01RF001\"" + "}"); AlipayTradeRefundResponse response = alipayClient.execute(request); + log.info("支付宝退款response:" + response.getBody()); // 调用成功,则处理业务逻辑 if (response.isSuccess()) { return true; @@ -528,20 +563,24 @@ * @param request * @return */ - public R<Map<String, Object>> alipay(String orderNo, String subject, String body, Double price, + public R<Map<String, Object>> alipay(String zfbOpenid, String orderNo, String subject, + String body, Double price, HttpServletRequest request) { try { // 接口封装支付宝请求参数 // Map<String, Object> mData = new HashMap<String, Object>(); // 构建请求支付签名参数 - Map<String, Object> pay = PayDemoActivity.appPay(subject, body, price, orderNo); + // Map<String, Object> pay = PayDemoActivity.appPay(subject, body, price, orderNo); + Map<String, Object> pay = PayDemoActivity.jsApiPay(subject, body, price, orderNo, + zfbOpenid); /* * Set<Entry<String, String>> entrySet = pay.entrySet(); * Iterator<Entry<String, String>> iterator = entrySet.iterator(); * while (iterator.hasNext()) { Entry<String, String> next = * iterator.next(); mData.put(next.getKey(), next.getValue()); } */ + log.info("支付宝预支付信息:{}", JSON.toJSONString(pay)); return R.ok(pay); } catch (Exception e) { e.printStackTrace(); @@ -730,11 +769,6 @@ System.out.println("微信退款结果: " + xmlResult); return true; } - - public static void main(String[] args) { - refundForWxpay(4, "4200002335202408022336923702", - "BO24080216090001", "R123456789", 1, 1, "2"); - } /** * 获取支付宝 * @@ -743,7 +777,7 @@ * @throws Exception */ private Paylog getPayLog_alipay(HttpServletRequest request) throws IOException { - //////// 获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//////// + //////// 获取支付宝的通知返回参数,可参考技术文档中页面跳转步通知同参数列表(以下仅供参考)//////// // 商户订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8"); // 支付宝交易号 @@ -751,12 +785,12 @@ // 交易状态 String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8"); // 支付者唯一Id - String buyer_id = new String(request.getParameter("buyer_id").getBytes("ISO-8859-1"), "UTF-8"); + // String buyer_id = new String(request.getParameter("buyer_id").getBytes("ISO-8859-1"), "UTF-8"); // 支付帐号 - String buyer_email = ""; - if (SinataUtil.isNotEmpty(request.getParameter("buyer_logon_id"))) { - buyer_email = new String(request.getParameter("buyer_logon_id").getBytes("ISO-8859-1"), "UTF-8"); - } + // String buyer_email = ""; + // if (SinataUtil.isNotEmpty(request.getParameter("buyer_logon_id"))) { + // buyer_email = new String(request.getParameter("buyer_logon_id").getBytes("ISO-8859-1"), "UTF-8"); + // } // 支付金额 String total_fee = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8"); // 支付时间 @@ -766,7 +800,7 @@ Paylog paylog = new Paylog(); paylog.setOutTradeNo(out_trade_no); paylog.setPayType(1); - paylog.setBuyerId(buyer_id); + // paylog.setBuyerId(buyer_id); paylog.setTradeNo(trade_no); paylog.setPayMoney(Double.parseDouble(total_fee)); paylog.setState(1); -- Gitblit v1.7.1