Mar*_* I. 4 ruby ruby-on-rails stripe-payments
我正在构建一个基于订阅的应用程序,我想通过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
Run Code Online (Sandbox Code Playgroud)
Mic*_*ill 13
所有这些都可以在优秀的Ruby API Docs中找到.涉及几个步骤,但并不是那么难.可能需要一些实验才能使其在您的应用程序中运行.
看起来您正试图在订阅计划(使用plan: 1212)上设置客户,因此我将解释Subscription的工作原理.我还会解释简单的一次性费用,以防万一你正在寻找的东西.
将条带键添加到您的config/secrets.yml文件:
development:
stripe_private_key: <%= ENV["STRIPE_PRIVATE_KEY"] %>
stripe_public_key: <%= ENV["STRIPE_PUBLIC_KEY"] %>
Run Code Online (Sandbox Code Playgroud)
您可以在您的环境中保留STRIPE_PRIVATE_KEY和STRIPE_PUBLIC_KEY.测试和生产环境需要类似的配置设置.
接下来,将此代码添加到您BillingController或您计划使用Stripe API的任何位置:
require "stripe"
Stripe.api_key = Rails.application.secrets.stripe_private_key
Run Code Online (Sandbox Code Playgroud)
class AddUserStripeCustomerId < ActiveRecord::Migration
def change
change_table :users do |t|
t.string :stripe_customer_id, limit: 50, null: true
end
end
end
Run Code Online (Sandbox Code Playgroud)
当您准备开始为客户开具结算流程时,请执行以下操作:
if !@user.stripe_customer_id
@user.stripe_customer_id = Stripe::Customer.create(
account_balance: 0,
email: @user.email_canonical
)
end
Run Code Online (Sandbox Code Playgroud)
确保在用户模型中保存客户ID.您需要确保不会为用户创建和覆盖客户ID,因为这是您与该用户的Stripe支付系统的搭配.
客户必须具有为订阅费用分配的默认来源.这可以从令牌创建,如下所示:
customer.sources.create({source: token_id})
Run Code Online (Sandbox Code Playgroud)
如果您已经为用户分配了卡片,则可以从客户现有的卡片中分配:
customer.default_source = customer.sources.retrieve(card_id)
Run Code Online (Sandbox Code Playgroud)
您可以向客户收取一次费用,而不会重复出现,您可以这样做:
Stripe::Charge.create(
:amount => 1395, # <== Currency in 'cents'
:currency => "usd",
:source => customer.default_source, # <== from previous section
:description => "Fuzzy eyeglasses"
)
Run Code Online (Sandbox Code Playgroud)
您应该捕获费用ID,但如果您以后碰巧需要它,您可以随时从Stripe中检索.
您可以在Stripe控制台上轻松创建订阅计划,因为这通常是一次性活动; 构建用于管理订阅计划的UI几乎肯定是过度杀伤,除非您有管理员用户可以管理订阅计划,但不应该访问Stripe控制台.
要以编程方式创建订阅计划,请尝试以下操作:
Stripe::Plan.create(
:amount => 4200, #<== Amount is in cents, not dollars
:interval => "month",
:name => "Purple Plan",
:currency => "usd",
:id => "purple"
)
Run Code Online (Sandbox Code Playgroud)
您可以根据需要创建任意数量的计划,并可以将用户订阅到他们喜欢的任何计划.
此时,您可以在客户上创建订阅,这将启动结算过程.
Stripe::Subscription.create(
:customer => customer,
:plan => "purple"
)
Run Code Online (Sandbox Code Playgroud)
出于某种原因,此文档位于不同的位置(请参阅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
Run Code Online (Sandbox Code Playgroud)
从Stripe发送的事件类型记录在事件类型中.你可以选择捕捉和处理一些,同时让别人通过.但是,在我的应用程序中,我发现最好捕获并记录所有事件,然后根据需要处理它们.这样,如果您错过了处理后来变得非常重要的事件,您可以引用事件并在事后处理它.
这是一个简单的部分,最好用你最喜欢的冷饮来完成.从这一点开始,您需要做的就是监控Stripe控制台和您的银行帐户.无需其他操作,因为Stripe会处理其余的事情.
| 归档时间: |
|
| 查看次数: |
4900 次 |
| 最近记录: |