39

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?

Julien
  • 3,743
  • 9
  • 38
  • 67
  • Seems your account is not activated, or you are not using the correct secret key. Did you contact them? – RST Mar 09 '15 at 23:35
  • 2
    This is because you didn't change the Publishable API key `pk_live_XXX` and you are still using the test one `pk_test_YYY` – koopajah Mar 09 '15 at 23:51
  • thanks foryour reply, but my account is activated and I've changed both the secret and the Publishable API key – Julien Mar 10 '15 at 04:56
  • @Julien: The only way you get this token is if you don't set the correct publishable key or secret key in your code. I would advise you to make sure that the key you see in your HTML when creating the token is the correct one. – koopajah Mar 11 '15 at 15:04
  • Did you solve your issue as I have the same? – iamyojimbo Mar 20 '15 at 16:24
  • 3
    My issue was that I changed a config file that updated back and front end, but then didn't refresh the front end page... – iamyojimbo Mar 21 '15 at 12:01

6 Answers6

21

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.

Jake T.
  • 4,308
  • 2
  • 20
  • 48
  • 1
    my question is dated from one year for now, I forgot to close it, yes that was a stupid issue, I was using the test key – Julien May 24 '16 at 13:40
  • No worries. I ran into this issue several times during development haha. And even a long time after, when I duplicated prod servers to do some testing with more and real data, rather than the minimal test data I had available. Threw me off for a bit trying to figure out why none of my stripe stuff was working when I had the proper keys... it was because the prod data had customer ids created with a live key, of course, so I couldn't do any operations on it with my test keys. – Jake T. Jun 01 '16 at 17:53
  • This is the one. I had create a test user with the same details as the "live test" user. My keys were updated properly to the live set. The test user is hidden once you enable you account as well so you have to explicitly select `View test data` in the navigation on the left. – th3coop Mar 08 '21 at 06:56
  • Is there an easy way to do this where in your "test mode" you preserve data from live mode for testing purposes? – Taylor C. White Feb 24 '23 at 18:24
6

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');
devXen
  • 3,013
  • 3
  • 35
  • 44
5

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();
                }
            }
A.Aleem11
  • 1,844
  • 17
  • 12
1

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

Daniel Costa
  • 275
  • 2
  • 14
0

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.

Victor Karangwa
  • 1,679
  • 20
  • 16
0

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

Dash2TheDot
  • 109
  • 1
  • 5