【问题标题】:Stripe Ruby - Find existing customer by ID and add a planStripe Ruby - 按 ID 查找现有客户并添加计划
【发布时间】:2023-03-26 07:04:02
【问题描述】:

所以 Stripe 文档说:

一旦您创建了一个客户,您应该将其 id 存储在您自己的数据库中,以便您以后与 stripe 通信时可以参考它。

好的,很简单。我的问题是:如何通过 ID 找到客户? 像这样更新

customer = Stripe::Customer.retrieve(id)

当我为我的一项服务注册新客户时,我会创建一个新的 Stripe Customer,但如果他们是现有客户,我想使用他们现有的帐户并简单地添加一个计划。如何找到它们并添加新计划?我查看了文档和红宝石宝石,但无法弄清楚。请帮忙?这是有问题的方法。

def social_signup
    token = params[:stripe_token]
    if current_vendor.stripe_id == nil
        customer = Stripe::Customer.create(
            :card => token,
            :plan => 'social',
            :email => current_vendor.email
        )

        current_vendor.update_attributes(stripe_id: customer.id)
    else
        customer = Stripe::Customer.retrieve(current_vendor.stripe_id)
        # bill the customer's existing account for the added subscription
    end

    redirect_to :somewhere
end

【问题讨论】:

    标签: ruby-on-rails ruby stripe-payments


    【解决方案1】:

    寻找客户:

    customer = Stripe::Customer.retrieve(current_vendor.stripe_id) # pass in the persisted ID
    

    为计划创建新订阅:

    customer.subscriptions.create(plan: "social")
    

    文档非常详尽,只需要深入研究并找到细节即可。顺便说一句,为单个客户添加多个订阅是 Stripe added in February of this year 的一项功能。

    【讨论】:

    • 就我而言,我使用current_user.stripe_id 并且它有效。 Rails 不明白current_vendor 是什么。
    • @QuyLe 是的,您应该使用存储条带 ID 的对象/字段。 current_vendor 是我正在开发的应用程序中的一个示例——您的应用程序可能会有所不同。
    【解决方案2】:

    我相信这就是您在拥有客户后制定计划的方式。

    Stripe::Customer.create({
        :card => customer.id,
        :plan => your_plan,
        }, access_token
      )
    

    【讨论】:

    • 这仍然调用Stripe::Customer 上的创建,不幸的是创建了一个新客户。我找到了答案,请查看以下内容。此外,:card 应键入由Stripe.js 创建的令牌,而不是客户 ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2017-04-20
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    相关资源
    最近更新 更多