From aa3c09b95eebe00e77f7fb21f417df86c18d8727 Mon Sep 17 00:00:00 2001 From: dzl <863620989@qq.com> Date: Mon, 19 May 2025 09:47:18 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E6=AC=BE=E7=94=B3=E8=AF=B7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=9E=E7=8E=B0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/WxPayServiceImpl.java | 79 +++++++++---------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/ebike-payment/src/main/java/com/cdzy/payment/service/impl/WxPayServiceImpl.java b/ebike-payment/src/main/java/com/cdzy/payment/service/impl/WxPayServiceImpl.java index 2f9dbc8c..499737ce 100644 --- a/ebike-payment/src/main/java/com/cdzy/payment/service/impl/WxPayServiceImpl.java +++ b/ebike-payment/src/main/java/com/cdzy/payment/service/impl/WxPayServiceImpl.java @@ -96,7 +96,7 @@ public class WxPayServiceImpl implements WxPayService { @Override - public JSONObject prepay(String orderId){ + public JSONObject prepay(String orderId) { JSONObject r = new JSONObject(); // 首先检查订单是否正在付中、或者已经支付成功 // 关闭了的订单,重新发起请求 @@ -109,26 +109,26 @@ public class WxPayServiceImpl implements WxPayService { return r; } if (String.valueOf(Transaction.TradeStateEnum.NOTPAY.ordinal()).equals(tradeState) - ||String.valueOf(Transaction.TradeStateEnum.USERPAYING.ordinal()).equals(tradeState)) { + || String.valueOf(Transaction.TradeStateEnum.USERPAYING.ordinal()).equals(tradeState)) { // 检查订单是否关闭 Transaction t = queryOrderByOutTradeNo(ebikePayment.getPaymentId()); if (t != null && Transaction.TradeStateEnum.CLOSED.equals(t.getTradeState())) { // 增加状态检查,防止已支付消息未返回重复发起请求 HandleNotifyResult hr = queryOrderStatusByOrderId(ebikePayment); - if(hr.isSuccess()){ + if (hr.isSuccess()) { r.put("error", "订单已经支付成功"); return r; } log.info("订单已经关闭,订单号:{}, 重新发起请求", orderId); ebikePayment.setTradeState(String.valueOf(Transaction.TradeStateEnum.CLOSED.ordinal())); ebikePayment.setPaymentId(StringUtils.generateSnowflakeId("paymentId")); - }else{ + } else { log.info("订单正在支付中,订单号:{}", orderId); } } else if (String.valueOf(Transaction.TradeStateEnum.CLOSED.ordinal()).equals(tradeState)) { log.info("订单已经关闭,订单号:{}, 重新发起请求", orderId); ebikePayment.setPaymentId(StringUtils.generateSnowflakeId("paymentId")); - }else { + } else { log.info("订单状态异常,订单号:{}", orderId); r.put("error", "订单状态异常: "); return r; @@ -138,7 +138,7 @@ public class WxPayServiceImpl implements WxPayService { JsonResult result = ordersFeignClient.getPaymentDetails(Long.valueOf(orderId)); if (result.getCode() != 200) { log.error("查询订单 {} 失败, {}", orderId, result.getMessage()); - r.put("error", "查询订单失败, "+result.getMessage()); + r.put("error", "查询订单失败, " + result.getMessage()); return r; } EbikePaymentDto paymentDto = JSON.parseObject(JSONObject.toJSONString(result.getData()), EbikePaymentDto.class); @@ -149,7 +149,7 @@ public class WxPayServiceImpl implements WxPayService { /** * JSAPI支付下单 * - * @param paymentDto 支付信息 + * @param paymentDto 支付信息 * @param ebikePayment 原始支付记录 * @return 下单成功返回wx支付请求参数,否则返回null */ @@ -161,7 +161,7 @@ public class WxPayServiceImpl implements WxPayService { String openId = paymentDto.getOpenId(); AmountDto amount = paymentDto.getAmount(); DetailDto detail = paymentDto.getDetail(); - String payId = ebikePayment==null?outTradeNo : ebikePayment.getPaymentId(); + String payId = ebikePayment == null ? outTradeNo : ebikePayment.getPaymentId(); try { PrepayRequest request = new PrepayRequest(); request.setAppid(wxPayConfig.getAppId()); @@ -185,21 +185,21 @@ public class WxPayServiceImpl implements WxPayService { goodsDetailReq.setGoodsName(goodsDetail.getItemName()); goodsDetailReq.setMerchantGoodsId(goodsDetail.getItemId()); goodsDetailReq.setQuantity(goodsDetail.getQuantity()); - goodsDetailReq.setUnitPrice(BigDecimal.valueOf(goodsDetail.getUnitPrice()*100.0).intValue()); + goodsDetailReq.setUnitPrice(BigDecimal.valueOf(goodsDetail.getUnitPrice() * 100.0).intValue()); return goodsDetailReq; }).toList(); - if (goodsDetails.isEmpty()){ + if (goodsDetails.isEmpty()) { goodsDetails = new ArrayList<>(); GoodsDetail goodsDetail = new GoodsDetail(); String snowId = StringUtils.generateSnowflakeId("goodsId"); goodsDetail.setMerchantGoodsId(snowId); goodsDetail.setGoodsName(goodsTag); goodsDetail.setQuantity(1); - goodsDetail.setUnitPrice(BigDecimal.valueOf(amount.getTotal()*100.0).intValue()); + goodsDetail.setUnitPrice(BigDecimal.valueOf(amount.getTotal() * 100.0).intValue()); goodsDetails.add(goodsDetail); } detailReq.setGoodsDetail(goodsDetails); - detailReq.setCostPrice(BigDecimal.valueOf(detail.getCostPrice()*100.0).intValue()); + detailReq.setCostPrice(BigDecimal.valueOf(detail.getCostPrice() * 100.0).intValue()); detailReq.setInvoiceId(detail.getInvoiceId()); request.setDetail(detailReq); SettleInfo settleInfo = new SettleInfo(); @@ -220,7 +220,7 @@ public class WxPayServiceImpl implements WxPayService { ebikePayment.setCostPrice(amount.getTotal()); ebikePayment.setTradeState(String.valueOf(Transaction.TradeStateEnum.NOTPAY.ordinal())); ebikePaymentService.save(ebikePayment); - }else{ + } else { ebikePayment.setPaymentId(payId); ebikePayment.setCreateTime(LocalDateTime.now()); ebikePayment.setPaymentMethod(PayMethod.wechat.name()); @@ -239,9 +239,9 @@ public class WxPayServiceImpl implements WxPayService { log.error("微信支付下单prepay失败,订单号:{}", outTradeNo); r.put("error", "微信支付下单prepay失败"); return r; - }catch (Exception e) { + } catch (Exception e) { logError("微信支付下单prepay", e); - r.put("error", "微信支付下单prepay失败: "+e.getMessage()); + r.put("error", "微信支付下单prepay失败: " + e.getMessage()); return r; } } @@ -257,7 +257,7 @@ public class WxPayServiceImpl implements WxPayService { @Override public Transaction queryOrderByOrderId(String orderId) { EbikePayment ebikePayment = ebikePaymentService.getByOrderId(orderId); - if (ebikePayment==null){ + if (ebikePayment == null) { log.error("{}支付订单不存在", orderId); return null; } @@ -290,8 +290,8 @@ public class WxPayServiceImpl implements WxPayService { */ private HandleNotifyResult queryOrderStatusByOrderId(EbikePayment ebikePayment) { HandleNotifyResult result = new HandleNotifyResult(); - Transaction r =queryOrderByOutTradeNo(ebikePayment.getPaymentId()); - if(r!=null&& Transaction.TradeStateEnum.SUCCESS.equals(r.getTradeState())) { + Transaction r = queryOrderByOutTradeNo(ebikePayment.getPaymentId()); + if (r != null && Transaction.TradeStateEnum.SUCCESS.equals(r.getTradeState())) { result.setMessage(String.format("订单%s支付成功", ebikePayment.getOrderId())); result.setSuccess(true); // 更新支付状态 @@ -320,7 +320,7 @@ public class WxPayServiceImpl implements WxPayService { .nonce(nonce) .build(); // 验签并解析通知 - NotificationParser parser = new NotificationParser((NotificationConfig)certificateConfig); + NotificationParser parser = new NotificationParser((NotificationConfig) certificateConfig); // 解析解密后的通知 Transaction transaction = parser.parse(requestParam, Transaction.class); // 更新状态 @@ -329,7 +329,7 @@ public class WxPayServiceImpl implements WxPayService { result.setSuccess(true); result.setMessage("success"); return result; - }catch (Exception e) { + } catch (Exception e) { result.setMessage(String.format("verify failed, request-id=[%s]", requestId)); logError("支付通知回调handlePayNotify", e); return result; @@ -340,12 +340,12 @@ public class WxPayServiceImpl implements WxPayService { public String refundApply(String outTradeNo, String reason) { // 检查是否重复退款申请 EbikeRefund ebikeRefund = ebikeRefundService.getByOrderId(outTradeNo); - if (ebikeRefund!= null) { - if(String.valueOf(Status.PROCESSING.ordinal()).equals(ebikeRefund.getStatus())){ + if (ebikeRefund != null) { + if (String.valueOf(Status.PROCESSING.ordinal()).equals(ebikeRefund.getStatus())) { log.error("订单{}正在退款中", outTradeNo); return null; } - if(String.valueOf(Status.SUCCESS.ordinal()).equals(ebikeRefund.getStatus())) { + if (String.valueOf(Status.SUCCESS.ordinal()).equals(ebikeRefund.getStatus())) { log.error("订单{}已经退款过", outTradeNo); return null; } @@ -374,7 +374,7 @@ public class WxPayServiceImpl implements WxPayService { @Override public Boolean refundReview(ReqRefundProcessDto processDto) { EbikeRefund ebikeRefund = ebikeRefundService.getById(processDto.getRefundId()); - if (ebikeRefund==null){ + if (ebikeRefund == null) { log.error("审核refundReview失败,{} 退款申请不存在", processDto.getRefundId()); return false; } @@ -382,8 +382,9 @@ public class WxPayServiceImpl implements WxPayService { ebikeRefund.setProcessState(processDto.getOperate()); ebikeRefund.setProcessTime(LocalDateTime.now()); ebikeRefund.setRemark(processDto.getReason()); - if(String.valueOf(RefundProcessState.processing).equals(processDto.getOperate())){ + if (String.valueOf(RefundProcessState.processing).equals(processDto.getOperate())) { ebikeRefund.setRefund(ebikeRefund.getRefundApply()); + ebikeRefund.setRefundMethod(processDto.getMethod()); } return ebikeRefundService.updateById(ebikeRefund); } @@ -396,7 +397,7 @@ public class WxPayServiceImpl implements WxPayService { //} // 检查退款记录是否存在 EbikeRefund ebikeRefund = ebikeRefundService.getById(refundDto.getRefundId()); - if (ebikeRefund==null){ + if (ebikeRefund == null) { log.error("退款refund失败,{} 退款申请不存在", refundDto.getRefundId()); return false; } @@ -428,7 +429,7 @@ public class WxPayServiceImpl implements WxPayService { ebikeRefund.setTotal(amount.getTotal()); ebikeRefund.setStatus(String.valueOf(result.getStatus().ordinal())); ebikeRefund.setCurrency(amount.getCurrency()); - if(Status.SUCCESS.equals(result.getStatus())){ + if (Status.SUCCESS.equals(result.getStatus())) { ebikeRefund.setRefund(result.getAmount().getRefund().doubleValue() / 100.0); ebikeRefund.setRefundTime(LocalDateTime.now()); ebikeRefund.setRemark(refundDto.getRemark()); @@ -453,20 +454,20 @@ public class WxPayServiceImpl implements WxPayService { HandleNotifyResult result = new HandleNotifyResult(); // 先查本地数据库,如果退款状态不为SUCCESS,需要查询微信支付 EbikeRefund ebikeRefund = ebikeRefundService.getById(outRefundNo); - if (ebikeRefund==null){ + if (ebikeRefund == null) { log.error("{}退款订单不存在", outRefundNo); result.setMessage(String.format("{%s}退款订单不存在", outRefundNo)); return result; } result.setSuccess(String.valueOf(Status.SUCCESS.ordinal()).equals(ebikeRefund.getStatus())); - if (result.isSuccess()){ + if (result.isSuccess()) { result.setMessage(String.format("退款%s成功", ebikeRefund.getRefundId())); return result; } QueryByOutRefundNoRequest request = new QueryByOutRefundNoRequest(); request.setOutRefundNo(outRefundNo); Refund r = wxRefundService.queryByOutRefundNo(request); - if(r!=null&& r.getStatus().equals(Status.SUCCESS)) { + if (r != null && r.getStatus().equals(Status.SUCCESS)) { result.setMessage(String.format("退款%s成功", ebikeRefund.getRefundId())); result.setSuccess(true); // 更新退款状态 @@ -495,7 +496,7 @@ public class WxPayServiceImpl implements WxPayService { .nonce(nonce) .build(); // 验签并解析通知 - NotificationParser parser = new NotificationParser((NotificationConfig)certificateConfig); + NotificationParser parser = new NotificationParser((NotificationConfig) certificateConfig); // 解析解密后的通知 Refund refund = parser.parse(requestParam, Refund.class); // 更新状态 @@ -504,7 +505,7 @@ public class WxPayServiceImpl implements WxPayService { result.setSuccess(true); result.setMessage("success"); return result; - }catch (Exception e) { + } catch (Exception e) { result.setMessage(String.format("verify failed, request-id=[%s]", requestId)); logError("退款通知回调handleRefundNotify", e); return result; @@ -526,22 +527,18 @@ public class WxPayServiceImpl implements WxPayService { * 打印日志 * * @param desc 描述 - * @param e 异常 + * @param e 异常 */ private void logError(String desc, Exception e) { if (e instanceof HttpException httpException) { log.error("{} 发送HTTP请求失败, {}", desc, httpException.getHttpRequest()); - } - else if (e instanceof ServiceException serviceException) { + } else if (e instanceof ServiceException serviceException) { log.error("{} 服务返回状态不正常, {}", desc, serviceException.getResponseBody()); - } - else if (e instanceof MalformedMessageException malformedMessageException) { + } else if (e instanceof MalformedMessageException malformedMessageException) { log.error("{} 返回体类型不合法或者解析返回体失败, {}", desc, malformedMessageException.getMessage()); - } - else if (e instanceof ValidationException validationException) { + } else if (e instanceof ValidationException validationException) { log.error("{} 验签失败,验证签名失败,{}", desc, validationException.getMessage()); - } - else { + } else { log.error("{} 执行异常, {}", desc, e.getMessage()); }