首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果没有发生错误,如何使Try/catch条错误处理程序将用户发送到success.php?

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

Stack Overflow用户
提问于 2019-02-18 12:34:56
回答 1查看 171关注 0票数 1

我正在为我的业务建立一个结账页面,我正在为此使用Stripe。当错误处理程序没有返回错误时,我的charge.php文件出现了问题,不能重定向。

我尝试使用header()函数,如果输入正确的卡片详细信息,就可以成功地重定向,但是当我尝试使用用于显示错误消息的卡片之一时,它只是重定向到输入表单所在的index.html。如果删除标头函数,charge.php将成功地显示错误,但显然,成功的充电不会出现重定向。

代码语言:javascript
复制
  // 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,并在错误电荷上显示错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-18 12:54:01

它执行重定向,因为它是在catch块之后。这些块将被执行,并且因为其中没有return语句,所以它将继续执行在块之后的下一行--您的重定向头行。

你可以:

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

两者都是可行的解决办法。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54747429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档