繁体   English   中英

如果没有错误发生,如何使Try / catch条带错误处理程序将用户发送到success.php?

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

我正在为我的公司设置一个结帐页面,我正在使用Stripe API。 当错误处理程序没有返回错误时,我的charge.php文件没有重定向问题。

我尝试使用header()函数,如果我输入正确的卡片详细信息,它会成功重定向,但当我尝试使用其中一张用于显示错误消息的卡片时,它只是重定向到index.html,输入表单是位于。 如果我删除了头文件功能,charge.php将成功显示错误,但显然,成功收费时不会有重定向。

  // 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);

我希望charge.php能够在成功充电时重定向到success.php,并在错误充电时显示错误。

它执行重定向,因为它位于catch块之后。 这些块被执行,因为那里没有return语句,它将继续执行块后面的下一行 - 你的重定向标题行。

你可以:

  1. 在创建电荷后,将header(....)行移动到您的try块中
  2. catch块中执行特定的returnexit类型的行。

两者都是可行的解决方

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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