Reputation: 2046
For a point of reference, I've been following this guide on Stripe for Ruby on Rails. After completing it, I'm receiving this error:
Cannot charge a customer that has no active card
I'm now at my wits end...
I have stripe in my bundle. I ran rails g controller payments
.
This is the payments controller I have set up:
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
is a method inside the application controller:
def session_check
redirect_to login_path unless current_user
end
In the routes I have resources :payments
.
This is what I have in 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]
Within payments/new.html.erb
we have:
<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>
And payments/create.html.erb
is pretty simple. Stripe default:
<h2> Thanks, you paid <strong>$5.00</strong>!</h2>
Key Question: Why am I receiving Cannot charge a customer that has no active card
when I process a test credit card?
Upvotes: 3
Views: 1680
Reputation: 705
I'm not an expert on this, but I do have a working app with String and in my Create method we have this line:
:card => params[:stripetoken]
in the Stripe::Charge, and not in the Stripe::Customer Which that makes sense to me, Customer can probably have more than one card, but each charge must be provided a unique card.
Here's what I have in my Payment model
def save_with_payment(payment)
if valid?
Stripe::Charge.create(
:amount => payment.amount*100, #amount in cents
:currency => "usd",
:card => stripe_card_token,
:description => "description of payment");
save
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
Upvotes: 2