# Cancel subscription contract

> Cancel a subscription contract immediately, at the end of the current billing cycle, or on a specified date.

```yaml
openapi: 3.1.0
x-onerway-docs-version: v2
x-onerway-spec-key: payments/versions/v2/subscriptions/cancel-subscription-contract
info:
  title: Cancel subscription contract
  version: v2
  description: Cancel a subscription contract immediately, at the end of the
    current billing cycle, or on a specified date.
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: Cancel subscription contract
      description: Cancel a subscription contract immediately, at the end of the
        current billing cycle, or on a specified date.
      parameters:
        - name: ApiKey
          in: header
          required: true
          description: API key for the merchant account. Send it in the `ApiKey` request
            header. See
            [Authentication](/payments/versions/v2/get-started/authentication).
          schema:
            type: string
            description: API key for the merchant account. Send it in the `ApiKey` request
              header. See
              [Authentication](/payments/versions/v2/get-started/authentication).
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                merchantNo:
                  type: string
                  description: Merchant number assigned by Onerway. See
                    [Setup](/payments/versions/v2/get-started/setup#retrieve-your-credentials)
                    for how to obtain it.
                contractId:
                  type: string
                  description: Subscription contract number to cancel. It normally comes from the
                    initial subscription response, webhook, or [Query
                    subscription
                    details](/payments/versions/v2/api-reference/endpoints/query-subscription-details).
                expireMode:
                  type: string
                  description: Cancellation timing for Onerway-managed subscriptions
                    (`selfExecute=1`). When both `expireMode` and `expireDate`
                    are omitted, cancellation is scheduled for the end of the
                    current billing cycle. Merchant-managed subscriptions
                    (`selfExecute=2`) are canceled immediately.
                  enum:
                    - "1"
                    - "2"
                    - "3"
                  x-enum-descriptions:
                    "1": Cancel immediately and send a subscription cancellation notification.
                      Customer emails for Onerway-managed subscriptions depend
                      on the configured email address and email settings.
                    "2": Cancel at the end of the current billing cycle.
                    "3": Cancel on a specified date. Submit `expireDate`; the cancellation
                      notification is sent when the cancellation takes effect.
                  x-onerway-constraints:
                    - kind: rule
                      text: For Onerway-managed subscriptions, modes `2` and `3` require an existing
                        contract expiration time. Mode `2` uses the end of the
                        current billing cycle, or the existing expiration time
                        if earlier; the cycle end must not be earlier than the
                        current time.
                    - kind: rule
                      text: Scheduled cancellation does not send a cancellation notification when the
                        request is accepted. The [subscription cancellation
                        webhook](/payments/versions/v2/api-reference/webhooks/subscription-canceled)
                        is sent when cancellation takes effect. Payments already
                        initiated are handled according to their own results.
                expireDate:
                  type: string
                  description: Date when cancellation takes effect, in `yyyy-MM-dd` format.
                  x-onerway-required: conditional
                  x-onerway-condition:
                    - Required when `expireMode=3`, cancel on a specified date.
                  x-onerway-constraints:
                    - kind: rule
                      text: For an Onerway-managed subscription in mode `3`, the date must not be
                        later than the next billing date or the existing
                        contract expiration date. Onerway combines this date
                        with the time of day of the billing-cycle start anchor;
                        the resulting time must not be earlier than the current
                        server time.
              required:
                - merchantNo
                - contractId
            examples:
              cancel-subscription-immediately:
                summary: Cancel a subscription immediately
                value:
                  merchantNo: replace_with_merchant_no
                  contractId: example_subscription_contract
                  expireMode: "1"
              cancel-at-cycle-end:
                summary: Cancel at the end of the billing cycle
                value:
                  merchantNo: replace_with_merchant_no
                  contractId: example_subscription_contract
                  expireMode: "2"
              cancel-on-specified-date:
                summary: Cancel on a specified date
                value:
                  merchantNo: replace_with_merchant_no
                  contractId: example_subscription_contract
                  expireMode: "3"
                  expireDate: 2099-12-15
      responses:
        "200":
          description: Cancellation request processed successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  respCode:
                    type: string
                    description: "`20000` means the cancellation request was processed successfully.
                      Other values are error codes. See [Response
                      codes](/payments/versions/v2/api-reference/response-codes\
                      )."
                  respMsg:
                    type: string
                    description: Human-readable message for the response code.
                  data:
                    type: string
                    description: Fixed to `3` when the cancellation request is processed
                      successfully. This response does not confirm that a
                      scheduled cancellation has taken effect. Use
                      `subscriptionStatus` from [Query subscription
                      details](/payments/versions/v2/api-reference/endpoints/query-subscription-details)
                      or the [subscription cancellation
                      webhook](/payments/versions/v2/api-reference/webhooks/subscription-canceled)
                      to determine whether cancellation has taken effect.
              examples:
                cancellation-request-succeeded:
                  summary: Cancellation request processed successfully
                  value:
                    respCode: "20000"
                    respMsg: Success
                    data: "3"
      security:
        - apiKeyAuth:
            []
components:
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: ApiKey
```
