Stripe payment intent exception handling

157 views
Skip to first unread message

Frank

unread,
Jul 13, 2023, 9:38:10 AM7/13/23
to Stripe API Discussion
So currently im trying to catch stripe exceptions when creating payment intents. No matter what i seem to do I cant seem to be able to catch them even though im sure the code is correct. The exception seems to be coming directly from Guzzle which is the weird part. Below is my code in PHP:

try {
$intent = $stripe->paymentIntents->create($payload);

header("Content-Type: application/json");
print( json_encode($intent->jsonSerialize() ));
} catch (\Stripe\Exception\RateLimitException $e) {
// Too many requests made to the API too quickly
error_log("ERROR: too many requests made to the API too quickly {$e->getError()->message}");
http_response_code(429);
} catch (\Stripe\Exception\CardException $e) {
// Card was declined
logAlert("Card was declined {$e->getError()->message}");
http_response_code(402);
} catch (\Stripe\Exception\InvalidRequestException $e) {
// Invalid parameters were supplied to Stripe's API
error_log("ERROR: invalid parameters were supplied to Stripe's API {$e->getError()->message}");
http_response_code(400);
} catch (\Stripe\Exception\ApiConnectionException $e) {
// Network communication with Stripe failed
error_log("ERROR: network communication with Stripe failed {$e->getError()->message}");
http_response_code(503);
} catch (\Stripe\Exception\AuthenticationException $e) {
// Authentication with Stripe's API failed
error_log("ERROR: authentication with Stripe's API failed {$e->getError()->message}");
http_response_code(511);
} catch (\Stripe\Exception\ApiErrorException $e) {
// generic error
error_log("ERROR: {$e->getError()->message}");
http_response_code(502);
} catch (\GuzzleHttp\Exception\RequestException $e) {
error_log("ERROR: YAY it hit the guzzle exception catcher");
error_log("ERROR: Guzzle error from paymentIntents create: {$e->getMessage()}");
http_response_code(500);
} catch (Exception $e) {
error_log("ERROR: {$e->getMessage()}");
error_log("ERROR: YAY it hit the exception catcher");
http_response_code(500);
}

The exception im getting is:

[12-Jul-2023 14:28:14 America/Edmonton] PHP Fatal error:  Uncaught GuzzleHttp\Exception\RequestException: cURL error 3:  (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:201


app/web.1: #1 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(105): GuzzleHttp\Handler\CurlFactory::finishError()


Any help would be appreciated, this might not even be a stripe problem but it does create the payment intents for regular cards no problem. It only gives a 500 when i use a declined card.


Thanks, Frank

Ben Snell

unread,
Jul 13, 2023, 9:45:54 AM7/13/23
to Stripe API Discussion, Frank
Hello Frank,

You are right that this doesn't look like a Stripe-specific issue. This does appear to be something specific to Guzzle. Without any familiarity with Guzzle, my guess is that when you use a declined card and Stripe returns a 402, your Guzzle framework is then taking this response and throwing a 500 before the Stripe exception is caught by the code you shared. I'd suggest looking into how Guzzle expects you to handle 4xx API responses and then troubleshooting from there.

I hope this helps.

Warm regards,
Ben

Reply all
Reply to author
Forward
0 new messages