如何在 Stripe 中创建费用和客户 (Rails)
How to create a charge and a customer in 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 中找到。涉及几个步骤,但并不难。可能需要进行一些试验才能使其在您的应用程序中正常工作。
您似乎在尝试为客户设置订阅计划(使用 plan: 1212
),所以我将解释订阅的工作原理。我还将解释简单的一次性收费,以防您正在寻找。
在您的应用程序中设置 Stripe
将 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
添加迁移以将 Stripe 客户 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 控制台上轻松创建订阅计划,因为这通常是一次性的 activity;构建一个 UI 来管理订阅计划几乎肯定是矫枉过正,除非您有可以管理订阅计划但无权访问 Stripe 控制台的管理员用户。
要以编程方式创建订阅计划,试试这个:
Stripe::Plan.create(
:amount => 4200, #<== Amount is in cents, not dollars
:interval => "month",
:name => "Purple Plan",
:currency => "usd",
:id => "purple"
)
您可以创建任意数量的计划,并可以为用户订阅他们喜欢的任何计划。
为客户创建订阅
此时,您可以在客户上创建订阅,这将启动计费过程。
Stripe::Subscription.create(
:customer => customer,
:plan => "purple"
)
设置网络挂钩接收器
出于某种原因,此文档位于不同的位置(请参阅 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
从 Stripe 发送的事件类型记录在 Types of Events。您可以选择捕获和处理一些,同时让其他人通过。但是,在我的应用程序中,我发现最好捕获并记录所有事件,然后根据需要处理它们。这样,如果您错过了处理一个后来变得很重要的事件,您可以参考该事件并处理它 post hoc.
收取定期付款
这是简单的部分,最好用您最喜欢的冷饮来完成。此时您需要做的就是监控 Stripe 控制台和您的银行帐户。无需额外操作,因为 Stripe 会处理剩下的事情。
我正在构建一个基于订阅的应用程序,我想通过 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 中找到。涉及几个步骤,但并不难。可能需要进行一些试验才能使其在您的应用程序中正常工作。
您似乎在尝试为客户设置订阅计划(使用 plan: 1212
),所以我将解释订阅的工作原理。我还将解释简单的一次性收费,以防您正在寻找。
在您的应用程序中设置 Stripe
将 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
添加迁移以将 Stripe 客户 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 控制台上轻松创建订阅计划,因为这通常是一次性的 activity;构建一个 UI 来管理订阅计划几乎肯定是矫枉过正,除非您有可以管理订阅计划但无权访问 Stripe 控制台的管理员用户。
要以编程方式创建订阅计划,试试这个:
Stripe::Plan.create(
:amount => 4200, #<== Amount is in cents, not dollars
:interval => "month",
:name => "Purple Plan",
:currency => "usd",
:id => "purple"
)
您可以创建任意数量的计划,并可以为用户订阅他们喜欢的任何计划。
为客户创建订阅
此时,您可以在客户上创建订阅,这将启动计费过程。
Stripe::Subscription.create(
:customer => customer,
:plan => "purple"
)
设置网络挂钩接收器
出于某种原因,此文档位于不同的位置(请参阅 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
从 Stripe 发送的事件类型记录在 Types of Events。您可以选择捕获和处理一些,同时让其他人通过。但是,在我的应用程序中,我发现最好捕获并记录所有事件,然后根据需要处理它们。这样,如果您错过了处理一个后来变得很重要的事件,您可以参考该事件并处理它 post hoc.
收取定期付款
这是简单的部分,最好用您最喜欢的冷饮来完成。此时您需要做的就是监控 Stripe 控制台和您的银行帐户。无需额外操作,因为 Stripe 会处理剩下的事情。