# 取消订阅合同

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

```yaml
openapi: 3.1.0
x-onerway-docs-version: v2
x-onerway-spec-key: payments/versions/v2/subscriptions/cancel-subscription-contract
info:
  title: 取消订阅合同
  version: v2
  description: 立即、当前计费周期结束或指定日期取消一份订阅合同。
servers:
  - url: https://sandbox-payment-api.onerway.com
    description: sandbox
  - url: https://payment-api.onerway.com
    description: production
paths:
  /v1/txn/sub/cancel:
    post:
      summary: 取消订阅合同
      description: 立即、当前计费周期结束或指定日期取消一份订阅合同。
      parameters:
        - name: ApiKey
          in: header
          required: true
          description: 商户账户的 API Key，通过 `ApiKey`
            请求头传入。详见[请求认证](/zh/payments/versions/v2/get-started/authentication)。
          schema:
            type: string
            description: 商户账户的 API Key，通过 `ApiKey`
              请求头传入。详见[请求认证](/zh/payments/versions/v2/get-started/authentication)。
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                merchantNo:
                  type: string
                  description: Onerway
                    分配的商户号；获取方式参见[接入准备](/zh/payments/versions/v2/get-started/setup#获取凭证)。
                contractId:
                  type: string
                  description: 要取消的订阅合约号，通常来自初始订阅创建后的响应、Webhook，或通过[查询订阅信息接口](/zh/payments/versions/v2/api-reference/endpoints/query-subscription-details)获取。
                expireMode:
                  type: string
                  description: 托管订阅（`selfExecute=1`）的取消生效方式；未传 `expireMode` 和 `expireDate`
                    时，预约在当前计费周期结束时取消。自主管理订阅（`selfExecute=2`）取消立即生效。
                  enum:
                    - "1"
                    - "2"
                    - "3"
                  x-enum-descriptions:
                    "1": 立即取消并发送订阅取消通知。托管订阅的客户邮件取决于通知邮箱及邮件配置。
                    "2": 当前计费周期结束时取消。
                    "3": 指定日期取消；必须同时传入 `expireDate`，取消生效时才发送订阅取消通知。
                  x-onerway-constraints:
                    - kind: rule
                      text: 托管订阅使用模式 `2` 或 `3` 时，合约必须已有到期时间。模式 `2`
                        在当前计费周期结束时取消；原到期时间更早时，保留较早的时间。当前计费周期结束时间不得早于当前时刻。
                    - kind: rule
                      text: 预约取消申请成功时不发送取消通知；取消实际生效后才发送[订阅取消通知](/zh/payments/versions/v2/api-reference/webhooks/subscription-canceled)。已经发起的扣款按该笔交易的结果处理。
                expireDate:
                  type: string
                  description: 订阅取消生效日期，格式 `yyyy-MM-dd`。
                  x-onerway-required: conditional
                  x-onerway-condition:
                    - 当 `expireMode=3`，即指定日期取消时必填。
                  x-onerway-constraints:
                    - kind: rule
                      text: 托管订阅使用模式 `3` 时，指定日期不得晚于下一计费日期或合约原到期日期。Onerway
                        将该日期与计费周期起始锚点的时刻组合；组合后的时间不得早于服务器当前时刻。
              required:
                - merchantNo
                - contractId
            examples:
              cancel-subscription-immediately:
                summary: 立即取消订阅
                value:
                  merchantNo: replace_with_merchant_no
                  contractId: example_subscription_contract
                  expireMode: "1"
              cancel-at-cycle-end:
                summary: 计费周期结束时取消
                value:
                  merchantNo: replace_with_merchant_no
                  contractId: example_subscription_contract
                  expireMode: "2"
              cancel-on-specified-date:
                summary: 指定日期取消
                value:
                  merchantNo: replace_with_merchant_no
                  contractId: example_subscription_contract
                  expireMode: "3"
                  expireDate: 2099-12-15
      responses:
        "200":
          description: 取消请求处理成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  respCode:
                    type: string
                    description: 响应码；`20000`
                      表示请求处理成功，其余为错误码。完整码表见[响应码](/zh/payments/versions/v2/api-reference/response-codes)。
                  respMsg:
                    type: string
                    description: 响应码对应的可读消息。
                  data:
                    type: string
                    description: 取消请求处理成功时固定返回
                      `3`，不表示预约取消已经生效。请通过[查询订阅详情](/zh/payments/versions/v2/api-reference/endpoints/query-subscription-details)返回的
                      `subscriptionStatus`
                      或[订阅取消通知](/zh/payments/versions/v2/api-reference/webhooks/subscription-canceled)判断取消是否生效。
              examples:
                cancellation-request-succeeded:
                  summary: 取消请求处理成功
                  value:
                    respCode: "20000"
                    respMsg: Success
                    data: "3"
      security:
        - apiKeyAuth:
            []
components:
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: ApiKey
```
