如何在 Stripe 中保存客户卡更新,Rails?
How to save customer card updates in Stripe, Rails?
我希望客户能够在我的 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
要为现有客户更新卡片,您提到的 PHP 食谱中的相关片段是:
$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
参数中包含的令牌中的卡更新现有客户。
您可能还想查看此 SO 答案 ,了解有关在 Rails 应用程序中使用 Stripe 的更多详细信息。
对于 Ruby 文档,您可以在 Stripe Ruby API 上找到很好的示例。在 Stripe 术语中,一张卡片对于客户来说称为 source
。您可以从 token
创建 source
,但是创建后,您将处理 Customer 对象上的 source
和 default_source
元素,并检索 card
对象来自客户的 source
。另请注意,除了创建 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)
这就是设置和准备开始向客户收费所需要的。开单愉快!
我希望客户能够在我的 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
要为现有客户更新卡片,您提到的 PHP 食谱中的相关片段是:
$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
参数中包含的令牌中的卡更新现有客户。
您可能还想查看此 SO 答案
对于 Ruby 文档,您可以在 Stripe Ruby API 上找到很好的示例。在 Stripe 术语中,一张卡片对于客户来说称为 source
。您可以从 token
创建 source
,但是创建后,您将处理 Customer 对象上的 source
和 default_source
元素,并检索 card
对象来自客户的 source
。另请注意,除了创建 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)
这就是设置和准备开始向客户收费所需要的。开单愉快!