Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Created October 11, 2022 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remarkablemark/8fb40497db210c34a54267c4826bd750 to your computer and use it in GitHub Desktop.
Save remarkablemark/8fb40497db210c34a54267c4826bd750 to your computer and use it in GitHub Desktop.
Stripe PHP exceptions
Description Exception
Decline \Stripe\Exception\CardException
Too many requests made to the API too quickly \Stripe\Exception\RateLimitException
Invalid parameters were supplied to Stripe's API \Stripe\Exception\InvalidRequestException
Authentication with Stripe's API failed \Stripe\Exception\AuthenticationException
Network communication with Stripe failed \Stripe\Exception\ApiConnectionException
Display a very generic error to the user \Stripe\Exception\ApiErrorException

See Stripe PHP handling errors:

try {
  // Use Stripe's library to make requests...
} 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment