【问题标题】:Catching Errors with Stripe PHP使用 Stripe PHP 捕获错误
【发布时间】:2019-08-14 17:52:01
【问题描述】:

我有一个用于捐赠的自定义 PHP Stripe 代码。

我在捕获被拒绝的卡片或一般错误时遇到问题(例如,当用户尝试加载卡片处理 PHP 页面时,该页面应重定向到错误页面)。

捐赠.PHP

//Create Customer
$customer = \Stripe\Customer::create(array(
    "email" => $email,
    "source"  => $token
));

try {
    //Charge the Card
    $charge = \Stripe\Charge::create(array(
        "customer" => $customer->id,
        "amount" => $totalAmount,
        "currency" => "usd",
        "description" => "Name: $fname $lname For: Donation",
        "statement_descriptor" => "DONATION",
    ));

} catch (\Stripe\Error\InvalidRequest $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

} catch (\Stripe\Error\Base $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

} catch (Exception $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

} catch(\Stripe\Error\Card $e) {
    // Since it's a decline, \Stripe\Error\Card will be caught
    $body = $e->getJsonBody();
    $err  = $body['error'];
    header("Location: https://example.com/declined/");
    exit();
};

//Send User to Thank You Page
header("Location: https://example.com/thank-you/");
exit();

在测试被拒绝的卡时,只需转到“错误”页面与“拒绝”页面。尝试直接加载“donation.php”页面确实会出现“错误”。

据我所知,上面的代码实际上并没有处理付款,因为它在测试模式下从未出现在我的条带付款选项卡中。

感谢您的帮助!

=== 已按要求更新

try {
    //Charge the Card
    $charge = \Stripe\Charge::create(array(
        "customer" => $customer->id,
        "amount" => $totalAmount,
        "currency" => "usd",
        "description" => "Name: $fname $lname For: Donation",
        "statement_descriptor" => "DONATION",
    ));

} catch (\Stripe\Error\InvalidRequest $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

} catch(\Stripe\Error\Card $e) {
    // Since it's a decline, \Stripe\Error\Card will be caught
    $body = $e->getJsonBody();
    $err  = $body['error'];
    header("Location: https://example.com/declined/");
    exit();

} catch (\Stripe\Error\Base $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

} catch (Exception $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();
};

//Send User to Thank You Page
header("Location: https://example.com/thank-you/");
exit();

【问题讨论】:

  • 您需要将异常按范围排列。 Exception 将捕获之前未指定的所有内容,因此需要将其放在最后。
  • 是的,我试过了,在尝试了一张被拒绝的卡后,我收到了这个错误:PHP 致命错误:未捕获的 Stripe\Error\Card:您的卡被拒绝了。在 /home/xxx/stripe/lib/ApiRequestor.php:214 来自 API 请求 'req_xxxxx
  • 奇数。一切都被 Exception 捕获,所以它不应该抛出一个未捕获的错误。你能用当前代码edit你的帖子,如果你有完整的跟踪代码吗?
  • 好的,我用最新的代码编辑了帖子。我没有完整的跟踪,谢谢

标签: php try-catch stripe-payments custom-errors


【解决方案1】:
} catch (Exception $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

}

将捕获任何异常,这就是为什么它总是回退到这个异常并且不会进入您的拒绝捕获块。

这样写:

//Create Customer
$customer = \Stripe\Customer::create(array(
    "email" => $email,
    "source"  => $token
));

try {
    //Charge the Card
    $charge = \Stripe\Charge::create(array(
        "customer" => $customer->id,
        "amount" => $totalAmount,
        "currency" => "usd",
        "description" => "Name: $fname $lname For: Donation",
        "statement_descriptor" => "DONATION",
    ));

} catch (\Stripe\Error\InvalidRequest $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

} catch (\Stripe\Error\Base $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

} catch(\Stripe\Error\Card $e) {
    // Since it's a decline, \Stripe\Error\Card will be caught
    $body = $e->getJsonBody();
    $err  = $body['error'];
    header("Location: https://example.com/declined/");
    exit();
} catch (Exception $e) {
    //Send User to Error Page
    header("Location: https://example.com/error/");
    exit();

};

//Send User to Thank You Page
header("Location: https://example.com/thank-you/");
exit();

【讨论】:

  • 谢谢您,我进行了更改,在尝试了一张被拒绝的卡后,我收到了这个错误:PHP 致命错误:未捕获的 Stripe\Error\Card:您的卡被拒绝了。在 /home/xxx/stripe/lib/ApiRequestor.php:214 来自 API 请求“req_xxxxx”
  • 这可能是在黑暗中拍摄,但 Base 似乎是 Card 的父类如果交换这两个 catch 块会发生什么?
猜你喜欢
  • 2013-07-18
  • 2019-05-14
  • 2020-02-16
  • 1970-01-01
  • 2011-07-28
  • 2018-03-29
  • 1970-01-01
  • 2013-12-24
  • 2016-04-29
相关资源
最近更新 更多