收藏
回答

当微信支付完成回调中执行上传发货信息代码报错

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug 上传发货信息 工具 - -

文档

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);

}


最后一次编辑于  2023-06-13
回答关注问题邀请回答
收藏

1 个回答

  • Memory (私信不回复)
    Memory (私信不回复)
    2023-06-13

    你换个支付方式再试试

    2023-06-13
    有用 1
    回复 3
    • 悟空
      悟空
      2023-06-13
      支付方式吗? 是微信支付
      2023-06-13
      回复
    • 悟空
      悟空
      2023-06-13
      有点怀疑是wx内部的情况,同步执行上传发货信息一定能成功。开启异步若线程不休眠等待一段时间则一定失败,测试休眠500ms一定能成功。 微信支付回调的时候已经告诉微信单号了,但是却找不到单号,可能是内部主从数据同步的问题吧。
      2023-06-13
      回复
    • Memory (私信不回复)
      Memory (私信不回复)
      2023-06-13回复悟空
      有些支付方式不支持上传发货,比如云闪付支付的
      2023-06-13
      回复
登录 后发表内容