SoftTimur
SoftTimur

Reputation: 5540

How to update a subscription (adding a pricing plan) by Stripe in Node.js?

I'm building a web app which sells several products, each product corresponds to a pricing plan. All the payment system is managed by Stripe.

It happens very often that a user has a subscription that contains Product A (i.e., one pricing plan), and then he wants to add Product B (i.e., another pricing plan) to the same subscription. I want to know how to achieve this by APIs of Stripe.

There is a webpage of Stripe to update a subscription (e.g., https://dashboard.stripe.com/test/subscriptions/sub_H4pQGW8nnc80vF/edit where sub_H4pQGW8nnc80vF is the subscription ID). Let's assume this customer already has 1 Verificator in the subscription, and then he wants to add 1 Pretty Formula to the same subscription. I do this update via the website. Here is the log:

enter image description here

enter image description here

Here is the full Request POST body:

{
  "items": {
    "0": {
      "billing_thresholds": "",
      "deleted": "false",
      "id": "si_H4pQal4ZxGzLbW",
      "quantity": "1",
      "tax_rates": ""
    },
    "1": {
      "billing_thresholds": "",
      "plan": "plan_Gz6i9yPVIjrDPX",
      "deleted": "false",
      "quantity": "1"
    }
  },
  "off_session": "true",
  "prorate": "true",
  "cancel_at": "",
  "days_until_due": "30",
  "default_tax_rates": "",
  "collection_method": "send_invoice",
  "billing_thresholds": "",
  "enable_incomplete_payments": "false",
  "invoice_settings": {
    "description": "",
    "send_hosted_payment_email": "true",
    "supported_payment_methods": {
      "ach_credit_transfer": "false",
      "au_becs_debit": "false",
      "bancontact": "false",
      "card": "true",
      "fpx": "false",
      "giropay": "false",
      "ideal": "false",
      "jp_credit_transfer": "false",
      "paper_check": "false",
      "sepa_credit_transfer": "false",
      "sofort": "false"
    },
    "custom_fields": "",
    "footer": ""
  },
  "default_payment_method": "",
  "default_source": ""
}

Here is the full Response body:

{
  "id": "sub_H4pQGW8nnc80vF",
  "object": "subscription",
  "application_fee_percent": null,
  "billing_cycle_anchor": 1586597833,
  "billing_thresholds": null,
  "cancel_at": null,
  "cancel_at_period_end": false,
  "canceled_at": null,
  "collection_method": "send_invoice",
  "created": 1586597833,
  "current_period_end": 1589189833,
  "current_period_start": 1586597833,
  "customer": "5e575130651c5721d808d25b",
  "customer_email": "[email protected]",
  "customer_name": "Thomas Joseph",
  "days_until_due": 30,
  "default_payment_method": null,
  "default_source": null,
  "default_tax_rates": [
  ],
  "discount": null,
  "ended_at": null,
  "invoice_settings": {
    "send_hosted_payment_email": true,
    "supported_payment_methods": {
      "ach_credit_transfer": false,
      "au_becs_debit": false,
      "bancontact": false,
      "card": true,
      "fpx": false,
      "giropay": false,
      "ideal": false,
      "jp_credit_transfer": false,
      "paper_check": false,
      "sepa_credit_transfer": false,
      "sofort": false
    }
  },
  "items": {
    "object": "list",
    "data": [
      {
        "id": "si_H4pQal4ZxGzLbW",
        "object": "subscription_item",
        "billing_thresholds": null,
        "created": 1586597833,
        "metadata": {
        },
        "plan": {
          "id": "plan_Ga6n9yMYCDnHCu",
          "object": "plan",
          "active": true,
          "aggregate_usage": null,
          "amount": 500,
          "amount_decimal": "500",
          "billing_scheme": "per_unit",
          "created": 1579512574,
          "currency": "usd",
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "metadata": {
          },
          "name": "Verificator",
          "nickname": "Verificator",
          "owning_merchant": "acct_1CiOQBEV4K2GahYL",
          "owning_merchant_info": "acct_1CiOQBEV4K2GahYL",
          "product": "prod_Ga6mVdA8KXyZ8I",
          "tiers": null,
          "tiers_mode": null,
          "transform_usage": null,
          "trial_period_days": null,
          "usage_type": "licensed"
        },
        "quantity": 1,
        "subscription": "sub_H4pQGW8nnc80vF",
        "tax_rates": [
        ]
      },
      {
        "id": "si_H82ES9BdIKZCNG",
        "object": "subscription_item",
        "billing_thresholds": null,
        "created": 1587337381,
        "metadata": {
        },
        "plan": {
          "id": "plan_Gz6i9yPVIjrDPX",
          "object": "plan",
          "active": true,
          "aggregate_usage": null,
          "amount": 500,
          "amount_decimal": "500",
          "billing_scheme": "per_unit",
          "created": 1585278262,
          "currency": "usd",
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "metadata": {
          },
          "name": "Pretty Formula",
          "nickname": "Pretty Formula",
          "owning_merchant": "acct_1CiOQBEV4K2GahYL",
          "owning_merchant_info": "acct_1CiOQBEV4K2GahYL",
          "product": "prod_GxqkRFdI08DvyR",
          "tiers": null,
          "tiers_mode": null,
          "transform_usage": null,
          "trial_period_days": null,
          "usage_type": "licensed"
        },
        "quantity": 1,
        "subscription": "sub_H4pQGW8nnc80vF",
        "tax_rates": [
        ]
      }
    ],
    "has_more": false,
    "total_count": 2,
    "url": "/v1/subscription_items?subscription=sub_H4pQGW8nnc80vF"
  },
  "latest_invoice": "in_1GWfm1EV4K2GahYLlmtUEISo",
  "livemode": false,
  "metadata": {
  },
  "next_pending_invoice_item_invoice": null,
  "owning_merchant": "acct_1CiOQBEV4K2GahYL",
  "owning_merchant_info": "acct_1CiOQBEV4K2GahYL",
  "pause_collection": null,
  "pending_invoice_item_interval": null,
  "pending_setup_intent": null,
  "pending_update": null,
  "plan": null,
  "quantity": null,
  "schedule": null,
  "start_date": 1586597833,
  "status": "active",
  "tax_percent": null,
  "trial_end": null,
  "trial_start": null
}

So my question is, how can I code in my backend (Node.js) to achieve exactly the same thing?

Upvotes: 0

Views: 1717

Answers (1)

floatingLomas
floatingLomas

Reputation: 8747

You just need to use the Update Subscription API and provide the items portion just as you're seeing above:

  "items": {
    "0": {
      "billing_thresholds": "",
      "deleted": "false",
      "id": "si_H4pQal4ZxGzLbW",
      "quantity": "1",
      "tax_rates": ""
    },
    "1": {
      "billing_thresholds": "",
      "plan": "plan_Gz6i9yPVIjrDPX",
      "deleted": "false",
      "quantity": "1"
    }
  },

The first item is the existing Subscription Item (si_), and the second one is the new one you want to add.

Upvotes: 0

Related Questions