jjjjjjjj
jjjjjjjj

Reputation: 4483

Rails/Stripe: No such token

I'm trying to create a one time, single charge in Stripe with Rails. I am getting the following error:

enter image description here

Stripe::InvalidRequestError (No such token: tok_18nnwSJ6tVEvTdcVs3dNIhGs)

However, as can clearly be seen in the photo, the token is in the parameters. That token is from Stripe.js.

Here is my code in my controller:

  Stripe.api_key = "xxxxxxxxxxx"
  customer = Stripe::Customer.create(source: params[:stripeToken])
  charge = Stripe::Charge.create({
  :amount => 10000, 
  :currency => "usd",
  :customer => customer.id,
  :description => "Example charge"
})

I have also tried:

  Stripe.api_key = "xxxxxxxxxxx"
  charge = Stripe::Charge.create({
  :amount => 10000, 
  :currency => "usd",
  :source => params[:stripeToken],
  :description => "Example charge"
})

And that does not work either. All of this is simple, boilerplate code straight from the Stripe site, any idea what I could be doing wrong? I'm not having any trouble with the Stripe embedded form.

Upvotes: 5

Views: 2644

Answers (2)

viola
viola

Reputation: 1

Hi I had the exact problem, and I am still new to web development, so not sure if my answer would help. At the end my problem was that I was not using the right Publishable key for my front end in the react component as I was just copying over the tutorial's key. But for backend I used my own secret key from Stripe account. Stripe didn't recognise as we need to use both our own Publishable key and Secret key because they are a set, connected. Hope it would be helpful.

Upvotes: 0

Hamza Khan
Hamza Khan

Reputation: 1481

I had been facing same issue for my test environment and the mistake i had been doing, i was adding the token received by Strip like this one tok_18nnwSJ6tVEvTdcVs3dNIhGs , for the test environment we have to use 'tok_visa' in source.

Here is the list of test sources provided by Stripe. https://stripe.com/docs/testing#cards

It created customer for me, let me know if it helped anyone else as well.

Upvotes: 4

Related Questions