Catching Stripe errors with Try/Catch PHP method -


during testing of stripe in website, built code this:

   try {         $charge = stripe_charge::create(array(           "amount" => $clientpricestripe, // amount in cents           "currency" => "usd",           "customer" => $customer->id,           "description" => $description));           $success = 1;           $paymentprocessor="credit card (www.stripe.com)";     }      catch (stripe_invalidrequesterror $a) {         // since it's decline, stripe_carderror caught         $error3 = $a->getmessage();     }      catch (stripe_error $e) {         // since it's decline, stripe_carderror caught         $error2 = $e->getmessage();         $error = 1;     } if ($success!=1) {     $_session['error3'] = $error3;     $_session['error2'] = $error2;     header('location: checkout.php');     exit(); } 

the problem there error card (not catched "catch" arguments have there) , "try" fails , page posts error in screen instead of going "if" , redirecting checkout.php.

how should structure error handling error , redirect checkout.php , display error there?

thanks!


error thrown:

fatal error: uncaught exception 'stripe_carderror' message 'your card declined.' in ............ /lib/stripe/apirequestor.php on line 92 

i think there more these exceptions (stripe_invalidrequesterror , stripe_error) catch.

the code below stripe's web site. probably, these additional exceptions, didn't consider, occurs , code fails sometimes.

try {   // use stripe's bindings... } catch(stripe_carderror $e) {   // since it's decline, stripe_carderror 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 '' in case   print('param is:' . $err['param'] . "\n");   print('message is:' . $err['message'] . "\n"); } catch (stripe_invalidrequesterror $e) {   // invalid parameters supplied stripe's api } catch (stripe_authenticationerror $e) {   // authentication stripe's api failed   // (maybe changed api keys recently) } catch (stripe_apiconnectionerror $e) {   // network communication stripe failed } catch (stripe_error $e) {   // display generic error user, , maybe send   // email } catch (exception $e) {   // else happened, unrelated stripe } 

edit:

try {     $charge = stripe_charge::create(array(     "amount" => $clientpricestripe, // amount in cents     "currency" => "usd",     "customer" => $customer->id,     "description" => $description));     $success = 1;     $paymentprocessor="credit card (www.stripe.com)"; } catch(stripe_carderror $e) {   $error1 = $e->getmessage(); } catch (stripe_invalidrequesterror $e) {   // invalid parameters supplied stripe's api   $error2 = $e->getmessage(); } catch (stripe_authenticationerror $e) {   // authentication stripe's api failed   $error3 = $e->getmessage(); } catch (stripe_apiconnectionerror $e) {   // network communication stripe failed   $error4 = $e->getmessage(); } catch (stripe_error $e) {   // display generic error user, , maybe send   // email   $error5 = $e->getmessage(); } catch (exception $e) {   // else happened, unrelated stripe   $error6 = $e->getmessage(); }  if ($success!=1) {     $_session['error1'] = $error1;     $_session['error2'] = $error2;     $_session['error3'] = $error3;     $_session['error4'] = $error4;     $_session['error5'] = $error5;     $_session['error6'] = $error6;     header('location: checkout.php');     exit(); } 

now, catch possible exceptions , can display error message wish. , $error6 unrelated exceptions.


Comments