【发布时间】:2014-04-28 14:19:08
【问题描述】:
作为参考,我一直在关注 this guide 的 Stripe for Ruby on Rails。完成后,我收到此错误:
Cannot charge a customer that has no active card
我现在已经不知所措了......
我的包裹中有条纹。我跑了rails g controller payments。
这是我设置的支付控制器:
class PaymentsController < ApplicationController
before_action :session_check
def index
redirect_to new_payment_path
end
def new
end
def create
@amount = 900
customer = Stripe::Customer.create(
:email => current_user.email,
:card => params[:stripetoken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'cad'
)
redirect_to account_path
rescue Stripe::CardError => e
render text: e.message
end
end
:session_check 是应用控制器内部的一个方法:
def session_check
redirect_to login_path unless current_user
end
在我有resources :payments的路线中。
这就是我在config/initializers/stripe.rb 中所拥有的:
# For Runnning on Development or Test Environments:
# sandbox number is 4242 4242 4242 4242
# any three digit CVC
# expiry date must be in future
Rails.configuration.stripe = {
:publishable_key => Rails.env.production? ? ENV['PUBLISHABLE_KEY'] : "pk_test_long_hash",
:secret_key => Rails.env.production? ? ENV['SECRET_KEY'] : "sk_test_another_long_hash"
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
在payments/new.html.erb 内我们有:
<h1 class="text-center"> Upgrade Today </h1>
<div class="row">
<div class="container">
<div class="text-center">
<%= form_tag payments_path do %>
<article>
<label class="amount">
<span>Amount: $9.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-name="Site Using Stripe"
data-description="One Month Subscription"
data-amount="900"
data-currency="cad"
data-email="<%= current_user.email %>"
data-allow-remember-me="true"></script>
<% end %>
</div>
</div>
</div>
payments/create.html.erb 非常简单。条纹默认:
<h2> Thanks, you paid <strong>$5.00</strong>!</h2>
关键问题:为什么我在处理测试信用卡时会收到Cannot charge a customer that has no active card?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 stripe-payments