Skip to main content

Documentation

Cancel Subscription

Delete a subscription from the database.

When the subscription is canceled immediately, this method triggers the following webhooks:

The following asynchronous webhook provides information about later changes to the subscription object:

When the subscription is canceled at the end of the current billing period, all of these webhooks are triggered asynchronously.

    • subscription

    • ID of the subscription. String starting with sub_.

    • cancel_at_period_end

    • Determines when the subscription is canceled.

      • true - Cancels the subscription at the end of the current period.

      • false - Cancels the subscription immediately.

      false

Code Samples
    • .NET

      • using System;
        using System.Text.Json;
        
        namespace RapydApiRequestSample
        {
            class Program
            {
                static void Main(string[] args)
                {
                    try
                    {
        
                        string subscriptionItem = "sub_5b47720e0281b501092a695773fb2279";
        
                        string result = RapydApiRequestSample.Utilities.MakeRequest("DELETE", $"/v1/payments/subscriptions/{subscriptionItem}");
        
                        Console.WriteLine(result);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error completing request: " + e.Message);
                    }
                }
            }
        }
        
    • JavaScript

      • const makeRequest = require('<path-to-your-utility-file>/utilities').makeRequest;
        
        async function main() {
          try {
            const result = await makeRequest(
              'DELETE',
              '/v1/payments/subscriptions/sub_5b47720e0281b501092a695773fb2279'
            );
        
            console.log(result);
          } catch (error) {
            console.error('Error completing request', error);
          }
        }
    • PHP

      • <?php
        $path = $_SERVER['DOCUMENT_ROOT'];
        $path .= "/<path-to-your-utility-file>/utilities.php";
        include($path);
        try {
            $object = make_request('delete', '/v1/payments/subscriptions/sub_5b47720e0281b501092a695773fb2279');
            var_dump($object);
        } catch(Exception $e) {
            echo "Error: $e";
        }
        ?>
    • Python

      • from pprint import pprint
        
        from utilities import make_request
        
        result = make_request(method='delete', path='/v1/payments/subscriptions/sub_c120969b649d4a029b660228e00c8d97')
        pprint(result)
        
  • /v1/payments/subscriptions/:subscription

  • Cancel Subscription Immediately

  • curl -X delete
    https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_6f634b7aced3e9f872f66fc8ef708ae5
    -H 'access_key: your-access-key-here'
    -H 'Content-Type: application/json'
    -H 'salt: your-random-string-here'
    -H 'signature: your-calculated-signature-here'
    -H 'timestamp: your-unix-timestamp-here'
    
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "ecc6d7bc-fa24-4659-a570-3190719b0d6e"
        },
        "data": {
            "id": "sub_6f634b7aced3e9f872f66fc8ef708ae5",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1701267785,
            "created_at": 1701267785,
            "customer_token": "cus_da95b15aa75f76e8d5ac42293e326ddd",
            "days_until_due": 30,
            "metadata": {},
            "tax_percent": 0,
            "cancel_at_period_end": false,
            "canceled_at": 1701854001,
            "current_period_end": 1703859785,
            "current_period_start": 1701267785,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_60ab178c14ab3cb0d74ff82458485963",
                        "created": 1701267785,
                        "metadata": null,
                        "quantity": 5,
                        "plan": {
                            "id": "plan_811bfbf126ea0cbabeee447cdb1486c2",
                            "aggregate_usage": "sum",
                            "amount": 0,
                            "billing_scheme": "tiered",
                            "created_at": 1701267773,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {
                                "merchant_defined": true
                            },
                            "product": {
                                "id": "product_c07612df254fce2aaba1854e3ce2008b",
                                "active": true,
                                "attributes": [
                                    "location",
                                    "size"
                                ],
                                "created_at": 1701267766,
                                "description": "Monthly parking - covered area, compact car",
                                "images": [],
                                "metadata": {},
                                "name": "Monthly parking",
                                "package_dimensions": {
                                    "height": 0,
                                    "length": 0,
                                    "weight": 0,
                                    "width": 0
                                },
                                "shippable": false,
                                "skus": [],
                                "statement_descriptor": "",
                                "type": "service",
                                "unit_label": "",
                                "updated_at": 1701267766
                            },
                            "nickname": "sbp",
                            "tiers": [
                                {
                                    "amount": 15,
                                    "up_to": "inf",
                                    "flat_amount": 0
                                }
                            ],
                            "tiers_mode": "volume",
                            "transform_usage": {
                                "divide_by": 0,
                                "round": ""
                            },
                            "trial_period_days": 0,
                            "usage_type": "licensed",
                            "active": true
                        }
                    }
                ],
                "has_more": false,
                "total_count": 1,
                "url": "/v1/subscription_items?subscription=sub_6f634b7aced3e9f872f66fc8ef708ae5"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_160e5257c76e6c33f35c1300e19a885a",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": true,
            "enable_retries": true,
            "retry_options": {
                "max_retries": 3,
                "retry_interval": "days",
                "retry_schedule": "fixed",
                "retry_interval_count": 3
            }
        }
    }
  • Cancel Subscription at end of billing cycle

  • curl -X delete
    https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_6f634b7aced3e9f872f66fc8ef708ae5?cancel_at_period_end=true
    -H 'access_key: your-access-key-here'
    -H 'Content-Type: application/json'
    -H 'salt: your-random-string-here'
    -H 'signature: your-calculated-signature-here'
    -H 'timestamp: your-unix-timestamp-here'
    
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "e172e73d-3892-437f-ac6d-1d8ac3913f31"
        },
        "data": {
            "id": "sub_6f634b7aced3e9f872f66fc8ef708ae5",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1701267785,
            "created_at": 1701267785,
            "customer_token": "cus_da95b15aa75f76e8d5ac42293e326ddd",
            "days_until_due": 30,
            "metadata": {},
            "tax_percent": 0,
            "cancel_at_period_end": true,
            "canceled_at": 1701853806,
            "current_period_end": 1703859785,
            "current_period_start": 1701267785,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_60ab178c14ab3cb0d74ff82458485963",
                        "created": 1701267785,
                        "metadata": null,
                        "quantity": 5,
                        "plan": {
                            "id": "plan_811bfbf126ea0cbabeee447cdb1486c2",
                            "aggregate_usage": "sum",
                            "amount": 0,
                            "billing_scheme": "tiered",
                            "created_at": 1701267773,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {
                                "merchant_defined": true
                            },
                            "product": {
                                "id": "product_c07612df254fce2aaba1854e3ce2008b",
                                "active": true,
                                "attributes": [
                                    "location",
                                    "size"
                                ],
                                "created_at": 1701267766,
                                "description": "Monthly parking - covered area, compact car",
                                "images": [],
                                "metadata": {},
                                "name": "Monthly parking",
                                "package_dimensions": {
                                    "height": 0,
                                    "length": 0,
                                    "weight": 0,
                                    "width": 0
                                },
                                "shippable": false,
                                "skus": [],
                                "statement_descriptor": "",
                                "type": "service",
                                "unit_label": "",
                                "updated_at": 1701267766
                            },
                            "nickname": "sbp",
                            "tiers": [
                                {
                                    "amount": 15,
                                    "up_to": "inf",
                                    "flat_amount": 0
                                }
                            ],
                            "tiers_mode": "volume",
                            "transform_usage": {
                                "divide_by": 0,
                                "round": ""
                            },
                            "trial_period_days": 0,
                            "usage_type": "licensed",
                            "active": true
                        }
                    }
                ],
                "has_more": false,
                "total_count": 1,
                "url": "/v1/subscription_items?subscription=sub_6f634b7aced3e9f872f66fc8ef708ae5"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_160e5257c76e6c33f35c1300e19a885a",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": true,
            "enable_retries": true,
            "retry_options": {
                "max_retries": 3,
                "retry_interval": "days",
                "retry_schedule": "fixed",
                "retry_interval_count": 3
            }
        }
    }