当微信支付完成回调中执行上传发货信息代码报错
文档 https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html 程序执行流程:在微信回调时执行完成业务代码后,马上“异步”调用上传发货信息接口得到如下报错。若将代码改为同步执行则执行成功。 尝试拿日志输出的参数调用postman请求成功。 错误描述:支付单不存在, hint: [bba82ec0-05db-40b8-a3e4-0b83040a169d] rid: 648830f0-73b6be8b-64002b67 日志如下: 上传发货信息参数=>{"delivery_mode":"UNIFIED_DELIVERY","logistics_type":2,"order_key":{"mchid":"*","order_number_type":2,"transaction_id":"4200001880202306138599253500"},"payer":{"openid":"*"},"shipping_list":[{"item_desc":"---"}],"upload_time":"2023-06-13T17:03:44+0800"}
Java部分代码如下: /**
* 线程池
*/
private final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(4, 8, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
/**
* 微信小程序录入发货信息
*/
private final String UPLOAD_SHIPPING_API = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=";
/**
* trace-link的key
*/
private final String TRACE_LINK = "trace_link";
@Override
public PayBusinessScene businessScene() {
return PayBusinessScene.CUSTOMER_MINI_APP_ORDER;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void handleExecute(String orderCode, String outOrderCode, PayAttach attach, Object notify) {
Long orderId = Long.parseLong(attach.getParam());
//标记订单完成
orderService.payDone(new OrderPayDoneCmd().setOrderId(orderId).setOutNo(outOrderCode));
//获取上下文的traceLink
String traceLink = MDC.get(TRACE_LINK);
//上传发货信息
threadPool.execute(() -> {
MDC.put(TRACE_LINK, traceLink);
//上传发货信息
uploadShipping((WxPayOrderNotifyResult) notify);
});
}
/**
* 上传发货信息
*
* @param notify
*/
private void uploadShipping(WxPayOrderNotifyResult notify) {
log.info("开始上传发货信息=>{}", JSONObject.toJSONString(notify));
//确认微信发货
if (!wxMaService.switchover(notify.getAppid())) {
throw CommonException.tips(WxMaTips.APPID_NOT_FOUND, wxMaProperties.getConfigs().get(0).getAppid());
}
try {
String accessToken = wxMaService.getAccessToken();
WxMaUploadShippingInfoCmd cmd = buildUploadShippingInfoCmd(notify);
log.info("上传发货信息参数=>{}", JSONObject.toJSONString(cmd));
String result = HttpUtil.post(UPLOAD_SHIPPING_API + accessToken, JSONObject.toJSONString(cmd));
WxMaUploadShippingResponse response = JSONObject.parseObject(result, WxMaUploadShippingResponse.class);
if (!response.isOk()) {
throw CommonException.message(response.getErrmsg());
}
} catch (Exception e) {
log.error("微信发起发货失败", e);
throw CommonException.message(e.getMessage());
} finally {
//清理ThreadLocal
WxMaConfigHolder.remove();
}
}
/**
* 构建上传发货信息参数
*
* @param result
* @return
*/
private WxMaUploadShippingInfoCmd buildUploadShippingInfoCmd(WxPayOrderNotifyResult result) {
//定义订单信息
WxMaUploadShippingInfoCmd.OrderKey orderKey = new WxMaUploadShippingInfoCmd.OrderKey();
orderKey.setOrder_number_type(2)
.setTransaction_id(result.getTransactionId())
.setMchid(result.getMchId());
//定义商品
List shippingList = new LinkedList<>();
shippingList.add(new WxMaUploadShippingInfoCmd.Shipping().setItem_desc("----"));
//定义客户标识
WxMaUploadShippingInfoCmd.Payer payer = new WxMaUploadShippingInfoCmd.Payer().setOpenid(result.getOpenid());
payer.setOpenid(result.getOpenid());
return new WxMaUploadShippingInfoCmd()
.setOrder_key(orderKey)
.setLogistics_type(2)
.setShipping_list(shippingList)
.setUpload_time(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date()))
.setPayer(payer);
}