[英]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
您可能還想查看此SO答案如何在Stripe(Rails)中創建費用和客戶,以獲取有關在Rails應用程序中使用Stripe的更多詳細信息。
對於Ruby文檔,您可以在Stripe Ruby API上找到很好的示例。 在Stripe術語中,卡被稱為客戶的source
。 您可以從token
創建source
,但是一旦創建它,您就可以處理Customer對象上的source
和default_source
元素,並從客戶的source
檢索card
對象。 另請注意,除了創建source
(或一次性費用)之外,您不應該嘗試使用token
。
客戶的Stripe Ruby API表明您可以創建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)
這就是獲得設置並准備開始為客戶充電所需的條件。 快樂結算!
要更新現有客戶的卡,您提到的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
參數中包含的令牌使用卡更新現有客戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.