1

I am attempting to display errors on my page when using a stripe card number that is supposed to trigger a stripe error this number 4000000000000002. The error seems to appear in the console as a 402, however I can't seem to grasp how to output it with php.

I am more used to javascript, Im am not sure how try and catch work, my assumption is that try runs code until a error is thrown and then catch runs. I just want to spit out any errors to the page that are at the charge point of the stripe code.

Here is my code

try {
  $customer = Stripe_Customer::create(array(
  'email' => $email,
  'card'  => $token,
  "description" => $quantity . " copies of The Economic Definition of Ore -Cut-off Grades in Theory and 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> Your payment was successful. For $' . $customertotal . ', and you have ordered ' . $quantity . ' copies </div>';

  $to  = $email; // note the 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 is a summary of your recent purchase.</p>
            </td>
          </tr>
          <tr>
           <td>
            <p>Your total purchase is $'.$customertotal . ' </p>
            <p>The number of books you have ordered is <b>'  . $quantity . '</b> </p></td>
         </tr>
       </table>
      </body>
    </html>
   ';

   // To send HTML mail, the Content-type header must be 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 it
   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 my code based on this question. The errors don't display when i use the error code credit card numbers, I am not sure what I am doing wrong.

try {
  $customer = Stripe_Customer::create(array(
  'email' => $email,
  'card'  => $token,
  "description" => $quantity . " copies of The Economic Definition of Ore -Cut-off Grades in Theory and 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> Your payment was successful. For $' .        $customertotal . ', and you have ordered ' . $quantity . ' copies </div>';
   }

   catch(Stripe_CardError $e) {
     $error1 = $e->getMessage();
   } catch (Stripe_InvalidRequestError $e) {
     // Invalid parameters were supplied to Stripe's API
     $error2 = $e->getMessage();
   } catch (Stripe_AuthenticationError $e) {
     // Authentication with Stripe's API failed
     $error3 = $e->getMessage();
   } catch (Stripe_ApiConnectionError $e) {
     // Network communication with Stripe failed
     $error4 = $e->getMessage();
   } catch (Stripe_Error $e) {
     // Display a very generic error to the user, and maybe send
     // yourself an email
     $error5 = $e->getMessage();
   } catch (Exception $e) {
     // Something else happened, completely unrelated to 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>
Community
  • 1
  • 1
Tom Sawyer
  • 59
  • 1
  • 7

2 Answers2

1

You can just catch all of them, the response is JSON as well:

catch (Exception $e) {
    $body = $e->getJsonBody();
    $err  = $body['error'];
    $errorMessage =  $err['message'];
}
David Nguyen
  • 8,368
  • 2
  • 33
  • 49
0

//do a try catch for the whole page

//so on top of the page put

try 

{

//...page code

}

//catch at bottom of page

catch (Exception $e) {
        $body = $e->getJsonBody();
        $err  = $body['error'];
        $errorMessage =  $err['message'];

    echo $errorMessage;

//then redirect to your payment page header( "refresh:10;url=mypaymentpage.php" );

}