Reputation: 3527
I am trying to create a customer and charge that customer using Rails and Stripe. The customer is getting created in Stripe, but I keep getting the error Cannot charge a customer that has no active card
when trying to do the charge.
I am following a stripe tutorial as a Rails beginner, so I'm not sure where to go from here. Any help would be greatly appreciated.
Rails code:
def process_payment
customer = Stripe::Customer.create email: email,
card: card_token
Stripe::Charge.create customer: customer.id,
amount: level.price*100,
description: level.name,
card: card_token,
currency: 'usd'
end
def create
@registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"],
card_token: stripe_params["stripeToken"])
raise "Please, check registration errors" unless @registration.valid?
@registration.process_payment
@registration.save
redirect_to @registration, notice: 'Registration was successfully created.'
end
Upvotes: 8
Views: 20094
Reputation: 21
Seems like you are following a Udemy course. You'll have a JavaScript file for token creation that actually is not executing at the time of the signup click, so no token is being generated.
Include that JavaScript file in layout/application file and then make sure your jQuery is working perfectly, otherwise rewrite that code totally into vanilla JavaScript instead of using the jQuery method.
The rest of the code is working perfect on the Ruby side.
Upvotes: 1
Reputation: 1
I think we can create a checkout session and add the card to the customer. This will trigger a checkout path to the Stripe API, and you can take the card token from there by passing the customer. After you can create charge.
session = Stripe::Checkout::Session.create(
customer: customer,
payment_method_types: ['card'],
line_items: [{
name: product.product_name,
amount: product.price.to_i,
currency: 'usd',
quantity: 1
}],
mode: 'payment',
success_url: product_url(product),
cancel_url: product_url(product)
)
redirect_to session.url
Hope this can help anyone with this issue.
Upvotes: 0
Reputation: 1
What you are trying to do is charge a customer once you have created a payment method for her. However, you cannot use the Charge
endpoint to do that if you have not set any subscriptions because this method is made for recurring payments linked to subscriptions.
In order to be able to charge your client, set a subscription. This will activate the card. You'll then be able to debit the card.
Upvotes: 0
Reputation: 1
You're trying to access the parameters, so you must use the params
keyword.
Instead of card_token
, use params["card_token"]
Upvotes: -4
Reputation: 9692
Try creating the charge without the card_token. You shouldn't need to specify the card a second time, since the card is attached to the customer, which you're specifying though the customer
parameter.
customer = Stripe::Customer.create email: email,
source: card_token
Stripe::Charge.create customer: customer.id,
amount: level.price*100,
description: level.name,
currency: 'usd'
Also, pass the card_token
through the source
param, rather than the deprecated card
param. Here is a blog post that talks about this change: https://stripe.com/blog/unifying-payment-types-in-the-api
More info: https://stripe.com/docs/api/ruby#create_customer and: https://stripe.com/docs/api#create_charge
Upvotes: 5