javascript - Using php catch to display errors in stripe -


i attempting display errors on page when using stripe card number supposed trigger stripe error number 4000000000000002. error seems appear in console 402, can't seem grasp how output php.

i more used javascript, im not sure how try , catch work, assumption try runs code until error thrown , catch runs. want spit out errors page @ charge point of stripe code.

here code

try {   $customer = stripe_customer::create(array(   'email' => $email,   'card'  => $token,   "description" => $quantity . " copies of economic definition of ore -cut-off grades in theory , practice"));    $charge = stripe_charge::create(array(   "amount" => $total, // amount in cents, again   "currency" => "usd",   'customer' => $customer->id,   "metadata" => array("first name:" => $firstname, "last name:" => $lastname)));   $success = '<div class="alert alert-success">   <strong>success!</strong> payment successful. $' . $customertotal . ', , have ordered ' . $quantity . ' copies </div>';    $to  = $email; // note comma   // subject   $subject = 'economic definition of ore purchase';   // message   $message = '     <html>     <head>       <title>sumary of purchase</title>      </head>      <body>        <table>          <tr>            <td> hi ' . $firstname . '</td>           </tr>           <tr>             <td>              <p>here summary of recent purchase.</p>             </td>           </tr>           <tr>            <td>             <p>your total purchase $'.$customertotal . ' </p>             <p>the number of books have ordered <b>'  . $quantity . '</b> </p></td>          </tr>        </table>       </body>     </html>    ';     // send html mail, content-type header must set    $headers  = 'mime-version: 1.0' . "\r\n";    $headers .= 'content-type: text/html; charset=iso-8859-1' . "\r\n";     // additional headers    $headers .= 'to: '. $firstname .' <' . $email . ' >  ' . "\r\n";    $headers .= 'from: comet <info@cometstrategy.com>' . "\r\n";    // $headers .= 'bcc: birthdaycheck@example.com' . "\r\n";     // mail    mail($to, $subject, $message, $headers); }  catch(\stripe\error\card $e){    $e_json = $e->getjsonbody();    $error = $e_json['error']; } }?> <span class="payment-success">  <?= $success ?>  <?= $error ?> </span> 

i have update code based on this question. errors don't display when use error code credit card numbers, not sure doing wrong.

try {   $customer = stripe_customer::create(array(   'email' => $email,   'card'  => $token,   "description" => $quantity . " copies of economic definition of ore -cut-off grades in theory , practice"             ));   $charge = stripe_charge::create(array(   "amount" => $total, // amount in cents, again   "currency" => "usd",   'customer' => $customer->id,   "metadata" => array("first name:" => $firstname, "last name:" => $lastname))    );     $successtest = 1;    $success = '<div class="alert alert-success">    <strong>success!</strong> payment successful. $' .        $customertotal . ', , have ordered ' . $quantity . ' copies </div>';    }     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 ($successtest!=1)     {    ?>     <span class="payment-success">       <?= $error1 ?>       <?= $error2 ?>       <?= $error3 ?>       <?= $error4 ?>       <?= $error5 ?>       <?= $error6 ?>     </span>   <?php    }   }    ?>      <span class="payment-success">       <?= $success ?>     </span> 

you can catch of them, response json well:

catch (exception $e) {     $body = $e->getjsonbody();     $err  = $body['error'];     $errormessage =  $err['message']; } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -