ruby-on-rails - 如何在 Stripe (Rails) 中创建费用和客户

标签 ruby-on-rails ruby stripe-payments

我正在构建一个基于订阅的应用程序,我想通过 Stripe 订阅向客户收费。提交表格后,我正在尝试创建客户和费用。但是,只创建代币,不创建费用和客户。所以表格成功通过,但在 Stripe 仪表板中,测试费用和客户不存在。这是我的 Controller :

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 key 添加到您的 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 支付系统的关联。

创建默认源

客户必须为订阅费用指定一个默认来源。这可以从 token 创建,如下所示:

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",
  :currency => "usd",
  :id => "purple"
)

您可以创建任意数量的计划,并可以为用户订阅他们喜欢的任何计划。

为客户创建订阅

此时,您可以在客户上创建订阅,这将启动计费流程。

Stripe::Subscription.create(
  :customer => customer,
  :plan => "purple"
)

设置网络钩子(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

从 Stripe 发送的事件类型记录在 Types of Events .您可以选择捕获和处理一些,同时让其他人通过。但是,在我的应用程序中,我发现最好捕获并记录所有事件,然后根据需要处理它们。这样,如果您错过了处理一个后来变得很重要需要处理的事件,您可以引用该事件并可以事后处理它。

收取定期付款

这是简单的部分,最好用您最喜欢的冷饮来完成。此时您需要做的就是监控 Stripe 控制台和您的银行帐户。无需额外操作,因为 Stripe 会处理其余的事情。

关于ruby-on-rails - 如何在 Stripe (Rails) 中创建费用和客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37242658/

相关文章:

iOS/Restkit应用程序设计

ruby-on-rails - 事件管理员日期过滤器日期格式自定义

ruby-on-rails - 从门卫那里获取 "The Authorization server does not support this response type"

ruby-on-rails - database.yml 代理?或等效的?

ruby-on-rails - 图像字段不显示在事件管理表单中

Ruby:如何从 json 格式中获取所有关键元素?

javascript - 动态下拉菜单 Ruby on Rails form_for

python - Stripe、PayPal、与 django-rest-framework 集成

ruby-on-rails - Active Admin 和 Stripe 成员(member)计划

javascript - 使用 Cucumber 测试时 Stripe JS 失败