Skip to main content

Documentation

Update Subscription

Modify a subscription.

You can redefine the trial period if the status is active or trialing. If you change trial_end to now, the trial period ends immediately and billing_cycle_anchor is set to the present time. If you define a new trial period and prorate is true, the charges will be prorated.

To change a payment method, set the payment_method field to the ID of a payment method belonging to the same customer.

This method triggers the Subscription Updated Webhook. This webhook contains the same information as the response.

    • subscription

    • ID of the subscription. String starting with sub_.

Code Samples
    • .NET

      • using System;
        using System.Text.Json;
        
        namespace RapydApiRequestSample
        {
            class Program
            {
                static void Main(string[] args)
                {
                    try
                    {
        
                        string subscription = "sub_4d9bcdf0a768a90e2f39a04ae1331e90";
        
                        var requestObj = new
                        {
                            metadata = new
                            {
                                merchant_defined = "updated"
                            },
                            tax_percent = 10
                        };
        
                        string request = JsonSerializer.Serialize(requestObj);
        
                        string result = RapydApiRequestSample.Utilities.MakeRequest("POST", $"/v1/subscriptions/{subscription}");
        
                        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 body = {
              trial_period_days: 30,
              coupon: 'coupon_f0517cffe4792ef3dd77f08a41b80d4e'
            };
            const result = await makeRequest(
              'POST',
              '/v1/payments/subscriptions/sub_4d9bcdf0a768a90e2f39a04ae1331e90',
              body
            );
        
            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 {
            $body = [
                "tax_percent"=> 10
            ];
            $object = make_request('post', '/v1/subscriptions/sub_4d9bcdf0a768a90e2f39a04ae1331e90');
            var_dump($object);
        } catch(Exception $e) {
            echo "Error: $e";
        }
        ?>
    • Python

      • from pprint import pprint
        
        from utilities import make_request
        
        subscription = {
            "metadata": {
                "merchant_defined": True
            },
            "tax_percent": 10
        }
        result = make_request(method='post', path='/v1/subscriptions/sub_4d9bcdf0a768a90e2f39a04ae1331e90', body=subscription)
        pprint(result)
  • /v1/payments/subscriptions/:subscription

  • Update Subscription - tax percent

  • curl -X post
    https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_4d9bcdf0a768a90e2f39a04ae1331e90
    -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 '{
        "metadata": {
            "merchant_defined": "updated"
        },
        "tax_percent": 10
    }
    '
    
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "8ba15aca-8cfa-416f-9469-a23738444840"
        },
        "data": {
            "id": "sub_4d9bcdf0a768a90e2f39a04ae1331e90",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1592379647,
            "created_at": 1592379647,
            "customer_token": "cus_ec543c4dfcf6ac1d015b2e3e4fd07180",
            "days_until_due": null,
            "metadata": {},
            "tax_percent": 10,
            "cancel_at_period_end": true,
            "canceled_at": null,
            "current_period_end": 1594971647,
            "current_period_start": 1592379647,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_a9f34be0551bf85ffb13e4fdb6107d6d",
                        "created": 1592379647,
                        "metadata": null,
                        "quantity": 1,
                        "plan": {
                            "id": "plan_51788cf4ed1b672cb0a2a97773887f5b",
                            "aggregate_usage": "sum",
                            "amount": 0,
                            "billing_scheme": "tiered",
                            "created_at": 1592311574,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {
                                "merchant_defined": true
                            },
                            "product": {
                                "id": "product_ab9599d248f6d4d4311148aea68e20d0",
                                "active": true,
                                "attributes": [
                                    "location",
                                    "size"
                                ],
                                "created_at": 1592299027,
                                "description": "Monthly parking - covered area, compact car",
                                "images": [
                                    "64bit-encoded-image-1",
                                    "64bit-encoded-image-2"
                                ],
                                "metadata": {
                                    "merchant_defined": true
                                },
                                "name": "Monthly parking",
                                "package_dimensions": {
                                    "height": 0,
                                    "length": 0,
                                    "weight": 0,
                                    "width": 0
                                },
                                "shippable": false,
                                "skus": [],
                                "statement_descriptor": "",
                                "type": "services",
                                "unit_label": "",
                                "updated_at": 1592299027
                            },
                            "nickname": "Basic parking",
                            "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_4d9bcdf0a768a90e2f39a04ae1331e90"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_156da7de485275ad0bae798b7cb8fea1",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": true
        }
    }
  • Add Discount

  • curl -X post
    https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_210c3425c928495873104df273ed237a
    -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 '{
        "coupon": "coupon_547e6e4a218fe358c5cf22a5393fef5f",
        "metadata": {
            "merchant_defined": "10% discount added"
        }
    }
    '
    
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "f5cce9cc-44f9-47ce-ae76-5857d3ee6545"
        },
        "data": {
            "id": "sub_210c3425c928495873104df273ed237a",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1646899867,
            "created_at": 1646899867,
            "customer_token": "cus_c62e8561f1f9217676819dce60accc08",
            "days_until_due": null,
            "metadata": {
                "merchant_defined": "10% discount added"
            },
            "tax_percent": 0,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "current_period_end": 1649578267,
            "current_period_start": 1646899867,
            "discount": {
                "id": "dis_0464bfadba272ab0d5530ce2d14ffd46",
                "coupon": {
                    "id": "coupon_547e6e4a218fe358c5cf22a5393fef5f",
                    "amount_off": 0,
                    "created": 1646893705,
                    "currency": "",
                    "duration": "forever",
                    "duration_in_months": 0,
                    "max_redemptions": 10,
                    "metadata": {},
                    "percent_off": 10,
                    "redeem_by": 0,
                    "times_redeemed": 1,
                    "discount_valid_until": 0,
                    "discount_validity_in_months": 0,
                    "discount_duration_in_uses": 0,
                    "description": "",
                    "valid": true
                },
                "customer": "",
                "end": -1,
                "start": 1646900084,
                "number_of_uses": 0,
                "valid": true
            },
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_6cbdb6ac74bed62b6953b7d9892a8b0c",
                        "created": 1646899867,
                        "metadata": null,
                        "quantity": 1,
                        "plan": {
                            "id": "plan_5a99897cdcd7175cb8119254d02befdb",
                            "aggregate_usage": "sum",
                            "amount": 3,
                            "billing_scheme": "per_unit",
                            "created_at": 1646897987,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {},
                            "product": {
                                "id": "product_13ae28f06213a4253b96e6143b598591",
                                "active": true,
                                "attributes": [],
                                "created_at": 1646897609,
                                "description": "",
                                "images": [],
                                "metadata": {},
                                "name": "Acme music stream",
                                "package_dimensions": {
                                    "height": 0,
                                    "length": 0,
                                    "weight": 0,
                                    "width": 0
                                },
                                "shippable": false,
                                "skus": [],
                                "statement_descriptor": "",
                                "type": "services",
                                "unit_label": "",
                                "updated_at": 1646897609
                            },
                            "nickname": "Basic",
                            "tiers": [],
                            "tiers_mode": "",
                            "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_210c3425c928495873104df273ed237a"
            },
            "status": "unpaid",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_156da7de485275ad0bae798b7cb8fea1",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": true
        }
    }
  • Change payment method

  • curl -X post
    https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_499f7792b08a5c06ff35ba6279092a9a
    -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 '{
        "payment_method": "card_156da7de485275ad0bae798b7cb8fea1"
    }
    '
    
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "44929fc2-c61d-44d2-b9b0-7316182b1a8a"
        },
        "data": {
            "id": "sub_499f7792b08a5c06ff35ba6279092a9a",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1651414755,
            "created_at": 1651414755,
            "customer_token": "cus_60cb18f281b7472a076c5834c552d12b",
            "days_until_due": 30,
            "metadata": {},
            "tax_percent": 0,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "current_period_end": 1654438755,
            "current_period_start": 1651414755,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_1b8641d7ca97fdb12b5dc3fbcacc5ff8",
                        "created": 1651414755,
                        "metadata": null,
                        "quantity": 1,
                        "plan": {
                            "id": "plan_def67ab781c1f0968d13582c4eba3532",
                            "aggregate_usage": "sum",
                            "amount": 100,
                            "billing_scheme": "per_unit",
                            "created_at": 1651414740,
                            "currency": "MXN",
                            "interval": "day",
                            "interval_count": 35,
                            "metadata": {},
                            "product": {
                                "id": "product_53060a42eb2d702c6d3e926434d63cd3",
                                "active": true,
                                "attributes": [
                                    "location",
                                    "size"
                                ],
                                "created_at": 1651414721,
                                "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": "ffff",
                                "type": "services",
                                "unit_label": "",
                                "updated_at": 1651414721
                            },
                            "nickname": "Parking - silver",
                            "tiers": [],
                            "tiers_mode": "",
                            "transform_usage": {
                                "divide_by": 1000,
                                "round": "up"
                            },
                            "trial_period_days": 7,
                            "usage_type": "licensed"
                        }
                    }
                ],
                "has_more": false,
                "total_count": 1,
                "url": "/v1/subscription_items?subscription=sub_499f7792b08a5c06ff35ba6279092a9a"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_156da7de485275ad0bae798b7cb8fea1",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": true
        }
    }
  • Update Subscription Billing Cycle Anchor - testing only

  • curl -X post
    https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_17b55f5ccfe97f80fe73a7acc0545386
    -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 '{
        "billing_cycle_anchor": "now",
        "days_until_due": 0
    }
    '
    
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "f3d596ad-0d48-45e5-87a2-e5e0a6fa57ba"
        },
        "data": {
            "id": "sub_17b55f5ccfe97f80fe73a7acc0545386",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1673500131,
            "created_at": 1673499338,
            "customer_token": "cus_5bee14584a78638ba717e746be62ee01",
            "days_until_due": null,
            "metadata": {},
            "tax_percent": 0,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "current_period_end": 1674104931,
            "current_period_start": 1673500131,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_a6f17fb61f9cad3c86327dd71dfaeb0c",
                        "created": 1673499338,
                        "metadata": {
                            "merchant_defined": true
                        },
                        "quantity": 25,
                        "plan": {
                            "id": "plan_d2b079cfbe29fc802baf215aa64bb1fa",
                            "aggregate_usage": "sum",
                            "amount": 0,
                            "billing_scheme": "tiered",
                            "created_at": 1673499122,
                            "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": 0,
                "url": "/v1/subscription_items?subscription=sub_17b55f5ccfe97f80fe73a7acc0545386"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_794ddbfd19fcc548c8399ca957f11a24",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": false
        }
    }