Commit 258b4c3d60cb2ed0ff927e5df4e6ad5af75fbf7e

Authored by YunaiV
1 parent 76477bbb

fix:【system】解决支付回调与订单信息上传之间的时间差导致 uploadWxaOrderShippingInfo 报错

yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java
... ... @@ -102,6 +102,19 @@ public class SocialClientServiceImpl implements SocialClientService {
102 102 @Value("${yudao.wxa-subscribe-message.miniprogram-state:formal}")
103 103 public String miniprogramState;
104 104  
  105 + /**
  106 + * 上传发货信息重试次数
  107 + */
  108 + private static final int UPLOAD_SHIPPING_INFO_MAX_RETRIES = 5;
  109 + /**
  110 + * 上传发货信息重试间隔
  111 + */
  112 + private static final Duration UPLOAD_SHIPPING_INFO_RETRY_INTERVAL = Duration.ofMillis(500L);
  113 + /**
  114 + * 微信错误码:支付单不存在
  115 + */
  116 + private static final int WX_ERR_CODE_PAY_ORDER_NOT_EXIST = 10060001;
  117 +
105 118 @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
106 119 @Autowired(required = false) // 由于 justauth.enable 配置项,可以关闭 AuthRequestFactory 的功能,所以这里只能不强制注入
107 120 private AuthRequestFactory authRequestFactory;
... ... @@ -368,16 +381,34 @@ public class SocialClientServiceImpl implements SocialClientService {
368 381 .payer(PayerBean.builder().openid(reqDTO.getOpenid()).build())
369 382 .uploadTime(ZonedDateTime.now().format(UTC_MS_WITH_XXX_OFFSET_FORMATTER))
370 383 .build();
371   - try {
372   - WxMaOrderShippingInfoBaseResponse response = service.getWxMaOrderShippingService().upload(request);
373   - if (response.getErrCode() != 0) {
  384 + // 重试机制:解决支付回调与订单信息上传之间的时间差导致的 10060001 错误
  385 + // 对应 ISSUE:https://gitee.com/zhijiantianya/yudao-cloud/pulls/230
  386 + for (int attempt = 1; attempt <= UPLOAD_SHIPPING_INFO_MAX_RETRIES; attempt++) {
  387 + try {
  388 + WxMaOrderShippingInfoBaseResponse response = service.getWxMaOrderShippingService().upload(request);
  389 + // 成功,直接返回
  390 + if (response.getErrCode() == 0) {
  391 + log.info("[uploadWxaOrderShippingInfo][上传微信小程序发货信息成功:request({}) response({})]", request, response);
  392 + return;
  393 + }
  394 + // 如果是 10060001 错误(支付单不存在)且还有重试次数,则等待后重试
  395 + if (response.getErrCode() == WX_ERR_CODE_PAY_ORDER_NOT_EXIST && attempt < UPLOAD_SHIPPING_INFO_MAX_RETRIES) {
  396 + log.warn("[uploadWxaOrderShippingInfo][第 {} 次尝试失败,支付单不存在,{} 后重试:request({}) response({})]",
  397 + attempt, UPLOAD_SHIPPING_INFO_RETRY_INTERVAL, request, response);
  398 + Thread.sleep(UPLOAD_SHIPPING_INFO_RETRY_INTERVAL.toMillis());
  399 + continue;
  400 + }
  401 + // 其他错误或重试次数用尽,抛出异常
374 402 log.error("[uploadWxaOrderShippingInfo][上传微信小程序发货信息失败:request({}) response({})]", request, response);
375 403 throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_ORDER_UPLOAD_SHIPPING_INFO_ERROR, response.getErrMsg());
  404 + } catch (WxErrorException ex) {
  405 + log.error("[uploadWxaOrderShippingInfo][上传微信小程序发货信息失败:request({})]", request, ex);
  406 + throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_ORDER_UPLOAD_SHIPPING_INFO_ERROR, ex.getError().getErrorMsg());
  407 + } catch (InterruptedException ex) {
  408 + Thread.currentThread().interrupt();
  409 + log.error("[uploadWxaOrderShippingInfo][重试等待被中断:request({})]", request, ex);
  410 + throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_ORDER_UPLOAD_SHIPPING_INFO_ERROR, "重试等待被中断");
376 411 }
377   - log.info("[uploadWxaOrderShippingInfo][上传微信小程序发货信息成功:request({}) response({})]", request, response);
378   - } catch (WxErrorException ex) {
379   - log.error("[uploadWxaOrderShippingInfo][上传微信小程序发货信息失败:request({})]", request, ex);
380   - throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_ORDER_UPLOAD_SHIPPING_INFO_ERROR, ex.getError().getErrorMsg());
381 412 }
382 413 }
383 414  
... ...