Stripe: 没有这样的令牌.. 测试模式下存在类似的对象,但使用实时模式密钥发出此请求

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

在实时模式下使用 Stripe 时出现此 PHP 错误:

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

在 Stripe 测试模式下一切正常,而且我已经切换到实时 API 键。

我这样创建一个新客户:

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

我已经测试了很多小时,但我仍然遇到此错误。我该如何解决?

听起来您正在尝试向存在于您的测试帐户而非真实帐户中的客户收费。确保您正在使用您的实时密钥创建新客户并使用他们的令牌创建费用。

查看使用测试 public API 密钥检索令牌的 javascript。将其更改为您的实时 public API 密钥。

应该是这样的

Stripe.setPublishableKey('pk_test_axEdfdasdfasfsadfsad');

您的 stripe 帐户中将有两个不同的密钥。请确保您已将两个测试密钥替换为实时密钥:

实时密钥:sk_live_00000000000000000000000

实时发布密钥:pk_live_00000000000000000000000

1- 密钥将替换所有正在收费的 php 脚本

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

2- 发布密钥将替换您用于验证付款表单的 .JS 文件,该文件也会在成功验证后创建令牌。它可能会调用 stripe.js 或者您需要找到此文件的其他名称,它将具有您需要从测试替换为实时的发布密钥:

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

在花了几个小时之后。如果它可能对其他人有帮助,我会把它放在这里:

我在 Heroku 上部署了一个应用程序,其中的秘密和可发布密钥存储在 heroku 的环境变量中。

我在 .coffee.erb

中使用 <%= ENV.fetch('STRIPE_PU_KEY') %>

请注意,如果您更改并重新启动服务器,这还不够。您将需要重新生成 application.js 否则它仍将采用捕获的值。

希望对您有所帮助

我遇到了同样的问题。我犯了这个错误:当从用户卡中收费时,我使用的是实时密钥,同时标记用户信用卡详细信息时,我使用的是测试密钥。

然后,我通过在从用户卡付款和标记用户信用卡详细信息时使用实时密钥解决了这个问题。