如何在Node.js中更新Stripe的订阅(添加定价计划)?

时间:2020-04-19 23:11:35

标签: stripe-payments

我正在构建一个销售数种产品的Web应用程序,每种产品都对应一个定价计划。所有付款系统都由Stripe管理。

经常发生这样的情况:用户拥有包含产品A(即一个定价计划)的订阅,然后又想将产品B(即另一个定价计划)添加到同一订阅中。我想知道如何通过Stripe的API实现这一目标。

有Stripe的网页来更新订阅(例如,https://dashboard.stripe.com/test/subscriptions/sub_H4pQGW8nnc80vF/edit,其中sub_H4pQGW8nnc80vF是订阅ID)。假设该客户在订阅中已经有1 Verificator,然后他想在同一订阅中添加1 Pretty Formula。我通过网站进行此更新。这是日志:

enter image description here

enter image description here

这是完整的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": ""
}

这是完整的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": "sdtikply@gmail.com",
  "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
}

所以我的问题是,如何在后端(Node.js)中进行编码以实现完全相同的效果?

1 个答案:

答案 0 :(得分:0)

您只需要使用Update Subscription API并提供items部分,就像上面看到的那样:

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

第一个项目是现有的订阅项目(si_),第二个项目是您要添加的新项目。

相关问题