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

匿名 (未验证) 提交于 2019-12-03 01:17:01

问题:

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 waited many hours but I still get this error. How can I fix it?

回答1:

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.



回答2:

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'); 


回答3:

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();                 }             } 


回答4:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!