Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果没有发生错误,如何使Try/catch stripe错误处理程序将用户发送到success.php?_Php_Stripe Payments - Fatal编程技术网

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

如果没有发生错误,如何使Try/catch stripe错误处理程序将用户发送到success.php?,php,stripe-payments,Php,Stripe Payments,我正在为我的业务设置一个结账页面,我正在为此使用StripeAPI。当错误处理程序没有返回错误时,我的charge.php文件无法重定向 我尝试过使用header()函数,如果输入正确的卡片详细信息,它会成功重定向,但是当我尝试使用用于显示错误消息的其中一张卡片时,它只会重定向到输入表单所在的index.html。如果删除header函数,charge.php将成功显示错误,但显然,成功充电时不会重定向 // added stripe dependencies with composer r

我正在为我的业务设置一个结账页面,我正在为此使用StripeAPI。当错误处理程序没有返回错误时,我的charge.php文件无法重定向

我尝试过使用header()函数,如果输入正确的卡片详细信息,它会成功重定向,但是当我尝试使用用于显示错误消息的其中一张卡片时,它只会重定向到输入表单所在的index.html。如果删除header函数,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);
//使用composer添加了条带依赖项
一次需要_('vendor/autoload.php');
\Stripe\Stripe::setApiKey('SECRETKEY');
//清理POST数组
$POST=过滤器\变量\数组($\过滤器\清理\字符串);
$first_name=$POST['first_name'];
$last_name=$POST['last_name'];
$email=$POST['email'];
$token=$POST['stripeToken'];
//在条带中创建客户
试一试{
$customer=\Stripe\customer::创建(数组)(
“email”=>$email,
“源”=>$token
));
//收费客户
$charge=\Stripe\charge::创建(数组)(
“金额”=>4999,
“货币”=>“美元”,
“说明”=>“网上购物”,
“客户”=>$customer->id
));
//错误处理程序
}捕获(条带\Error\Base$e){
//发生错误时对$e异常对象执行操作的代码。
echo$e->getMessage();
//调试。
$body=$e->getJsonBody();
$err=$body['error'];
回音“
--
”; 回显“
您尚未被收费-
”; echo'-状态为:'.$e->getHttpStatus()。
; echo'-Message是:'.$err['Message'].
'; echo'-Type是:'.$err['Type'].
; echo'-Param为:'.$err['Param'].
'; echo'-Code是:'.$err['Code'].
; echo'如果您输入了正确的详细信息,请尝试域(在Safari或Chrome中)。如果错误仍然存在,请截屏此消息并将其与您的电子邮件地址一起发送给我。

; 回音“
--
”; //捕获任何其他非条带异常。 }捕获(例外$e){ $body=$e->getJsonBody(); $err=$body['error']; 回音“
--
”; 回显“
错误-
”; echo'-状态为:'.$e->getHttpStatus()。
; echo'-Message是:'.$err['Message'].
'; echo'-Type是:'.$err['Type'].
; echo'-Param为:'.$err['Param'].
'; echo'-Code是:'.$err['Code'].
; echo'如果您输入了正确的详细信息,请尝试域(在Safari或Chrome中)。如果错误仍然存在,请截屏此消息并将其与您的电子邮件地址一起发送给我。

; 回音“
--
”; } 标题('Location:success.php?tid='.$charge->id.&product='.$charge->description);

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

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

您可以:

  • 在创建费用之后,将
    标题(..)
    行移动到
    try
    块中
  • catch
    块中执行特定的
    return
    exit
    行类型
    这两种都是可行的解决方案。

    是什么阻止了您从try block重定向。。您的成功条件是否满足?我不知道这是否相关,但您应该在catch Stripe\Error\Base应为\Stripe\Error\Base和Exception中为类使用完全限定的命名空间\Exception@pwaterz-OP已在使用完全限定的命名空间。如果在另一个名称空间下使用它们,则只需在它们前面加上反斜杠,而这段代码似乎不是这样。