{"id":7110,"date":"2014-05-22T05:11:24","date_gmt":"2014-05-22T05:11:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/22\/stripe-error-handling-on-php-collection-of-common-programming-errors\/"},"modified":"2014-05-22T05:11:24","modified_gmt":"2014-05-22T05:11:24","slug":"stripe-error-handling-on-php-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/22\/stripe-error-handling-on-php-collection-of-common-programming-errors\/","title":{"rendered":"Stripe error handling on PHP-Collection of common programming errors"},"content":{"rendered":"<p>I think there is more than these exceptions (<strong>Stripe_InvalidRequestError<\/strong> and <strong>Stripe_Error<\/strong>) to catch.<\/p>\n<p>The code below is from Stripe&#8217;s web site. Probably, these additional exceptions, which you didn&#8217;t consider, occurs and your code fails <em>sometimes<\/em>.<\/p>\n<pre><code>try {\n  \/\/ Use Stripe's bindings...\n} catch(Stripe_CardError $e) {\n  \/\/ Since it's a decline, Stripe_CardError will be caught\n  $body = $e-&gt;getJsonBody();\n  $err  = $body['error'];\n\n  print('Status is:' . $e-&gt;getHttpStatus() . \"\\n\");\n  print('Type is:' . $err['type'] . \"\\n\");\n  print('Code is:' . $err['code'] . \"\\n\");\n  \/\/ param is '' in this case\n  print('Param is:' . $err['param'] . \"\\n\");\n  print('Message is:' . $err['message'] . \"\\n\");\n} catch (Stripe_InvalidRequestError $e) {\n  \/\/ Invalid parameters were supplied to Stripe's API\n} catch (Stripe_AuthenticationError $e) {\n  \/\/ Authentication with Stripe's API failed\n  \/\/ (maybe you changed API keys recently)\n} catch (Stripe_ApiConnectionError $e) {\n  \/\/ Network communication with Stripe failed\n} catch (Stripe_Error $e) {\n  \/\/ Display a very generic error to the user, and maybe send\n  \/\/ yourself an email\n} catch (Exception $e) {\n  \/\/ Something else happened, completely unrelated to Stripe\n}\n<\/code><\/pre>\n<p><strong>EDIT:<\/strong><\/p>\n<pre><code>try {\n    $charge = Stripe_Charge::create(array(\n    \"amount\" =&gt; $clientPriceStripe, \/\/ amount in cents\n    \"currency\" =&gt; \"usd\",\n    \"customer\" =&gt; $customer-&gt;id,\n    \"description\" =&gt; $description));\n    $success = 1;\n    $paymentProcessor=\"Credit card (www.stripe.com)\";\n} catch(Stripe_CardError $e) {\n  $error1 = $e-&gt;getMessage();\n} catch (Stripe_InvalidRequestError $e) {\n  \/\/ Invalid parameters were supplied to Stripe's API\n  $error2 = $e-&gt;getMessage();\n} catch (Stripe_AuthenticationError $e) {\n  \/\/ Authentication with Stripe's API failed\n  $error3 = $e-&gt;getMessage();\n} catch (Stripe_ApiConnectionError $e) {\n  \/\/ Network communication with Stripe failed\n  $error4 = $e-&gt;getMessage();\n} catch (Stripe_Error $e) {\n  \/\/ Display a very generic error to the user, and maybe send\n  \/\/ yourself an email\n  $error5 = $e-&gt;getMessage();\n} catch (Exception $e) {\n  \/\/ Something else happened, completely unrelated to Stripe\n  $error6 = $e-&gt;getMessage();\n}\n\nif ($success!=1)\n{\n    $_SESSION['error1'] = $error1;\n    $_SESSION['error2'] = $error2;\n    $_SESSION['error3'] = $error3;\n    $_SESSION['error4'] = $error4;\n    $_SESSION['error5'] = $error5;\n    $_SESSION['error6'] = $error6;\n    header('Location: checkout.php');\n    exit();\n}\n<\/code><\/pre>\n<p>Now, you will catch all possible exceptions and you can display error message as you wish. And also $error6 is for unrelated exceptions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think there is more than these exceptions (Stripe_InvalidRequestError and Stripe_Error) to catch. The code below is from Stripe&#8217;s web site. Probably, these additional exceptions, which you didn&#8217;t consider, occurs and your code fails sometimes. try { \/\/ Use Stripe&#8217;s bindings&#8230; } catch(Stripe_CardError $e) { \/\/ Since it&#8217;s a decline, Stripe_CardError will be caught $body [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7110","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7110"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7110\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}