【问题标题】:How to update a subscription (adding a pricing plan) by Stripe in Node.js?如何在 Node.js 中通过 Stripe 更新订阅(添加定价计划)?
【发布时间】:2020-08-02 09:20:35
【问题描述】:

我正在构建一个销售多种产品的网络应用,每种产品对应一个定价计划。所有支付系统都由 Stripe 管理。

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

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

这是完整的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) 中编写代码来实现完全相同的目标?

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

    您只需要使用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_),第二项是您要添加的新订阅项。

    【讨论】:

    • 谢谢...很高兴知道提供items 部分就足够了。
    • 如果答案不是即时的,我可能会问另一个问题。当用户在我们的change-subscription 页面中选择更改计划时,我们希望根据他们的选择快速显示/预览即将到来的发票日期和金额,您知道在确定更改订阅之前如何实现吗?
    • 你可以使用这个:stripe.com/docs/api/invoices/upcoming
    • 好的,看来我可以将未来的计划传递为subscription_items。谢谢。
    猜你喜欢
    • 2019-06-21
    • 2018-10-20
    • 2021-11-19
    • 2019-02-28
    • 2021-12-17
    • 2017-08-20
    • 1970-01-01
    • 2020-10-19
    • 2020-04-05
    相关资源
    最近更新 更多