Created
November 22, 2014 13:56
-
-
Save dljoseph/a10caf9a7cb57a0c2772 to your computer and use it in GitHub Desktop.
Stripe API try/catch block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
// Use Stripe's bindings... | |
} catch(Stripe_CardError $e) { | |
// Since it's a decline, Stripe_CardError 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_InvalidRequestError $e) { | |
// Invalid parameters were supplied to Stripe's API | |
} catch (Stripe_AuthenticationError $e) { | |
// Authentication with Stripe's API failed | |
// (maybe you changed API keys recently) | |
} catch (Stripe_ApiConnectionError $e) { | |
// Network communication with Stripe failed | |
} catch (Stripe_Error $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