hujihuji
hujihuji

Reputation: 81

I get error that You cannot use a Stripe token more than once

I run my local server using stripe and I got error when I submitted my payment form.

class ChargesController < ApplicationController

  def create
   //below is the reason I got error for 
    customer = Stripe::Customer.create(
      :email => params[:stripeEmail],
      :source  => params[:stripeToken]
    )

    charge = Stripe::Charge.create(
      :customer    => customer.id,
      :amount      => params[:amount],
      :description => 'Growth Hacking Crash Course',
      :currency    => 'jpy'
    )

    purchase = Purchase.create(email: params[:stripeEmail], card: params[:stripeToken], amount: params[:amount], description: charge.description, currency: charge.currency, customer_id: customer.id, product_id: 1, uuid: SecureRandom.uuid)

    redirect_to purchase

  rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to new_charge_path
  end


end

I don't know why I get that error. I need your help!!!

Upvotes: 0

Views: 4874

Answers (1)

Ywain
Ywain

Reputation: 17505

Your code is correct. However, this error message means that the card token you're trying to use in your customer creation request (in the params[:stripeToken] variable) has already been used.

Stripe tokens can only be used in a single request. Afterwards, the token is consumed and cannot be used again.

You'd have to create a new card token, using Checkout or Stripe.js, or directly by using the create token API endpoint.

Upvotes: 2

Related Questions