- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在构建一个基于订阅的应用程序,我想通过 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 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
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"
)
出于某种原因,此文档位于不同的位置(请参阅 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/
我有一个问题,多次执行 ActorSystem system = ActorSystem.create("System"); 是否会提高生产力,或者我必须执行一次并使用一个引用? 最佳答案 您应该仅创
我希望学生能够在收到付款请求之前访问 Moodle 中的前几节课。我在 Google 上搜索了好久,但一无所获,还搜索了我在这里能想到的所有可能的术语,但还是没有找到。因此,对于缺乏贡献的研究/证据,
有什么方法可以用 PHP 分摊 paypal 费用吗?我希望 90% 转到一个帐户,10% 转到另一个帐户,但是当向此人收费时,它看起来应该只是转到 10% 的帐户。我希望使用 IPN 向此人收费。无
在使用 Adaptive Payment API 完成链式或并行支付的情况下,我有一个关于 Paypal 如何计算适用于每个接收方的费用的答案。 费用是按发件人支付的总金额计算的吗?在这种情况下,每个
SINCH 是否会对双方(调用者和接收者)的通信收费? 正如我所见,如果我使用 SINCH 执行应用到应用的通话,那么我的通话时间将从通话双方的通话时间中扣除。 我知道 sinch 允许 2500 分
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭13 年前。 Improve th
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我一直在尝试将 hr_expenses 表单中的某些字段复制到我的自定义模块purchase_orders 中。 下面的方法是为我在扩展的 hr_expenses.py 文件中执行此操作而编写的: d
Paypal 费用是否适用于其 API 内的任何调用或特定调用? 例如,如果我在/billing-agreements api 中使用任何调用,我是否会被收取 2.9% 或更少的费用,外加每笔交易 0
在 Woocommerce 中,我们试图在通过 Paypal 支付网关购买商品时向订单添加额外费用。 我们通过这种方式更改了发送到 Paypal 的价格: add_filter('woocommerc
我正在尝试设置 Stripe Connect 并且需要 首先通过创建客户向买家收费, 然后生成一个token,最后 使用此 token 向客户收费。 只要买家和卖家不是 Stripe Connect
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。 Improve this
使用 .NET 库如何确定查询的 RU 费用。它返回 IQueryable,我不知道如何记录它。如何记录所有请求的 RU 的奖励积分。 简单的代码但不返回 RU: var docs = Docume
我有一个有一些购买选项的应用程序。但是,我不想通过应用程序本身来实现它。为此,我已经有一个购买网站。 那么我可以创建一个从我的应用程序到相应网页的重定向页面吗? 我的应用程序会被拒绝吗? 或者有更好的
我正在尝试弄清楚如何(粗略地)计算我正在考虑创建的某些工作角色的预期成本。 TLDR; 如果您的工作人员Sleep(someTime)/Sleeping,azure 是否会向您收取 $$ 费用? 详细
我是 Lambda 的新手,正在尝试了解如何在 Lambda 函数中处理 Stripe charge。 我的应用程序从 Stripe API 创建 token ,现在我需要将该 token 发送到 L
我为管理用户使用easyui表,我正确添加了所有脚本,但是与jquery.easyui.min.js发生冲突我尝试消除所有js以查看是否有效,但是,它不起作用,所以不是js冲突。 有人遇到过类似的问题
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
有谁知道是否有公开可用的 PayPal 资源可以让我在我的 Magento 代码中计算交易费用? 例如,如果有人以 1.00 美元的价格购买东西,我想知道其中的 0.15 美元是否是 PayPal 费
我只是想知道是否有办法检索 strip 中多个 charges_id 的费用。 例如在 docs展示如何获得一次充电。但是我们需要多次收费。所以,我们不想多次调用 stripe 方法 retrieve
我是一名优秀的程序员,十分优秀!