Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe: Cannot charge a customer that has no active card

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

enter image description here

like image 893
Trey Copeland Avatar asked Jul 19 '15 22:07

Trey Copeland


1 Answers

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

like image 126
Ryenski Avatar answered Sep 30 '22 04:09

Ryenski