【问题标题】:Stripe Insufficient Funds: Redirect to new pageStripe 资金不足:重定向到新页面
【发布时间】:2021-01-06 15:51:29
【问题描述】:

在提交付款并重定向用户后,我无法尝试捕捉资金不足错误。现在,如果用户资金不足,网站会给出 500 错误。

在 PHP 中,错误日志是:

PHP 致命错误:未捕获的 Stripe\Error\InvalidRequest:必须提供来源或客户。在 /home/betheexe/public_html/_www/legalrideshare.com/stripe-php/lib/ApiRequestor.php:210 来自 API 请求“req_o4ePJL1WVn611Y”

这是我接受付款的代码:

结帐页面:

<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"                                                 
data-key="pk_live_XXXXXXXXXXXXXXXXXXX"
data-amount="2499"
data-name="LR"
data-description="Deactivation"
data-email="<?php echo $_SESSION["email"]; ?>"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto">
</script>

提交后:

\Stripe\Stripe::setApiKey("sk_live_XXXXXXXXXXXXXX");
// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];

$charge = \Stripe\Charge::create([
    'amount' => '2499',
    'currency' => 'usd',
    'description' => 'Deactivation Letter',
    'source' => $token,
]);

我只想捕捉发生的任何错误,以免页面失败。我也一定缺少来源吗?

【问题讨论】:

  • IT GIVE A 500 Error 它是条带服务器或您的服务器
  • 错误提示 Must provide source or customer.,我没有看到你在哪里告诉 Stripe WHO 给 Bill,最好先解决这个问题吗?
  • 对不起....我的服务器。

标签: php stripe-payments


【解决方案1】:

为了在收费失败时捕获任何错误,例如您从 Stripe 看到的错误,您需要将您的收费请求包装在一个 try/catch 块中。例如:

try {

  $charge = \Stripe\Charge::create([
    'amount' => '2499',
    'currency' => 'usd',
    'description' => 'Deactivation Letter',
    'source' => $token,
  ]);

} catch(\Stripe\Exception\CardException $e) {
  // Since it's a decline, \Stripe\Exception\CardException will be caught
  echo 'Status is:' . $e->getHttpStatus() . '\n';
  echo 'Type is:' . $e->getError()->type . '\n';
  echo 'Code is:' . $e->getError()->code . '\n';
  // param is '' in this case
  echo 'Param is:' . $e->getError()->param . '\n';
  echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $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
}

在每个 catch 块中,您可以添加逻辑以向用户返回有用的错误消息以及相关的后续步骤。 Stripe 文档在本节中提供了这些错误的完整示例以及如何处理它们:

https://stripe.com/docs/api/errors/handling?lang=php

您也可以使用以下测试卡/代币来测试资金不足的情况:

  • raw card number4000000000009995
  • test token stringtok_chargeDeclinedInsufficientFunds

【讨论】:

  • 这就是我要找的。谢谢!我试试看。
猜你喜欢
  • 2016-04-17
  • 2020-04-14
  • 2018-08-30
  • 2021-02-25
  • 2018-11-29
  • 2021-07-24
  • 2021-04-05
  • 2016-05-03
  • 2022-11-28
相关资源
最近更新 更多