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

6

42

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?

Culp answered 9/3, 2015 at 22:34 Comment(6)
Seems your account is not activated, or you are not using the correct secret key. Did you contact them?Deucalion
This is because you didn't change the Publishable API key pk_live_XXX and you are still using the test one pk_test_YYYLedge
thanks foryour reply, but my account is activated and I've changed both the secret and the Publishable API keyCulp
@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.Ledge
Did you solve your issue as I have the same?Chura
My issue was that I changed a config file that updated back and front end, but then didn't refresh the front end page...Chura
G
26

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.

Gerrigerrie answered 28/10, 2015 at 18:51 Comment(6)
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 keyCulp
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.Gerrigerrie
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.Kidderminster
Is there an easy way to do this where in your "test mode" you preserve data from live mode for testing purposes?Neel
@TaylorC.White A little late here, but not something that is automatically maintained without a lot of custom configuration and set up. You may already have that if you frequently use prod data on your test server. You could export prod data, and set up an import method to your test server that will bring in all data except the customer id. Then have a background function that'll go through and create new customer ids for anyone that is missing it.Gerrigerrie
9 years later and you still reply! I appreciate it! I (sadly) figured as much. Thanks for the info.Neel
D
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');
Dylane answered 23/5, 2016 at 14:51 Comment(0)
S
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();
                }
            }
Site answered 10/1, 2017 at 16:17 Comment(0)
T
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

Tercet answered 11/4, 2018 at 10:55 Comment(0)
V
1

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

Vip answered 23/7, 2022 at 19:1 Comment(0)
P
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.

Picul answered 17/12, 2020 at 18:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.