# 取消运单

[调试诊断](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_tools)

> 接口应在服务器端调用，不可在前端（小程序、网页、APP等）直接调用，具体可参考[接口调用指南](/doc/oplatform/developers/dev/guide.md)。

接口英文名：cancelOrder

该接口用于取消运单。

## 1. 调用方式

### HTTPS 调用

```bash
POST https://api.weixin.qq.com/cgi-bin/express/business/order/cancel?access_token=ACCESS_TOKEN
```

### 云调用

- 调用方法：logistics.cancelOrder

- 出入参和 HTTPS 调用相同，调用方式可查看 [云调用](/doc/oplatform/developers/dev/cloudCall.md) 说明文档。

### 第三方调用

- 本接口支持第三方平台代商家调用。

- 该接口所属的权限集 id 为：45、71

- 服务商获得其中之一权限集授权后，可通过使用 [authorizer_access_token](/doc/oplatform/developers/dev/AuthorizerAccessToken.md) 代商家进行调用，具体可查看 [第三方调用](/doc/oplatform/Third-party_Platforms/2.0/Before_Develop/call_interface.md) 说明文档。

## 2. 请求参数

### 查询参数 `Query String Parameters`

```json
[
  {
    "参数名": "access_token",
    "类型": "string",
    "必填": "是",
    "说明": "接口调用凭证，可使用 [access_token](/miniprogram/dev/server/API/mp-access-token/api_getaccesstoken)、[authorizer_access_token](/doc/oplatform/openApi/ticket-token/api_getauthorizeraccesstoken.md)"
  }
]
```

### 请求体 `Request Payload`

```json
[
  {
    "参数名": "openid",
    "类型": "string",
    "必填": "否",
    "说明": "用户openid，当add_source=2时无需填写（不发送物流服务通知）"
  },
  {
    "参数名": "delivery_id",
    "类型": "string",
    "必填": "是",
    "说明": "快递公司ID，参见[getAllDelivery](api_getalldelivery)"
  },
  {
    "参数名": "waybill_id",
    "类型": "string",
    "必填": "是",
    "说明": "运单ID"
  },
  {
    "参数名": "order_id",
    "类型": "string",
    "必填": "是",
    "说明": "订单 ID，需保证全局唯一"
  }
]
```

## 3. 返回参数

### 返回体 `Response Payload`

```json
[
  {
    "参数名": "errcode",
    "类型": "number",
    "说明": "错误码"
  },
  {
    "参数名": "errmsg",
    "类型": "string",
    "说明": "错误信息"
  },
  {
    "参数名": "delivery_resultcode",
    "类型": "number",
    "说明": "运力返回的错误码"
  },
  {
    "参数名": "delivery_resultmsg",
    "类型": "string",
    "说明": "运力返回的错误信息"
  }
]
```

## 4. 注意事项

本接口无特殊注意事项

## 5. 代码示例

### 5.1 HTTPS调用

请求示例

```json
{
  "order_id": "01234567890123456789",
  "openid": "oABC123456",
  "delivery_id": "SF",
  "waybill_id": "123456789"
}
```

返回示例

```json
{
  "errcode": 0,
  "errmsg": "ok",
  "delivery_resultcode": 0,
  "delivery_resultmsg": ""
}
```

### 5.2 云函数调用

请求示例

```json
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.logistics.cancelOrder({
        "openid": 'oABC123456',
        "orderId": '01234567890123456789',
        "deliveryId": 'SF',
        "waybillId": '123456789'
      })
    return result
  } catch (err) {
    return err
  }
}
```

返回示例

```json
{
  "errCode": 0,
  "errMsg": "openapi.logistics.cancelOrder:ok",
  "deliveryResultcode": 0,
  "deliveryResultmsg": ""
}
```

## 6. 错误码

以下是本接口的错误码列表，其他错误码可参考 [通用错误码](/doc/oplatform/developers/errCode/README.md)；调用接口遇到报错，可使用官方提供的 [API 诊断工具](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_errcode) 辅助定位和分析问题。

```json
[
  {
    "错误码": "40001",
    "错误描述": "invalid credential  access_token isinvalid or not latest",
    "解决方案": "获取 access_token 时 AppSecret 错误，或者 access_token 无效。请开发者认真比对 AppSecret 的正确性，或查看是否正在为恰当的公众号调用接口"
  }
]
```

## 7. 适用范围

本接口支持「小程序（仅认证）」账号类型调用。其他账号类型如无特殊说明，均不可调用。
