我正在为我的业务建立一个结账页面,我正在为此使用Stripe。当错误处理程序没有返回错误时,我的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,并在错误电荷上显示错误。
发布于 2019-02-18 12:54:01
它执行重定向,因为它是在catch块之后。这些块将被执行,并且因为其中没有return语句,所以它将继续执行在块之后的下一行--您的重定向头行。
你可以:
header(....)行移动到您的try块中,就在电荷创建之后return块中执行特定的exit或exit类型的行。两者都是可行的解决办法。
https://stackoverflow.com/questions/54747429
复制相似问题