ruby-on-rails - Stripe Ruby - 按 ID 查找现有客户并添加计划

标签 ruby-on-rails ruby stripe-payments

所以 Stripe 文档说:

Once you've created a customer, you should store its id in your own database so you can refer to it later when communicating with stripe.

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

customer = Stripe::Customer.retrieve(id)

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

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

最佳答案

寻找客户:

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 .

关于ruby-on-rails - Stripe Ruby - 按 ID 查找现有客户并添加计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26150133/

相关文章:

ruby-on-rails - 同一 rails 资源的多个 devise_for 路线

ruby - 将数组中的连续数字分组

ruby - 包装一个 Ruby 函数

javascript - 在我的 Rails 应用程序中使用 Stripe 为卡充电时出现问题

listview - 有没有办法在Flust中的LIstView.Builder中将纪元时间戳格式化为人类日期格式?

mysql - 在选择标签中显示当前用户的数据及其名称

ruby-on-rails - 如何创建自定义路由助手以供在 routes.rb 中使用

ruby - 使用send方法和dot方法的区别

javascript - Rails - 如何正确实现支付/预订流程

ruby-on-rails - Simplecov 覆盖率报告似乎遗漏了某些行