fix[支付模块]:订单支付回调逻辑新增,退款逻辑回调逻辑新增完成,测试通过
This commit is contained in:
+1
-6
@@ -3,7 +3,6 @@ package com.central.order.config;
|
||||
import com.wechat.pay.java.core.Config;
|
||||
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||
import com.wechat.pay.java.core.notification.NotificationConfig;
|
||||
import com.wechat.pay.java.core.notification.NotificationParser;
|
||||
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
||||
import com.wechat.pay.java.service.refund.RefundService;
|
||||
import lombok.Getter;
|
||||
@@ -59,7 +58,7 @@ public class TencentPayConfiguration {
|
||||
* 微信支付配置获取
|
||||
* @return
|
||||
*/
|
||||
private Config getConfig() {
|
||||
public Config getConfig() {
|
||||
return new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(merchantId)
|
||||
.privateKeyFromPath(privateKeyPath)
|
||||
@@ -68,10 +67,6 @@ public class TencentPayConfiguration {
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationParser initialNotificationParser() {
|
||||
return new NotificationParser(initialNotificationConfig());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationConfig initialNotificationConfig() {
|
||||
|
||||
+1
-1
@@ -249,7 +249,7 @@ public class OrderController {
|
||||
Assert.notNull(qrCode,"二维码CODEID不能为空");
|
||||
Order order = orderService.getOrderyStatusAndUser(OrderStatus.PAID.getId(), user.getId());
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("price",appendPriceStr(qrCode.getPrice()));
|
||||
map.put("price",Integer.parseInt(qrCode.getPrice()) /100);
|
||||
//如果用户存在已支付订单,则查询当前排在第几
|
||||
if (order != null){
|
||||
Integer count = orderService.getOrderRankingByOrderId(order.getId());
|
||||
|
||||
+45
-15
@@ -1,7 +1,9 @@
|
||||
package com.central.order.controller;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.order.enums.OrderStatus;
|
||||
import com.central.order.enums.RefundState;
|
||||
import com.central.order.model.Order;
|
||||
import com.central.order.service.IOrderService;
|
||||
import com.central.order.service.IRefundService;
|
||||
@@ -10,14 +12,16 @@ import com.wechat.pay.java.service.refund.model.AmountReq;
|
||||
import com.wechat.pay.java.service.refund.model.CreateRequest;
|
||||
import com.wechat.pay.java.service.refund.model.Refund;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/refund")
|
||||
@@ -35,10 +39,46 @@ public class OrderRefundController {
|
||||
@Value("${tencent.refund-notify-url}")
|
||||
private String refundNotifyUrl;
|
||||
|
||||
/**
|
||||
* 列表
|
||||
*/
|
||||
@Operation(summary = "查询全部列表")
|
||||
@Parameters({
|
||||
@Parameter(name = "page",description = "分页起始位置",required = true,in= ParameterIn.QUERY),
|
||||
@Parameter(name = "limit",description = "分页结束位置",required = true,in=ParameterIn.QUERY)
|
||||
})
|
||||
@GetMapping("/list")
|
||||
public PageResult list(@RequestParam Map<String, Object> params) {
|
||||
return refundService.findList(params);
|
||||
}
|
||||
|
||||
@Operation(summary = "创建退款单")
|
||||
@PostMapping("/{orderNo}")
|
||||
public Result<com.central.order.model.Refund> createRefundOrder(@PathVariable String orderNo) {
|
||||
Order order = orderService.getOrderByNumber(orderNo);
|
||||
Assert.notNull(order,"ORDER DATA不存在");
|
||||
if (!order.getOrderStatus().equals(OrderStatus.PAID.getId())){
|
||||
return Result.failed("该订单不支持退款操作");
|
||||
}
|
||||
// 创建退款订单
|
||||
com.central.order.model.Refund refund = new com.central.order.model.Refund();
|
||||
refund.setRefundNo("RF" + order.getOrderNo());
|
||||
refund.setOrderNo(order.getOrderNo());
|
||||
refund.setUserId(order.getUserId());
|
||||
refund.setStatus(OrderStatus.REFUNDING.getId());
|
||||
refund.setState(RefundState.APPROVING.getId());
|
||||
boolean res = refundService.createRefund(refund);
|
||||
if (res) {
|
||||
order.setOrderStatus(OrderStatus.REFUNDING.getId());
|
||||
return Result.succeed("退款成功");
|
||||
}
|
||||
return Result.failed("请重试");
|
||||
}
|
||||
|
||||
@Operation(summary = "微信支付退款")
|
||||
@PostMapping()
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Result<Refund> refundOrder(@RequestBody String orderNo) {
|
||||
public Result<com.central.order.model.Refund> refundOrder(@RequestBody String orderNo) {
|
||||
try {
|
||||
// 检查订单是否存在
|
||||
Order order = orderService.getOrderByNumber(orderNo);
|
||||
@@ -64,17 +104,7 @@ public class OrderRefundController {
|
||||
Refund response = service.create(request);
|
||||
Assert.notNull(response, "退款失败");
|
||||
|
||||
// 创建退款订单
|
||||
com.central.order.model.Refund refund = new com.central.order.model.Refund();
|
||||
refund.setRefundNo("RF" + order.getOrderNo());
|
||||
refund.setOrderNo(order.getOrderNo());
|
||||
refund.setUserId(order.getUserId());
|
||||
refund.setStatus(OrderStatus.REFUNDING.getId());
|
||||
|
||||
boolean res = refundService.createRefund(refund);
|
||||
if (res) {
|
||||
return Result.succeed("退款成功");
|
||||
}
|
||||
throw new RuntimeException("创建退款订单失败");
|
||||
} catch (Exception e) {
|
||||
// 记录异常信息
|
||||
|
||||
+60
-99
@@ -1,32 +1,22 @@
|
||||
package com.central.order.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.central.order.enums.OrderStatus;
|
||||
import com.central.order.model.Order;
|
||||
import com.central.order.service.IOrderService;
|
||||
import com.central.order.service.IRefundService;
|
||||
import com.central.order.util.HttpServletUtils;
|
||||
import com.wechat.pay.java.core.exception.MalformedMessageException;
|
||||
import com.wechat.pay.java.core.exception.ValidationException;
|
||||
import com.wechat.pay.java.core.notification.NotificationConfig;
|
||||
import com.wechat.pay.java.core.notification.NotificationParser;
|
||||
import com.wechat.pay.java.core.notification.RequestParam;
|
||||
import com.wechat.pay.java.service.payments.model.Transaction;
|
||||
import com.wechat.pay.java.service.refund.model.RefundNotification;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Date;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@@ -34,113 +24,81 @@ import java.util.Map;
|
||||
@RequestMapping("/wechat")
|
||||
public class WxPayController {
|
||||
|
||||
@Autowired
|
||||
private NotificationConfig notificationConfig;
|
||||
@Autowired
|
||||
private IOrderService orderService;
|
||||
@Autowired
|
||||
private IRefundService refundService;
|
||||
|
||||
@Autowired
|
||||
private NotificationParser notificationParser;
|
||||
|
||||
private NotificationConfig config;
|
||||
/**
|
||||
* 支付回调
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
@Operation(summary = "支付回调")
|
||||
@PostMapping("/pay/Notify")
|
||||
public String wechatPayNotify(HttpServletRequest request) throws IOException {
|
||||
log.info("------收到微信支付回调通知------");
|
||||
return payNotify(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付回调
|
||||
* <pre>
|
||||
* 注意:
|
||||
* 对后台通知交互时,如果微信收到应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功
|
||||
*
|
||||
* 同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。 推荐的做法是,当商户系统收到通知进行处理时,先检查对应业务数据的状态,并判断该通知是否已经处理。如果未处理,则再进行处理;如果已处理,则直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱。
|
||||
* 如果在所有通知频率后没有收到微信侧回调。商户应调用查询订单接口确认订单状态。
|
||||
* </pre>
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String payNotify(HttpServletRequest request) throws IOException {
|
||||
// 请求头Wechatpay-Signature
|
||||
String signature = request.getHeader("Wechatpay-Signature");
|
||||
// 请求头Wechatpay-nonce
|
||||
String nonce = request.getHeader("Wechatpay-Nonce");
|
||||
// 请求头Wechatpay-Timestamp
|
||||
String timestamp = request.getHeader("Wechatpay-Timestamp");
|
||||
// 微信支付证书序列号
|
||||
String serial = request.getHeader("Wechatpay-Serial");
|
||||
// 签名方式
|
||||
String signType = request.getHeader("Wechatpay-Signature-Type");
|
||||
// 构造 RequestParam
|
||||
RequestParam requestParam = new RequestParam.Builder()
|
||||
.serialNumber(serial)
|
||||
.nonce(nonce)
|
||||
.signature(signature)
|
||||
.timestamp(timestamp)
|
||||
.signType(signType)
|
||||
.body(HttpServletUtils.getRequestBody(request))
|
||||
.build();
|
||||
log.info("微信支付回调body信息:{}", requestParam.getBody());
|
||||
// 初始化 NotificationParser
|
||||
NotificationParser parser = new NotificationParser(notificationConfig);
|
||||
// 以支付通知回调为例,验签、解密并转换成 Transaction
|
||||
log.info("验签参数:{}", JSONObject.toJSONString(requestParam));
|
||||
Map<String, String> returnMap = new HashMap<>(2);
|
||||
returnMap.put("code", "FAIL");
|
||||
returnMap.put("message", "失败");
|
||||
Transaction transaction = null;
|
||||
@PostMapping("/pay/notify")
|
||||
public String handleCallback(@RequestHeader("Wechatpay-Signature") String signature,
|
||||
@RequestHeader("Wechatpay-Timestamp") String timestamp,
|
||||
@RequestHeader("Wechatpay-Nonce") String nonce,
|
||||
@RequestHeader("Wechatpay-Serial") String serial,
|
||||
@RequestBody String body) {
|
||||
try {
|
||||
transaction = parser.parse(requestParam, Transaction.class);
|
||||
} catch (MalformedMessageException e) {
|
||||
log.error("验签失败,解析微信支付应答或回调报文异常,返回信息:", e);
|
||||
return JSONObject.toJSONString(returnMap);
|
||||
} catch (ValidationException e) {
|
||||
log.error("验签失败,验证签名失败,返回信息:", e);
|
||||
return JSONObject.toJSONString(returnMap);
|
||||
// 构建请求参数
|
||||
RequestParam requestParam = new RequestParam
|
||||
.Builder()
|
||||
.body(body)
|
||||
.nonce(nonce)
|
||||
.signature(signature)
|
||||
.serialNumber(serial)
|
||||
.timestamp(timestamp)
|
||||
.build();
|
||||
|
||||
|
||||
NotificationParser notificationParser = new NotificationParser(config);
|
||||
// 解析通知
|
||||
Transaction transaction = notificationParser.parse(requestParam,Transaction.class);
|
||||
|
||||
// 处理支付结果
|
||||
handlePaymentResult(transaction);
|
||||
|
||||
// 返回成功响应
|
||||
return "{\"code\":\"SUCCESS\",\"message\":\"成功\"}";
|
||||
} catch (Exception e) {
|
||||
log.error("验签失败,返回信息:", e);
|
||||
return JSONObject.toJSONString(returnMap);
|
||||
// 处理异常,记录日志等
|
||||
e.printStackTrace();
|
||||
return "{\"code\":\"FAIL\",\"message\":\"失败\"}";
|
||||
}
|
||||
log.info("验签成功!-支付回调结果:{}", transaction.toString());
|
||||
// 判断订单状态
|
||||
if (Transaction.TradeStateEnum.SUCCESS != transaction.getTradeState()) {
|
||||
log.info("内部订单号【{}】,微信支付订单号【{}】支付未成功", transaction.getOutTradeNo(), transaction.getTransactionId());
|
||||
return JSONObject.toJSONString(returnMap);
|
||||
}
|
||||
|
||||
Order order = orderService.getOrderByNumber(transaction.getOutTradeNo());
|
||||
order.setOrderStatus(OrderStatus.PAID.getId());
|
||||
boolean res = orderService.updateOrder(order);
|
||||
|
||||
if (res){
|
||||
returnMap.put("code", "SUCCESS");
|
||||
returnMap.put("message", "成功");
|
||||
|
||||
}else {
|
||||
returnMap.put("code", "FAIL");
|
||||
returnMap.put("message","失败");
|
||||
|
||||
}
|
||||
return JSONObject.toJSONString(returnMap);
|
||||
}
|
||||
|
||||
@PostMapping("/refund/Notify")
|
||||
private void handlePaymentResult(Transaction transaction) {
|
||||
// 获取支付通知中的关键信息
|
||||
String outTradeNo = transaction.getOutTradeNo();
|
||||
String tradeState = transaction.getTradeState().toString();
|
||||
|
||||
// 根据支付状态进行相应处理
|
||||
if ("SUCCESS".equals(tradeState)) {
|
||||
// 支付成功,更新订单状态等操作
|
||||
System.out.println("支付成功,商户订单号:" + outTradeNo);
|
||||
Order order = orderService.getOrderByNumber(transaction.getOutTradeNo());
|
||||
order.setOrderStatus(OrderStatus.PAID.getId());
|
||||
order.setPayTime(new Date());
|
||||
orderService.updateOrder(order);
|
||||
// 这里可以添加更新数据库订单状态的代码
|
||||
} else {
|
||||
// 其他支付状态,如支付中、失败等
|
||||
System.out.println("支付状态:" + tradeState + ",商户订单号:" + outTradeNo);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("/refund/notify")
|
||||
public String refundCallback(@RequestHeader("Wechatpay-Signature") String signature,
|
||||
@RequestHeader("Wechatpay-Timestamp") String timestamp,
|
||||
@RequestHeader("Wechatpay-Nonce") String nonce,
|
||||
@RequestHeader("Wechatpay-Serial") String serial,
|
||||
String body) {
|
||||
@RequestBody String body) {
|
||||
log.info("------收到微信退款回调通知------");
|
||||
try {
|
||||
// 构建请求参数
|
||||
@@ -152,6 +110,8 @@ public class WxPayController {
|
||||
.serialNumber(serial)
|
||||
.timestamp(timestamp)
|
||||
.build();
|
||||
|
||||
NotificationParser notificationParser = new NotificationParser(config);
|
||||
// 解析通知
|
||||
RefundNotification notification = notificationParser.parse(requestParam, RefundNotification.class);
|
||||
|
||||
@@ -182,6 +142,7 @@ public class WxPayController {
|
||||
com.central.order.model.Refund refund = refundService.getRefundByNo(outRefundNo);
|
||||
Assert.notNull(refund,"退款订单数据不存在");
|
||||
refund.setStatus(OrderStatus.REFUNDED.getId());
|
||||
refund.setRefundTime(new Date());
|
||||
refundService.updateById(refund);
|
||||
|
||||
Order order = orderService.getOrderByNumber(refund.getOrderNo());
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.central.order.enums;
|
||||
|
||||
import com.central.common.enums.ZltEnum;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 订单状态枚举类型
|
||||
*
|
||||
* @author lsl create by 2025/1/10
|
||||
*/
|
||||
@Getter
|
||||
public enum RefundState implements ZltEnum {
|
||||
APPROVING(0,"审核中"),
|
||||
APPROVED(1, "已通过"),
|
||||
REFUSED(2,"已拒绝");
|
||||
|
||||
|
||||
RefundState(Integer id, String content) {
|
||||
this.id = id;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
private Integer id;
|
||||
private String content;
|
||||
|
||||
}
|
||||
@@ -56,6 +56,6 @@ public interface OrderMapper extends SuperMapper<Order> {
|
||||
"WHERE \n" +
|
||||
" order_status = 1 \n" +
|
||||
"ORDER BY \n" +
|
||||
" create_time;")
|
||||
" pay_time;")
|
||||
List<OrderRanking> getOrderRanking1();
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public class Order extends SuperEntity {
|
||||
|
||||
private transient String createdTime;
|
||||
|
||||
private transient String codeId;
|
||||
private String codeId;
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Date createTime;
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
|
||||
@@ -31,6 +31,9 @@ public class Refund extends SuperEntity {
|
||||
private String refundReason;
|
||||
private Integer status;
|
||||
|
||||
private Integer state;
|
||||
private String approveReason;
|
||||
|
||||
private Date refundTime;
|
||||
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
|
||||
+1
-7
@@ -21,13 +21,7 @@ public class OrderTimeoutScheduler {
|
||||
orderService.checkAndHandleOrderTimeout();
|
||||
}
|
||||
|
||||
// 每隔一定时间(这里设置为每分钟检查一次,可根据实际情况调整)检查订单超时情况
|
||||
// @Scheduled(fixedRate = 1 * 1000)
|
||||
public void checkOrderPaySuccess() {
|
||||
orderService.updateOrderStatus();
|
||||
}
|
||||
|
||||
// 每隔一定时间(这里设置为每分钟检查一次,可根据实际情况调整)检查订单超时情况
|
||||
// 每隔一定时间(这里设置为每分钟检查一次,可根据实际情况调整)检查是否存在未服务完成的订单
|
||||
// @Scheduled(fixedRate = 60 * 1000)
|
||||
public void processOrdersInQueue() {
|
||||
orderService.processOrdersInQueue();
|
||||
|
||||
-25
@@ -2,26 +2,18 @@ package com.central.order.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.lock.DistributedLock;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import com.central.order.mapper.OrderMapper;
|
||||
import com.central.order.mapper.RefundMapper;
|
||||
import com.central.order.mapper.TourismMapper;
|
||||
import com.central.order.model.Refund;
|
||||
import com.central.order.service.IRefundService;
|
||||
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Order information
|
||||
@@ -34,23 +26,6 @@ import java.util.concurrent.Executors;
|
||||
@Primary
|
||||
public class RefundServiceImpl extends SuperServiceImpl<RefundMapper, Refund> implements IRefundService {
|
||||
|
||||
private final static String ORDER_LOCK = "order_lock_";
|
||||
|
||||
@Autowired
|
||||
private DistributedLock lock;
|
||||
|
||||
private final ExecutorService executorService = Executors.newFixedThreadPool(10); // 线程池,可根据实际并发量调整大小
|
||||
|
||||
@Autowired
|
||||
private JsapiServiceExtension service;
|
||||
|
||||
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Autowired
|
||||
private TourismMapper tourismMapper;
|
||||
|
||||
/**
|
||||
* 列表
|
||||
* @param params
|
||||
|
||||
Reference in New Issue
Block a user