ruby-on-rails – 如何在Stripe(Rails)中创建费用和客户
我正在构建一个基于订阅的应用程序,我想通过Stripe订阅向客户收费.我在提交表单后试图创建客户和收费.但是,只创建令牌,而不是收费和客户.因此表单成功完成,但在Stripe仪表板中,测试费用和客户不存在.这是我的控制器:
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 ) Stripe::Charge.create( :amount => 8999,:currency => "usd",:source => token,:description => "Example charge" ) current_user.subscribed = true current_user.stripeid = customer.id current_user.save redirect_to profiles_user_path end end 解决方法
所有这些都可以在优秀的
Ruby API Docs中找到.有几个步骤,但并不是那么难.可能需要一些实验才能使其在您的应用程序中运行.
看起来您正试图在订阅计划(使用计划:1212)上设置客户,因此我将解释订阅如何工作.我还会解释简单的一次性费用,以防万一你正在寻找的东西. 在您的应用程序中设置Stripe 将条带键添加到config / secrets.yml文件中: development: stripe_private_key: <%= ENV["STRIPE_PRIVATE_KEY"] %> stripe_public_key: <%= ENV["STRIPE_PUBLIC_KEY"] %> 您可以在您的环境中保留STRIPE_PRIVATE_KEY和STRIPE_PUBLIC_KEY.测试和生产环境需要类似的配置设置. 接下来,将此代码添加到您的BillingController,或者您计划使用Stripe API的任何位置: require "stripe" Stripe.api_key = Rails.application.secrets.stripe_private_key 添加迁移以向客户添加条带客户ID class AddUserStripeCustomerId < ActiveRecord::Migration def change change_table :users do |t| t.string :stripe_customer_id,limit: 50,null: true end end end 创建客户 当您准备开始为客户开具结算流程时,请执行以下操作: if !@user.stripe_customer_id @user.stripe_customer_id = Stripe::Customer.create( account_balance: 0,email: @user.email_canonical ) end 确保在用户模型中保存客户ID.您需要确保不会为用户创建和覆盖客户ID,因为这是您与该用户的Stripe支付系统的搭配. 创建默认源 客户必须具有为订阅费用分配的默认来源.这可以从令牌创建,如下所示: customer.sources.create({source: token_id}) 如果您已经为用户分配了卡片,则可以从客户现有的卡片中分配: customer.default_source = customer.sources.retrieve(card_id) 一次性充电 您可以向客户收取一次费用,而不会再次发生,您可以这样做: Stripe::Charge.create( :amount => 1395,# <== Currency in 'cents' :currency => "usd",:source => customer.default_source,# <== from previous section :description => "Fuzzy eyeglasses" ) 您应该捕获费用ID,但如果您以后碰巧需要它,您可以随时从Stripe中检索. 创建订阅计划 您可以在Stripe控制台上轻松创建订阅计划,因为这通常是一次性活动;构建用于管理订阅计划的UI几乎肯定是过度杀伤,除非您有管理员用户可以管理订阅计划,但不应该访问Stripe控制台. 要以编程方式创建订阅计划,请尝试以下操作: Stripe::Plan.create( :amount => 4200,#<== Amount is in cents,not dollars :interval => "month",:name => "Purple Plan",:id => "purple" ) 您可以根据需要创建任意数量的计划,并可以将用户订阅到他们喜欢的任何计划. 为客户创建订阅 此时,您可以在客户上创建订阅,这将启动结算过程. Stripe::Subscription.create( :customer => customer,:plan => "purple" ) 设置Web Hook接收器 出于某种原因,该文档位于不同的位置(参见Webhooks),但它是该过程中非常必要的部分.这将使您的应用程序得到通知 def PaymentController protect_from_forgery :except => :webhook def webhook # Capture the event information from the webhook params event_id = params[:event] # Verify that the event isn't forged to your Stripe account event = Stripe::Event.retrieve(event_id) # Record the event PaymentEvents.create!(event) # Handle the event in terms of your application #... end end 从条带发送的事件类型记录在Types of Events.您可以选择捕获和处理某些事件,同时让其他人通过.但是,在我的应用程序中,我发现最好捕获并记录所有事件,然后根据需要处理它们.这样,如果您错过了处理后来变得非常重要的事件,您可以引用事件并在事后处理它. 收取定期付款 这是一个简单的部分,最好用你最喜欢的冷饮来完成.从这一点开始,您需要做的就是监控Stripe控制台和您的银行帐户.无需其他操作,因为Stripe会处理其余的事情. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |