【问题标题】:Automatically Setting a Devise User as a Stripe Customer自动将设计用户设置为 Stripe 客户
【发布时间】:2014-10-08 20:36:58
【问题描述】:

我的策略是这样的:

  1. 让用户注册
  2. 自动将该用户与 Stripe ID 相关联

我是如何尝试实现的:

我创建了一个用户控制器并让它继承自 RegistrationController:

devise_for :users, :controllers => { :registrations => 'users'}

我在保存用户后创建了一个回调:

after_save :set_stripe_customer_id

private
def set_stripe_customer_id
end

现在,我认为这里需要的是:

current_user.update_attribute(:stripe_id => ....

文档显示代码如下

Stripe::Customer.create(
  :description => "Customer for test@example.com",
  :card => "tok_1046el4BfU4hLNTvxYcIz4rE"
)

问题是,我不希望我的客户必须立即放入他的卡。虽然这没什么大不了的,但我可以等待要求注册直到实际购买点。我的问题是让我的回调自动创建 Stripe 客户所需的最简单、最少的代码是什么?

【问题讨论】:

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


    【解决方案1】:

    Stripe::Customer.create 的所有参数都是可选的,所以你可以做一些简单的事情:

    def set_stripe_customer_id
      customer = Stripe::Customer.create
      current_user.update_attributes :stripe_id => customer.id
    end
    

    然后再为该客户订阅,或根据需要更新各种详细信息。

    虽然我会说这不是必需的。等到您真正需要客户来制作它(例如,因为他们正在订阅),这并没有真正的缺点。

    【讨论】:

      猜你喜欢
      • 2015-04-08
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 2019-04-14
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多