简体   繁体   中英

How to make Try/catch stripe error handler send user to success.php if no errors occurred?

I am setting up a checkout page for my business, I`m using Stripe API for this. I am having issues with my charge.php file not redirecting when no errors are returned by the error handler.

I've tried using the header() function which does successfully redirect if I enter the right card details, but when I try with one of the cards used for displaying an error message, it just redirects to index.html where the input form is located. If I remove the header function, charge.php will successfully display the errors, but obviously, there will be no redirect on a successful charge.

  // added stripe dependencies with composer
require_once('vendor/autoload.php');

  \Stripe\Stripe::setApiKey('SECRETKEY');

 // Sanitize POST Array
 $POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);

 $first_name = $POST['first_name'];
 $last_name = $POST['last_name'];
 $email = $POST['email'];
 $token = $POST['stripeToken'];

// Create Customer In Stripe
try {
$customer = \Stripe\Customer::create(array(
  "email" => $email,
  "source" => $token
));

// Charge Customer

$charge = \Stripe\Charge::create(array(
  "amount" => 4999,
  "currency" => "usd",
  "description" => "Online Purchase",
  "customer" => $customer->id
));

//ERROR HANDLER
} catch ( Stripe\Error\Base $e ) {
  // Code to do something with the $e exception object when an error occurs.
  echo $e->getMessage();

  // DEBUG.
  $body = $e->getJsonBody();
  $err  = $body['error'];
  echo '<br> ——— <br>';
  echo '<br>YOU HAVE NOT BEEN CHARGED — <br>';
  echo '— Status is: ' . $e->getHttpStatus() . '<br>';
  echo '— Message is: ' . $err['message'] . '<br>';
  echo '— Type is: ' . $err['type'] . '<br>';
  echo '— Param is: ' . $err['param'] . '<br>';
  echo '— Code is: ' . $err['code'] . '<br>';
  echo '<p>If you have entered the correct details, please try DOMAIN (in Safari or Chrome). If the error persists, please screenshot this message and send it to me alongside your email address.</p>';
  echo '<br> ——— <br>';

// Catch any other non-Stripe exceptions.
} catch ( Exception $e ) {
    $body = $e->getJsonBody();
    $err  = $body['error'];
    echo '<br> ——— <br>';
    echo '<br>Error — <br>';
    echo '— Status is: ' . $e->getHttpStatus() . '<br>';
    echo '— Message is: ' . $err['message'] . '<br>';
    echo '— Type is: ' . $err['type'] . '<br>';
    echo '— Param is: ' . $err['param'] . '<br>';
    echo '— Code is: ' . $err['code'] . '<br>';
    echo '<p>If you have entered the correct details, please try DOMAIN (in Safari or Chrome). If the error persists, please screenshot this message and send it to me alongside your email address.</p>'; 
    echo '<br> ——— <br>';
}

header('Location: success.php?tid='.$charge->id.'&product='.$charge->description);

I expect the charge.php to redirect to success.php on a successful charge and to display the errors on a faulty charge.

It does the redirect because it's after the catch blocks. Those blocks get executed and because there is no return statement in there, it will go on and execute the next line after the block(s) - your redirect header line.

You can either:

  1. Move the header(....) line into your try block, just after the charge creation
  2. Do a specific return or exit type of line in your catch blocks.

Both are viable solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM