【问题标题】:Add stipe stripe subscriptions for different projects of each user为每个用户的不同项目添加条带订阅
【发布时间】:2021-08-10 06:59:55
【问题描述】:

我正在处理的用例很常见,但我需要一些建议来可视化它。 每个用户可以有多个可以订阅的项目。

例如,用户名下有两个项目,项目 X 和项目 Y。现在每个项目都有自己的订阅。

对于每个项目特定的付款,我如何标记客户 -> 项目 -> 订阅?

我可以使用订阅标记客户,但不确定如何将订阅标记到项目。

我在想类似的事情

  1. 在创建用户时,添加客户。
  2. 在项目创建中添加带有价格的产品。
  3. 结帐
  4. 会话
  5. 订阅/结帐完成
  6. 更新数据库

我看到的这个问题,如果我更改价格计划,我将不得不在所有地方进行更新。 :(

实现这一目标的最佳/其他替代方案是什么? 为此,我正在使用 Nextjs 和 Supbase。按照这个例子。 https://github.com/vercel/nextjs-subscription-payments

【问题讨论】:

  • 我已经创建了 smilar 应用程序。你能更具体点if I change the price plans, I will have to update in all the places吗?
  • 非常感谢您的回复。因此,例如supabase.io/pricing 他们按项目/每个用户或团队计费。也有现收现付。我有完全相同的东西。现在我想像上面评论的那样做,我在想如果我必须改变定价,比如说 25 美元到 30 美元,我必须去改变每个项目的价格(这是我描述的产品更多)。我猜可能无法想象这是如何工作的。
  • 我的理解是,你先创建多个订阅计划,然后用户可以每个项目订阅一个计划,用户可以创建多个项目,对吧?我很困惑if I had to change the pricing。这意味着你更新条带价格? Stripe 价格无法通过 api 更新金额。您必须创建新的条带价格,并且可以使用 lookup_key 和 transfer_lookup_key 从产品中引用。
  • 如果我的理解是正确的,您可以通过使用 price.lookup_key 的单个产品和 mliti 价格模式来实现这一点,这样您就可以从条带产品中参考最新的项目图片。当然,为了更新已订阅用户的计费金额,您需要通知他们并以编程方式更改他们的条带价格
  • if I had to change the pricing,是的,我的意思是更新。你说的很有道理。

标签: next.js stripe-payments supabase


【解决方案1】:

首先,您应该为您的订阅计划创建一个 product 和一些 pricesprice 代表您的订阅计划的实体。

lookup_key 用于从静态字符串中动态检索价格,如果 transfer_lookup_key 设置为 true,则会自动从现有价格中删除查找键,并将其分配给该价格。因此,您始终可以使用lookup_key 并设置transfer_lookup_key true 来检索计划的最新价格。

const product = await stripe.products.create({
  name: 'MyService', // your service name
});

const beginnerPrice = await stripe.prices.create({
  unit_amount: 5,
  currency: 'usd',
  recurring: {interval: 'month'},
  product: product.id,
  lookup_key: 'beginner',
  transfer_lookup_key: true,
});

const proPrice = await stripe.prices.create({
  unit_amount: 20,
  currency: 'usd',
  recurring: {interval: 'month'},
  product: product.id,
  lookup_key: 'pro',
  transfer_lookup_key: true,
});

这是我假设的 db 架构。

// db schema

interface IUser{
  id: string
  stripeCustomerId: string
}

interface IProject{
  id: string
  userId: string
}

interface IProjectSubscription{
  id: string
  projectId: string
  stripeSubscriptionId: string // or/and stripeCheckoutSessionId, it's up to your use case
}

当用户创建新项目并选择他/她的订阅计划时,您将创建新的checkout.session 并通过price 传递相应的行项目。您可以使用lookup_key获取所选计划的当前price

const prices = await stripe.prices.list({
  lookup_keys: ['pro'],
  type: 'recurring',
  limit: 1,
});

const session = await stripe.checkout.sessions.create({
  success_url: 'https://example.com/success',
  cancel_url: 'https://example.com/cancel',
  payment_method_types: ['card'],
  line_items: [
    {price: prices[0].id, quantity: 1},
  ],
  mode: 'subscription',
});

然后在checkout.session.completed 上,您可以接收checkout.session 对象并在webhook 中迁移您的数据库。

接下来,假设您要将“pro”计划的价格从 20 美元更改为 30 美元。在这种情况下,您将创建具有相同lookup_key 的新price。通过这样做,您可以更改新订阅者的订阅价格,而无需更改向现有订阅者收取的金额。

const newProPrice = await stripe.prices.create({
  unit_amount: 30,
  currency: 'usd',
  recurring: {interval: 'month'},
  product: product.id,
  lookup_key: 'pro',
  transfer_lookup_key: true,
});

【讨论】:

  • 这太棒了❤️ 但是我看不到您将 subscriptionId 与 IProjectSubscription 的项目 ID 链接到哪里?
  • @SubhenduKundu 创建checkout.session 对象时,在元数据中设置projectId。可以在 webhook 的 checkout.session.completed 事件中检索到带有元数据的 checkout.session 对象,并且该对象现在具有条带订阅 ID。 stripe.com/docs/webhooks
  • 我做对了,只是元数据是我需要的。
【解决方案2】:

您可以使用metadata 来“标记” Stripe 中的事物以映射到数据模型中的事物。

如果您想更改价格,是的,您必须更新所有订阅。相反,您可能希望查看使用数量或计量计费。 https://stripe.com/docs/billing/subscriptions/model

【讨论】:

  • 你是对的。我需要的是元数据。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 2021-11-29
  • 2021-09-04
  • 2018-09-20
  • 1970-01-01
相关资源
最近更新 更多