Julien
Julien

Reputation: 4163

Stripe: No such token.. a similar object exists in test mode, but a live mode key was used to make this request

When using Stripe in live mode I get this PHP error:

No such token tok_fgfhn.. a similar object exists in test mode, but a live mode key was used to make this request

Everything works well in Stripe test mode, and and I've switched to a live API key.

I create a new customer like this:

$token  = $_POST['stripeToken'];
    $email  = $_POST['email'];

$customer = \Stripe\Customer::create(array(
      'email' => $email,
      'card'  => $token
    ));

    //charge for user ads
    $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'eur'
    ));

I've tested many hours but I still get this error. How can I fix it?

Upvotes: 42

Views: 52170

Answers (6)

Dash2TheDot
Dash2TheDot

Reputation: 157

Same issue (but with Django).

Solution for me: I was linking a customer ID to a membership model. However, I created the membership (storing the test customer ID with the membership), then switched to live mode. Obviously, setting the customer_id to None (or null, for PHP folks) fixed the isuse

Upvotes: 1

Victor Karangwa
Victor Karangwa

Reputation: 2216

I had the same issue. I was doing this mistake: When making some charges from user card I was using the live secret key while tokenizing user credit card details I was using the test secret key.

Then, I resolved it by using the live secret key on both making the payment from the user card and tokenizing user's credit card details.

Upvotes: 0

Daniel Costa
Daniel Costa

Reputation: 295

After spending some hours on it. I'm letting this here if it might help someone else:

I've an application deployed on Heroku with the secret and publishable key stored in environment variable on heroku.

I use <%= ENV.fetch('STRIPE_PU_KEY') %> in a .coffee.erb

Be aware if you change and restart your server it won't be enough. You will need to regenerate your application.js otherwise it will still take the catched value.

Hope it helps

Upvotes: 1

A.Aleem11
A.Aleem11

Reputation: 1974

You will have two different keys in your stripe account. Kindly make sure you've replace both test keys with live keys:

live sectret key: sk_live_00000000000000000000000

live publish key: pk_live_00000000000000000000000

1- Secret key will replace in all your php scripts where're charging

  \Stripe\Stripe::setApiKey("sk_live_00000000000000000000");

2- Publish key will replace in your .JS file through which you're validating your payment form this same file also creates token after successful validation. It may call stripe.js or may other name you need to locate this file it will have publish key that you need to replace from test to live:

 Stripe.setPublishableKey('pk_live_0000000000000'); //this would be publish key

            function stripeResponseHandler(status, response) { //token function
                if (response.error) {
                    // re-enable the submit button
                    $('.submit-button').removeAttr("disabled");
                    // show hidden div
                    document.getElementById('a_x200').style.display = 'block';
                    // show the errors on the form
                    $(".payment-errors").html(response.error.message);
                } else {
                    var form$ = $("#payment-form");
                    // token contains id, last4, and card type
                    var token = response['id'];
                    // insert the token into the form so it gets submitted to the server
                    form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
                    // and submit
                    form$.get(0).submit();
                }
            }

Upvotes: 5

devXen
devXen

Reputation: 3152

Look into the javascript that uses test public API key to retrieve token. Change it to your live public API key.

It should be something like this

Stripe.setPublishableKey('pk_test_axEdfdasdfasfsadfsad');

Upvotes: 6

Jake T.
Jake T.

Reputation: 4378

It sounds like you're trying to charge a customer who exists on your test account, not on your live account. Make sure you are making a new customer with your live keys and using their token to create the charge.

Upvotes: 26

Related Questions