From 4afc12d84244a094a7f9b57d9eb9ba4a318aae8b Mon Sep 17 00:00:00 2001 From: 18650502300 <18650502300@163.com> Date: Sun, 15 Sep 2024 15:26:33 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=94=AF=E4=BB=98=E6=B8=A0=E9=81=93--?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/CallbackController.java | 3 +- .../controller/system/SysLoginController.java | 3 + .../com/ruoyi/common/enums/BusinessType.java | 2 + .../com/ruoyi/common/pay/PaymentResult.java | 15 ++--- .../common/pay/syb/service/SybPayService.java | 2 +- .../java/com/ruoyi/common/pay/wx/Payable.java | 5 ++ .../java/com/ruoyi/system/domain/Channel.java | 3 + .../java/com/ruoyi/system/domain/EtOrder.java | 4 ++ .../service/impl/EtOrderServiceImpl.java | 63 +++++++++++++------ .../system/service/impl/WxPayService.java | 40 +++++++----- .../mapper/system/EtChannelMapper.xml | 4 +- .../resources/mapper/system/EtOrderMapper.xml | 4 +- 12 files changed, 98 insertions(+), 50 deletions(-) diff --git a/electripper-admin/src/main/java/com/ruoyi/web/controller/common/CallbackController.java b/electripper-admin/src/main/java/com/ruoyi/web/controller/common/CallbackController.java index 1f92247..6a08936 100644 --- a/electripper-admin/src/main/java/com/ruoyi/web/controller/common/CallbackController.java +++ b/electripper-admin/src/main/java/com/ruoyi/web/controller/common/CallbackController.java @@ -103,8 +103,9 @@ public class CallbackController { String trxCode = params.get("trxcode"); // 交易类型 String outTradeNo = params.get("cusorderid"); // 商户自定义订单号 String trxStatus = params.get("trxstatus"); // 交易结果 - String remark = params.get("remark"); // 附加信息,用于判断业务类型 + String remark = params.get("trxreserved"); // 附加信息,用于判断业务类型 AttachVo attachVo = JSONObject.parseObject(remark,AttachVo.class); + log.info("【通联微信支付回调】回调参数--附加信息 : 【{}】",JSON.toJSONString(attachVo)); if (SybTrxStatus.isSuccess(trxStatus) && SybTrxCode.WX_PAY.equalsCode(trxCode)) { // 新版支付订单 diff --git a/electripper-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java b/electripper-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java index 1a948ec..bc2ca75 100644 --- a/electripper-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java +++ b/electripper-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java @@ -3,6 +3,7 @@ package com.ruoyi.web.controller.system; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson2.JSON; +import com.ruoyi.common.annotation.Log; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.HttpStatus; import com.ruoyi.common.core.domain.AjaxResult; @@ -11,6 +12,7 @@ import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.domain.entity.SysMenu; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.LoginBody; +import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.framework.web.service.SysLoginService; import com.ruoyi.framework.web.service.SysPermissionService; @@ -159,6 +161,7 @@ public class SysLoginController * 微信登录 */ @PostMapping("/wxlogin") + @Log(title = "微信登录", businessType = BusinessType.LOGIN) public AjaxResult wxlogin(String mobileCode,String jsCode,String areaId) { if(StrUtil.isBlank(areaId)){ log.info("没有传入areaId参数:【areaId={}】",areaId); diff --git a/electripper-common/src/main/java/com/ruoyi/common/enums/BusinessType.java b/electripper-common/src/main/java/com/ruoyi/common/enums/BusinessType.java index b872587..5a6c90a 100644 --- a/electripper-common/src/main/java/com/ruoyi/common/enums/BusinessType.java +++ b/electripper-common/src/main/java/com/ruoyi/common/enums/BusinessType.java @@ -216,4 +216,6 @@ public enum BusinessType * 0元订单 */ ZEROORDER, + + LOGIN, } diff --git a/electripper-common/src/main/java/com/ruoyi/common/pay/PaymentResult.java b/electripper-common/src/main/java/com/ruoyi/common/pay/PaymentResult.java index 531cf4f..06da388 100644 --- a/electripper-common/src/main/java/com/ruoyi/common/pay/PaymentResult.java +++ b/electripper-common/src/main/java/com/ruoyi/common/pay/PaymentResult.java @@ -1,21 +1,14 @@ package com.ruoyi.common.pay; +import com.wechat.pay.java.service.payments.model.Transaction; import lombok.Data; -import java.math.BigDecimal; +import java.util.Map; @Data public class PaymentResult { - private String outTradeNo; // 商户订单号 - private String status; - private String transactionId; // 平台交易ID - private BigDecimal amount; // 交易金额 + private Transaction transaction; - public PaymentResult(String outTradeNo, String status, String transactionId, BigDecimal amount) { - this.outTradeNo = outTradeNo; - this.status = status; - this.transactionId = transactionId; - this.amount = amount; - } + private Map result; } diff --git a/electripper-common/src/main/java/com/ruoyi/common/pay/syb/service/SybPayService.java b/electripper-common/src/main/java/com/ruoyi/common/pay/syb/service/SybPayService.java index 684ed6f..819a756 100644 --- a/electripper-common/src/main/java/com/ruoyi/common/pay/syb/service/SybPayService.java +++ b/electripper-common/src/main/java/com/ruoyi/common/pay/syb/service/SybPayService.java @@ -45,7 +45,7 @@ public class SybPayService { "", "", "", - "", + payable.getAppid(), "", "", "", diff --git a/electripper-common/src/main/java/com/ruoyi/common/pay/wx/Payable.java b/electripper-common/src/main/java/com/ruoyi/common/pay/wx/Payable.java index 779de31..b70cd86 100644 --- a/electripper-common/src/main/java/com/ruoyi/common/pay/wx/Payable.java +++ b/electripper-common/src/main/java/com/ruoyi/common/pay/wx/Payable.java @@ -34,4 +34,9 @@ public class Payable { */ private String openid; + /** + * appid + */ + private String appid; + } diff --git a/electripper-system/src/main/java/com/ruoyi/system/domain/Channel.java b/electripper-system/src/main/java/com/ruoyi/system/domain/Channel.java index 21fe400..2ad85f6 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/domain/Channel.java +++ b/electripper-system/src/main/java/com/ruoyi/system/domain/Channel.java @@ -61,4 +61,7 @@ public class Channel extends BaseEntity /** 退款回调地址 */ private String refundNotifyUrl; + /** appid */ + private String appid; + } diff --git a/electripper-system/src/main/java/com/ruoyi/system/domain/EtOrder.java b/electripper-system/src/main/java/com/ruoyi/system/domain/EtOrder.java index d610683..4919184 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/domain/EtOrder.java +++ b/electripper-system/src/main/java/com/ruoyi/system/domain/EtOrder.java @@ -246,6 +246,10 @@ public class EtOrder extends BaseEntity @Excel(name = "是否押金抵扣") private String depositDeduction; + /** 押金订单号 */ + @Excel(name = "押金订单号") + private String depositOrderNo; + /** 是否开启押金抵扣:0-否;1-是 */ @Excel(name = "是否开启押金抵扣") @TableField(exist = false) diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOrderServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOrderServiceImpl.java index b991725..84699e9 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOrderServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOrderServiceImpl.java @@ -13,6 +13,8 @@ import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.enums.BusinessStatus; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.pay.PaymentResult; +import com.ruoyi.common.pay.syb.enums.SybTrxStatus; +import com.ruoyi.common.pay.syb.service.SybPayService; import com.ruoyi.common.utils.CommonUtil; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.PageUtils; @@ -25,7 +27,6 @@ import com.ruoyi.system.mapper.*; import com.ruoyi.system.service.*; import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse; import com.wechat.pay.java.service.payments.model.Transaction; -import com.wechat.pay.java.service.refund.model.Refund; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -132,6 +133,9 @@ public class EtOrderServiceImpl implements IEtOrderService @Autowired private EtChannelService etChannelService; + @Autowired + private SybPayService sybPayService; + /** * 查询订单 * @@ -335,7 +339,13 @@ public class EtOrderServiceImpl implements IEtOrderService List etOrders = etOrderMapper.selectEtOrderList(etOrder); //如果查询押金则增加退款记录 etOrders.forEach(etOrder1 -> { - List refunds = etRefundService.selectEtRefundByOrderNo(etOrder1.getOrderNo()); + String orderNo; + if(etOrder1.getDepositDeduction().equals(ServiceConstants.IS_DEPOSIT_DEDUCTION) && etOrder1.getStatus().equals(ServiceConstants.ORDER_STATUS_ORDER_END)){ + orderNo = etOrder1.getDepositOrderNo(); + }else{ + orderNo = etOrder1.getOrderNo(); + } + List refunds = etRefundService.selectEtRefundByOrderNo(orderNo); if(ObjectUtil.isNotNull(refunds) && refunds.size() > 0){ etOrder1.setEtRefund(refunds.get(0)); etOrder1.setEtRefunds(refunds); @@ -523,27 +533,29 @@ public class EtOrderServiceImpl implements IEtOrderService if(StrUtil.isNotBlank(order.getOutTradeNo())){ String outTradeNo = order.getOutTradeNo(); log.info("【押金抵扣】订单【{}】,有outTradeNo = 【{}】", order.getOrderNo(),outTradeNo); + ChannelVO channelVO = etChannelService.selectSmChannelByChannelId(order.getPayChannel()); // 如果原来有outtradeno,去查询一次,查询是否支付过 PaymentResult paymentResult = wxPayService.queryOrderByOutTradeNo(outTradeNo); try { - if (Transaction.TradeStateEnum.SUCCESS.equals(paymentResult.getStatus())) { - // 订单已支付 - order.setPaid(ServiceConstants.ORDER_PAY_STATUS_PAID); - order.setPayTime(DateUtils.getNowDate()); - order.setStatus(ServiceConstants.ORDER_STATUS_ORDER_END); - order.setPayType(ServiceConstants.PAY_TYPE_WX); - order.setMark("主动查询-骑行支付"); - int updateEtOrder = etOrderMapper.updateEtOrderByOrderNo(order); - if (updateEtOrder == 0) { - // 抛出自定义异常来标识订单更新失败 - throw new ServiceException("押金抵扣失败,更新骑行订单失败"); + if (ObjectUtil.isNotNull(paymentResult.getTransaction())) { + if (Transaction.TradeStateEnum.SUCCESS.equals(paymentResult.getTransaction().getTradeState())) { + handleSuccess(order); + return 1; + }else{ + // 没有支付,则关闭订单 + boolean b = wxPayService.closeOrder(outTradeNo,channelVO); + log.info("【押金抵扣】订单【{}】,有outTradeNo = 【{}】,查询订单未支付,关闭订单:{}", order.getOrderNo(),outTradeNo,b); } - return 1; }else{ - ChannelVO channelVO = etChannelService.selectSmChannelByChannelId(order.getPayChannel()); - // 没有支付,则关闭订单 - boolean b = wxPayService.closeOrder(outTradeNo,channelVO); - log.info("【押金抵扣】订单【{}】,有outTradeNo = 【{}】,查询订单未支付,关闭订单:{}", order.getOrderNo(),outTradeNo,b); + Map result = paymentResult.getResult(); + if(SybTrxStatus.isSuccess(result.get("trxstatus"))) { + handleSuccess(order); + return 1; + }else{ + // 没有支付,则关闭订单 + sybPayService.closeOrderWx(outTradeNo); + log.info("【押金抵扣】订单【{}】,有outTradeNo = 【{}】,查询订单未支付,关闭订单:{}", order.getOrderNo(),outTradeNo); + } } } catch (ServiceException e) { log.error("异常发生: {}", e.getMessage()); @@ -573,6 +585,7 @@ public class EtOrderServiceImpl implements IEtOrderService order.setPayType(ServiceConstants.PAY_TYPE_YJ); order.setMark(mark); order.setDepositDeduction(ServiceConstants.IS_DEPOSIT_DEDUCTION); + order.setDepositOrderNo(depositOrder.getOrderNo()); int updateEtOrder = etOrderMapper.updateEtOrder(order); if(updateEtOrder == 0){ throw new ServiceException("押金抵扣失败,更新骑行订单失败"); @@ -613,6 +626,20 @@ public class EtOrderServiceImpl implements IEtOrderService return 1; } + private void handleSuccess(EtOrder order) { + // 订单已支付 + order.setPaid(ServiceConstants.ORDER_PAY_STATUS_PAID); + order.setPayTime(DateUtils.getNowDate()); + order.setStatus(ServiceConstants.ORDER_STATUS_ORDER_END); + order.setPayType(ServiceConstants.PAY_TYPE_WX); + order.setMark("主动查询-骑行支付"); + int updateEtOrder = etOrderMapper.updateEtOrderByOrderNo(order); + if (updateEtOrder == 0) { + // 抛出自定义异常来标识订单更新失败 + throw new ServiceException("押金抵扣失败,更新骑行订单失败"); + } + } + /** * 修复押金抵扣 * 1. 查询所有押金抵扣的订单 diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/WxPayService.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/WxPayService.java index 9e71f1d..35cde1d 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/WxPayService.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/WxPayService.java @@ -113,20 +113,24 @@ public class WxPayService implements IWxPayService { if(ObjectUtil.isNull(sysDept)){ throw new ServiceException("没有运营商:【"+etAreaDept.getDeptId()+"】"); } - String type = order.getType(); - String description = type.equals(ServiceConstants.ORDER_TYPE_RIDING) ? "骑行订单-"+billNo : "押金充值-"+billNo; - - String outTradeNo = IdUtils.getOrderNo("wx"); - EtOrder order1 = new EtOrder(); - order1.setOutTradeNo(outTradeNo); - order1.setOrderId(order.getOrderId()); Long payChannel = sysDept.getPayChannel(); if(ObjectUtil.isNull(payChannel)){ throw new ServiceException("运营商【"+sysDept.getDeptName()+"】没有支付渠道"); } - ChannelVO channelVO = etChannelService.selectSmChannelByChannelId(payChannel); + String outTradeNo = null; + if(PayChannel.CT_WX.equalsCode(channelVO.getCode()) || PayChannel.YS_WX.equalsCode(channelVO.getCode())){ + outTradeNo = IdUtils.getOrderNo("wx"); + }else if(PayChannel.TL_WX.equalsCode(channelVO.getCode())){ + outTradeNo = IdUtils.getOrderNo("tlwx"); + } + String type = order.getType(); + String description = type.equals(ServiceConstants.ORDER_TYPE_RIDING) ? "骑行订单-"+billNo : "押金充值-"+billNo; + + EtOrder order1 = new EtOrder(); + order1.setOutTradeNo(outTradeNo); + order1.setOrderId(order.getOrderId()); order1.setPayChannel(payChannel); int updateEtOrder = etOrderService.updateEtOrder(order1); @@ -143,6 +147,7 @@ public class WxPayService implements IWxPayService { request.setMchid(channelVO.getMerchantId()); request.setAttach(JSON.toJSONString(new AttachVo(payType,user.getUserId(), ""))); request.setDescription(description); + log.info("支付渠道------"+JSON.toJSON(channelVO)); request.setNotifyUrl(channelVO.getNotifyUrl()); request.setPayer(getPayer(user.getWxopenid())); JsapiServiceExtension jsapiServiceExtension = getJsapiServiceExtension(channelVO); @@ -165,11 +170,12 @@ public class WxPayService implements IWxPayService { sybPayService.closeOrderWx(order.getOutTradeNo()); } Payable payable = new Payable(); - payable.setAmount(order.getPayFee().longValue()); + payable.setAmount(order.getPayFee().multiply(new BigDecimal(100)).longValue()); payable.setOutTradeNo(outTradeNo); payable.setAttach(JSON.toJSONString(new AttachVo(payType,user.getUserId(), ""))); payable.setDescription(description); payable.setOpenid(user.getWxopenid()); + payable.setAppid(channelVO.getAppid()); PrepayWithRequestPaymentResponse res = sybPayService.prepayWxApp(payable); return res; }else{ @@ -297,22 +303,21 @@ public class WxPayService implements IWxPayService { public PaymentResult queryOrderByOutTradeNo(String outTradeNo) { EtOrder order = etOrderService.selectEtOrderByOutTradeNo(outTradeNo); ChannelVO channelVO = etChannelService.selectSmChannelByChannelId(order.getPayChannel()); + PaymentResult paymentResult1 = new PaymentResult(); if(PayChannel.CT_WX.equalsCode(channelVO.getCode()) || PayChannel.YS_WX.equalsCode(channelVO.getCode())){ QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest(); request.setMchid(channelVO.getMerchantId()); request.setOutTradeNo(outTradeNo); JsapiService jsapiService = getJsapiService(channelVO); Transaction transaction = jsapiService.queryOrderByOutTradeNo(request); - return new PaymentResult(transaction.getOutTradeNo(), transaction.getTradeState().name(), - transaction.getTransactionId(), new BigDecimal(transaction.getAmount().getTotal()/100)); + log.info("微信查询订单信息outTradeNo={}-----【{}】",outTradeNo,JSON.toJSON(transaction)); + paymentResult1.setTransaction(transaction); + return paymentResult1; }else if(PayChannel.TL_WX.equalsCode(channelVO.getCode())){ Map result = sybPayService.queryOrderByOutTradeNo(order.getOutTradeNo()); if(SybTrxStatus.isSuccess(result.get("trxstatus"))) { - PaymentResult paymentResult = new PaymentResult(result.get("reqsn"), - result.get("trxstatus"), - result.get("chnltrxid"), - new BigDecimal(result.get("amount")).divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)); - return paymentResult; + paymentResult1.setResult(result); + return paymentResult1; } return null; }else{ @@ -418,6 +423,9 @@ public class WxPayService implements IWxPayService { public void refund(EtOrder etOrder,String reason,BigDecimal amount,String outRefundNo) { if(!etOrder.getStatus().equals(ServiceConstants.ORDER_STATUS_ORDER_END)) throw new ServiceException("订单状态异常"); ChannelVO channelVO = etChannelService.selectSmChannelByChannelId(etOrder.getPayChannel()); + if (channelVO == null) { + throw new ServiceException("支付渠道不存在: " + etOrder.getPayChannel()); + } if(PayChannel.CT_WX.equalsCode(channelVO.getCode()) || PayChannel.YS_WX.equalsCode(channelVO.getCode())){ log.info("----------{}-------------","微信官方退款"); diff --git a/electripper-system/src/main/resources/mapper/system/EtChannelMapper.xml b/electripper-system/src/main/resources/mapper/system/EtChannelMapper.xml index a6d79d2..1fdad4d 100644 --- a/electripper-system/src/main/resources/mapper/system/EtChannelMapper.xml +++ b/electripper-system/src/main/resources/mapper/system/EtChannelMapper.xml @@ -21,7 +21,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" sc.notify_url, sc.private_key_path, sc.merchant_serial_number, - sc.refund_notify_url + sc.refund_notify_url, + sc.appid from et_channel sc @@ -82,6 +83,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" private_key_path = #{data.privateKeyPath}, merchant_serial_number = #{data.merchantSerialNumber}, refund_notify_url = #{data.refundNotifyUrl}, + appid = #{data.appid}, where channel_id = #{data.channelId} diff --git a/electripper-system/src/main/resources/mapper/system/EtOrderMapper.xml b/electripper-system/src/main/resources/mapper/system/EtOrderMapper.xml index 1a4bfab..ddb054c 100644 --- a/electripper-system/src/main/resources/mapper/system/EtOrderMapper.xml +++ b/electripper-system/src/main/resources/mapper/system/EtOrderMapper.xml @@ -70,7 +70,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" create_time, appointment_start_time, appointment_end_time,appointment_timeout, unlock_time,return_time, rule_end_time, return_type, AsText(trip_route) trip_route,trip_route_str,cycle,deposit_deduction,video_url, upload_time,deduction_amount,audio_files,used_sn,change_reason,locking,auto_refund_deposit,free_ride_time, - rental_unit,riding_rule,riding_rule_json,charging_cycle,charging_cycle_value,capped_amount,handling_charge, platform_service_fee, pay_channel + rental_unit,riding_rule,riding_rule_json,charging_cycle,charging_cycle_value,capped_amount,handling_charge, platform_service_fee, pay_channel, operator_dividend from et_order @@ -81,7 +81,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" create_time, appointment_start_time, appointment_end_time,appointment_timeout, unlock_time,return_time, rule_end_time, return_type, cycle,deposit_deduction,video_url,upload_time,deduction_amount,audio_files, used_sn,change_reason,locking,auto_refund_deposit,free_ride_time, - rental_unit,riding_rule,riding_rule_json,charging_cycle,charging_cycle_value,capped_amount,handling_charge, platform_service_fee, pay_channel + rental_unit,riding_rule,riding_rule_json,charging_cycle,charging_cycle_value,capped_amount,handling_charge, platform_service_fee, pay_channel, operator_dividend from et_order