如何解决未接收数据时出现致命错误而再次加载页面时发送数据条带? [英] How to solve fatal error when not receiving data and it sends data when loading the page again stripe?

查看:25
本文介绍了如何解决未接收数据时出现致命错误而再次加载页面时发送数据条带?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过条带发送卡的数据进行购买时,它向我显示以下消息:

When I send the data of the card to make the purchase by means of stripe it shows me the following message:

已成功收取 50 美元!

tok_1BKYfLDi3J7CIS2eVqZVeZqT

tok_1BKYfLDi3J7CIS2eVqZVeZqT

但是当您发送付款数据时没有收到数据显示这些错误

But when you do not receive data when sending the payment data shows these errors

注意: 未定义索引:第 9 行 C:\xampp\htdocs\DesignFormStripe\charge.php 中的令牌致命错误: 未捕获的 Stripe\Error\Card:无法向 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php:128 中没有活动卡的客户收费API 请求 'req_W6Qbd7iMVHDsGS' 堆栈跟踪:#0 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(102): Stripe\ApiRequestor::_specificAPIError('{\n "error": {\n...', 402, Array, Array, Array) #1 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(309): Stripe\ApiRequestor->handleErrorResponse('{\n "error": {\n...', 402, Array, Array) #2 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(65): Stripe\ApiRequestor->_interpretResponse('{\n "error": {\n...', 402, Array) #3 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiResource.php(119): Stripe\ApiRequestor->request('post', '/v1/charges', Array, Array) #4 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiResource.php(158): Stripe\ApiResource::_staticRequest('post', '/v1/charges', Array, NULL) #5 C:\xampp\htdocs\DesignFormStripe\php\lib\str in C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php 第 128 行

Notice: Undefined index: token in C:\xampp\htdocs\DesignFormStripe\charge.php on line 9 Fatal error: Uncaught Stripe\Error\Card: Cannot charge a customer that has no active card in C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php:128 from API request 'req_W6Qbd7iMVHDsGS' Stack trace: #0 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(102): Stripe\ApiRequestor::_specificAPIError('{\n "error": {\n...', 402, Array, Array, Array) #1 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(309): Stripe\ApiRequestor->handleErrorResponse('{\n "error": {\n...', 402, Array, Array) #2 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(65): Stripe\ApiRequestor->_interpretResponse('{\n "error": {\n...', 402, Array) #3 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiResource.php(119): Stripe\ApiRequestor->request('post', '/v1/charges', Array, Array) #4 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiResource.php(158): Stripe\ApiResource::_staticRequest('post', '/v1/charges', Array, NULL) #5 C:\xampp\htdocs\DesignFormStripe\php\lib\str in C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php on line 128

如何修复那些运行错误?

How can I fix those running errors?

处理错误上使用 Stripe 的文档修复以前的错误.

Fix previous errors with the documentation of Stripe, on handling errors.

代码差不多是这样解决的:

<?php
  require_once('./config.php');

  $token  = $_POST['token']; // retrieve token POST parameter to charge the card (stripeToken)


  $customer = \Stripe\Customer::create(array(
      'email' => 'customer2@example.com',
      //'email' => $_POST['stripeEmail'],
      'card'  => $token
  ));

try {
  // Use Stripe's library to make requests...

  $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => 500,
      'description' => 'Event charge',
      'currency' => 'usd'
  ));

  echo '<h1>Successfully charged $50!</h1>';
  echo "$token";

} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}
?>

现在在没有收到数据的情况下打开页面时会显示以下消息:

状态为:402 类型为:card_error 代码为:missing 参数为:card 消息为:无法向没有活动卡的客户收费

Status is:402 Type is:card_error Code is:missing Param is:card Message is:Cannot charge a customer that has no active card

现在是一个新问题 :(

但是当再次加载页面并确认表单重新提交时,它显示了与之前错误相同的消息.

But when loading the page again, and confirm form re-submission, it shows the same messages of previous errors.

我想象显示错误是因为Stripe只接受一次收费请求生成的Token.

I imagine that the error is shown because Stripe only accepts the Token generated at the request of the charge only once.

但在确认重新提交表单时不会显示这些错误,而是显示自定义错误消息,例如:付款请求已在处理中.

But instead of showing these errors when confirm form re-submission, show an custom error message, for example: The payment request is already in process.

推荐答案

这里的问题是您的代码期望卡片令牌 tok_XXXX 出现在 $_POST['token'] 中.似乎在某些情况下,提交表单时您的表单未发送参数并且您没有捕获它.

The issue here is that your code expects the card token tok_XXXX to be present in $_POST['token']. It seems that there are cases where the parameter is not sent by your form when the form is submitted though and you're not catching it.

因此,$token 是空的,根本不会发送到 Stripe,并且创建的客户没有卡.下一步是向客户收费,但由于客户没有卡,API 返回错误.

Because of this, $token is empty and simply not sent to Stripe and the customer is created without a card. The next step is to charge the customer but since it has no card the API returns an error.

您需要更改代码中的两件事来避免这种情况.首先,您应该确保按照 此处 中记录的那样捕获 Stripe API 可以返回的所有错误一>.这个想法是,您捕获异常,然后显示一条用户友好的错误消息,同时在您的终端记录详细错误以解决此问题.

You need to change two things in your code to avoid this. First off, you should make sure that you catch all errors that can be returned by Stripe's API as documented here. The idea is that you catch the exception and then you display a user-friendly error message while you log the detailed error on your end to solve this.

然后,您需要在代码的开头添加一些逻辑,以确保 $_POST['token'] 按预期设置.如果没有,请早点离开并向客户返回错误.

Then, you need to add some logic at the beginning of your code that will ensure that $_POST['token'] is set as expected. If not, leave early and return an error to the customer.

最后,问题通常来自客户端的错误,您的表单可以在没有先成功创建令牌的情况下提交.常见问题来自在创建令牌之前不禁用表单提交或在某些浏览器上未正确初始化 JS 代码.

Finally, the issue usually comes from a bug client-side where your form can be submitted without the token being created successfully first. Common issues come from not disabling form submission until a token is created or not properly initializing JS code on some browsers.

这篇关于如何解决未接收数据时出现致命错误而再次加载页面时发送数据条带?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆