Cancel Subscription
Cancel a subscription.
When the subscription is canceled immediately, this method triggers the following webhooks:
Subscription Canceled Webhook. This webhook contains the same information as the response.
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_5b47720e0281b501092a695773fb2279 -H 'access_key: your-access-key-here' -H 'Content-Type: application/json' -H 'idempotency: your-idempotency-parameter-here' -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": "0040dd38-b2a8-4151-a461-c5be6aedc0d2" }, "data": { "id": "sub_08cfc0e792997a164c60d7a0264156f5", "billing": "pay_automatically", "billing_cycle_anchor": 1590332014, "created_at": 1590332014, "customer_token": "cus_efd877ff7df0b0259f021792d0780185", "days_until_due": null, "metadata": { "merchant_defined": true }, "tax_percent": 10.5, "cancel_at_period_end": false, "canceled_at": 1592214901, "current_period_end": 1592214901, "current_period_start": 1590332014, "discount": null, "ended_at": null, "subscription_items": { "data": [ { "id": "subi_bb7c23b6b91b85b1ca76e64dada1b29e", "created": 1590332014, "metadata": null, "quantity": 1, "plan": { "id": "plan_55a5f774ff9ac7f1eba29da9a21cea96", "aggregate_usage": "sum", "amount": 0, "billing_scheme": "tiered", "created_at": 1590331951, "currency": "USD", "interval": "month", "interval_count": 1, "metadata": {}, "product": { "id": "product_cc5759bfc0c1c116cbf3b2f852a7bf45", "active": true, "attributes": [ "location", "size" ], "created_at": 1590331937, "description": "Monthly parking - covered area, compact car", "images": [ "64bit-encoded-image-1", "64bit-encoded-image-2" ], "metadata": {}, "name": "Monthly parking", "package_dimensions": { "height": 0, "length": 0, "weight": 0, "width": 0 }, "shippable": false, "skus": [], "statement_descriptor": "STATEMENT DESCRIPTOR", "type": "services", "unit_label": "", "updated_at": 1590331937 }, "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" } } ], "has_more": false, "total_count": 1, "url": "/v1/subscription_items?subscription=sub_08cfc0e792997a164c60d7a0264156f5" }, "status": "canceled", "trial_end": null, "trial_start": null, "payment_method": "card_1dd66ca041c5ddcd1afa1d5351497221", "payment_fields": null, "payout_fields": null, "type": "payment", "simultaneous_invoice": true } }
Cancel Subscription at end of billing cycle
curl -X delete https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_5b47720e0281b501092a695773fb2279 -H 'access_key: your-access-key-here' -H 'Content-Type: application/json' -H 'idempotency: your-idempotency-parameter-here' -H 'salt: your-random-string-here' -H 'signature: your-calculated-signature-here' -H 'timestamp: your-unix-timestamp-here' -d '{ "cancel_at_period_end": true } '
{ "status": { "error_code": "", "status": "SUCCESS", "message": "", "response_code": "", "operation_id": "ba0f204d-c34a-423f-979f-0553e524d177" }, "data": { "id": "sub_ca4e991ddbf941d8b3e6c679b6c24890", "billing": "pay_automatically", "billing_cycle_anchor": 1675152894, "created_at": 1675152894, "customer_token": "cus_d2ae632abd7649d0074d57ea0644d796", "days_until_due": 30, "metadata": { "merchant_defined": true }, "tax_percent": 10.5, "cancel_at_period_end": true, "canceled_at": 1675152908, "current_period_end": 1677572094, "current_period_start": 1675152894, "discount": null, "ended_at": null, "subscription_items": { "data": [ { "id": "subi_465d1f18162387ceca951e343cd340b4", "created": 1675152894, "metadata": null, "quantity": 1, "plan": { "id": "plan_0403e74bdfbfaec6c4eeb93630266631", "aggregate_usage": "sum", "amount": 0, "billing_scheme": "tiered", "created_at": 1675152734, "currency": "USD", "interval": "month", "interval_count": 1, "metadata": {}, "product": { "id": "product_fa1b30b1caa6711910f60e758dbe70c6", "active": true, "attributes": [ "quantity" ], "created_at": 1673498178, "description": "Public Transportation Usage", "images": [ "64bit-encoded-image-1" ], "metadata": { "merchant_defined": true }, "name": "Transit Rides", "package_dimensions": { "height": 0, "length": 0, "weight": 0, "width": 0 }, "shippable": false, "skus": [], "statement_descriptor": "Transit Usage", "type": "services", "unit_label": "rides", "updated_at": 1673498178 }, "nickname": "Transit Use", "tiers": [ { "amount": 4, "up_to": 5, "flat_amount": 1 }, { "amount": 3, "up_to": 10, "flat_amount": 0 }, { "amount": 2, "up_to": 20, "flat_amount": 0 }, { "amount": 1, "up_to": "inf", "flat_amount": 0 } ], "tiers_mode": "graduated", "transform_usage": { "divide_by": 1, "round": "up" }, "trial_period_days": 0, "usage_type": "licensed" } } ], "has_more": false, "total_count": 1, "url": "/v1/subscription_items?subscription=sub_ca4e991ddbf941d8b3e6c679b6c24890" }, "status": "active", "trial_end": null, "trial_start": null, "payment_method": "card_00bb4ee57965b83392d9718ab345a744", "payment_fields": null, "payout_fields": null, "type": "payment", "simultaneous_invoice": false } }