# 取消订阅合同

> 立即、当前计费周期结束或指定日期取消一份订阅合同。

```yaml
openapi: 3.1.0
info:
  title: 取消订阅合同
  version: 1.0.0
  description: 立即、当前计费周期结束或指定日期取消一份订阅合同。
paths:
  /v1/txn/sub/cancel:
    post:
      summary: 取消订阅合同
      description: 立即、当前计费周期结束或指定日期取消一份订阅合同。
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                merchantNo:
                  type: string
                  description: Onerway 分配的商户号；获取方式参见[接入准备](/zh/payments/get-started/setup#获取凭证)。
                contractId:
                  type: string
                  description: 要取消的订阅合约号，通常来自初始订阅创建后的响应、Webhook，或通过[查询订阅信息接口](/zh/payments/api-reference/endpoints/query-subscription-details)获取。
                expireMode:
                  type: string
                  description: 订阅取消的生效方式；不传且未提供 `expireDate` 时，订阅将在当前计费周期结束时取消。
                  enum:
                    - "1"
                    - "2"
                    - "3"
                  x-enum-descriptions:
                    "1": 立即取消。立即触发订阅取消通知；托管订阅还会向客户发送邮件通知。
                    "2": 当前计费周期结束时取消。
                    "3": 指定日期取消；必须同时传入 `expireDate`，取消生效时才发送订阅取消通知。
                expireDate:
                  type: string
                  description: 订阅取消生效日期，格式 `yyyy-MM-dd`。
                  x-onerway-required: conditional
                  x-onerway-condition:
                    - 当 `expireMode=3`，即指定日期取消时必填。
                sign:
                  type: string
                  description: 请求签名字符串；生成方式详见[请求签名](/zh/payments/get-started/request-signing)。
              required:
                - merchantNo
                - contractId
                - sign
            examples:
              cancel-subscription-immediately:
                summary: 立即取消订阅
                value:
                  contractId: sub_contract_demo_202606210001
                  expireMode: "1"
                  merchantNo: replace_with_merchant_no
                  sign: "{{SIGN}}"
      responses:
        "200":
          description: Response
          content:
            application/json:
              schema:
                type: object
                properties:
                  respCode:
                    type: string
                    description: 响应码；`20000`
                      表示请求处理成功，其余为错误码。完整码表见[响应码](/zh/payments/api-reference/response-codes)。
                  respMsg:
                    type: string
                    description: 响应码对应的可读消息。
                  data:
                    type: string
                    description: 取消后的订阅合约状态；本接口成功取消示例返回 `3`。
                    enum:
                      - "0"
                      - "1"
                      - "2"
                      - "3"
                    x-enum-descriptions:
                      "0": 处理中。
                      "1": 生效。
                      "2": 不生效。
                      "3": 被取消。
```
