【问题标题】:How to save customer card updates in Stripe, Rails?如何在 Stripe、Rails 中保存客户卡更新?
【发布时间】:2016-09-23 06:54:36
【问题描述】:

我希望客户能够在我的 Rails 应用中更新他们的信用卡详细信息。 Stripe 有关于如何实现这一点的文档,但文章显示了一个 PHP 示例,但我需要一个 Rails 示例:https://stripe.com/docs/recipes/updating-customer-cards

基本上,我需要保存客户的信用卡而不收取任何费用。

这是subscribers_controller.rb

class SubscribersController < ApplicationController        
  before_filter :authenticate_user!

  def new
  end

  def update
    token = params[:stripeToken]

    customer = Stripe::Customer.create(
      card: token,
      plan: 1212,
      email: current_user.email
    )

    current_user.subscribed = true
    current_user.stripeid = customer.id
    current_user.save

    redirect_to profiles_user_path
  end
end

【问题讨论】:

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


【解决方案1】:

您可能还想查看这个 SO 答案 How to create a charge and a customer in Stripe ( Rails),了解有关在 Rails 应用程序中使用 Stripe 的更多详细信息。

对于 Ruby 文档,您可以在 Stripe Ruby API 上找到很好的示例。在 Stripe 术语中,一张卡被称为客户的source。您可以从token 创建source,但一旦创建,您将处理客户对象上的sourcedefault_source 元素,并从客户的source 检索card 对象。另请注意,除了创建source(或一次性收费)之外,您永远不应尝试使用token

Stripe Ruby API for Customers 表示可以同时创建customer 并分配source

customer = Stripe::Customer.create(
    source: token,
    email: current_user.email
)

不必必须分配source 来创建客户。但是,如果您为客户设置订阅,他们将需要source 可用,并且费用将计入客户的default_source。如果客户只有一个source,则自动为default_source

Stripe Ruby API for Cards,表明您还可以使用令牌向现有客户添加新卡:

customer = Stripe::Customer.retrieve(customer_id)
customer.sources.create({source: token_id})

一旦您为客户分配了一张卡片,您就可以将其设为default_source,使用以下命令:

customer.default_source = customer.sources.retrieve(card_id)

这就是设置并准备开始向客户收费的过程。结算愉快!

【讨论】:

  • 你需要在使用赋值运算符后调用customer.save,我相信
【解决方案2】:

要为现有客户更新卡,您提到的 PHP 配方中的相关 sn-p 是:

$cu = \Stripe\Customer::retrieve($customer_id); // stored in your application
$cu->source = $_POST['stripeToken']; // obtained with Checkout
$cu->save();

在 Ruby 中,这将是:

cu = Stripe::Customer.retrieve(customer_id)
cu.source = params[:stripeToken]
cu.save

这将使用 stripeToken 参数中包含的令牌中的卡更新现有客户。

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 2014-09-24
    • 2015-06-27
    • 2018-09-11
    • 2020-05-26
    • 2021-04-25
    • 2018-05-26
    • 2014-02-20
    • 1970-01-01
    相关资源
    最近更新 更多