如果返回任何php错误,请将用户指向x页面

时间:2015-04-26 00:45:16

标签: php mysql ajax json stripe-payments

我有以下代码:

$stripChargeValid = true;
try {
 $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'cad',
            'capture' => 'false',

      'description'=> $crs_title

  ));


$charge_json = $charge->__toJSON();
$array = json_decode($charge_json, true);
$chargeID = json_decode($charge_json);
$chargeCapture = $chargeID->id;



} catch(\Stripe\Error\Card $e) {
  // The card has been declined
  $stripChargeValid = false;
  $_SESSION["cardError"] = $e->getMessage();

  $location2 = "failedPayment.php";

  echo $location2;

}

if($stripChargeValid){
   //Run your queries.
      $insert_c = "insert into orders (course_title,course_price_final,course_provider,user_email,course_date,course_delivery,order_date,charge_id,card_name,final_coupon,coupon_discount,coupon_name) 
             values ('$crs_title','$course_price_final','$course_provider','$user_email','$course_date1','$course_delivery','$order_date','$chargeCapture','$card_name','$finalCoupon','$couponDiscount','$couponName')";
    $run_c = mysqli_query($con, $insert_c);
$location = "index.php";
echo $location;
}

如果充电有效而没有问题我有以下问题,但如果充电无效,很多时会出现一些奇怪的错误:

http://localhost:8080/test/%3Cbr%20/%3E%3Cb%3EFatal%20error%3C/b%3E:%20%20Uncaught%20exception%20'Stripe/Error/Card'%20with%20message%20'Your%20card%20has%20expired.'%20in%20C:/xampp/htdocs/test/vendor/stripe/stripe-php/lib/ApiRequestor.php:155Stack%20trace:#0 

C:\xampp\htdocs\test\vendor\stripe\stripe-php\lib\ApiRequestor.php(268): Stripe\ApiRequestor-&gt;handleApiError('{\n  &quot;error&quot;: {\n...', 402, Array)#1 C:\xampp\htdocs\test\vendor\stripe\stripe-php\lib\ApiRequestor.php(114): Stripe\ApiRequestor-&gt;_interpretResponse('{\n  &quot;error&quot;: {\n...', 402)#2 C:\xampp\htdocs\test\vendor\stripe\stripe-php\lib\ApiResource.php(105): Stripe\ApiRequestor-&gt;request('post', '/v1/customers', Array, Array)#3 C:\xampp\htdocs\test\vendor\stripe\stripe-php\lib\ApiResource.php(137): Stripe\ApiResource::_staticRequest('post', '/v1/customers', Array, NULL)#4 C:\xampp\htdocs\test\vendor\stripe\stripe-php\lib\Customer.php(37): Stripe\ApiResource::_create(Array, NULL)#5 C:\xampp\htdocs\test\paymentCapture.php(28): Stripe\Customer::create(Array)# in <b>C:\xampp\htdocs\test\vendor\stripe\stripe-php\lib\ApiRequestor.php</b> on line <b>155</b><br />

因此当发生这种情况时,没有其他代码被执行。是否有任何方法可以使这更加全局化,对于任何发生的PHP错误,将用户带到以下页面:

 $location2 = "failedPayment.php";

  echo $location2;

更新

try {

$stripChargeValid = true;
try {
 $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'cad',
            'capture' => 'false',

      'description'=> $crs_title

  ));


$charge_json = $charge->__toJSON();
$array = json_decode($charge_json, true);
$chargeID = json_decode($charge_json);
$chargeCapture = $chargeID->id;



} catch(\Stripe\Error\Card $e) {
  // The card has been declined
  $stripChargeValid = false;
  $_SESSION["cardError"] = $e->getMessage();

  $location2 = "failedPayment.php";

  echo $location2;

} }
} catch (Exception $e) {
 $location2 = "failedPayment.php";

  echo $location2;
}

if($stripChargeValid){
   //Run your queries.
      $insert_c = "insert into orders (course_title,course_price_final,course_provider,user_email,course_date,course_delivery,order_date,charge_id,card_name,final_coupon,coupon_discount,coupon_name) 
             values ('$crs_title','$course_price_final','$course_provider','$user_email','$course_date1','$course_delivery','$order_date','$chargeCapture','$card_name','$finalCoupon','$couponDiscount','$couponName')";
    $run_c = mysqli_query($con, $insert_c);
$location = "index.php";
echo $location;
}

仍然无法正常工作

返回的错误:

 switch ($rcode) {
            case 400:
                if ($code == 'rate_limit') {
                    throw new Error\RateLimit($msg, $param, $rcode, $rbody, $resp);
                }

                // intentional fall-through
            case 404:
                throw new Error\InvalidRequest($msg, $param, $rcode, $rbody, $resp);
            case 401:
                throw new Error\Authentication($msg, $rcode, $rbody, $resp);
            case 402:
                throw new Error\Card($msg, $param, $code, $rcode, $rbody, $resp);
            default:
                throw new Error\Api($msg, $rcode, $rbody, $resp);
        }
    }

1 个答案:

答案 0 :(得分:2)

你可能想做\ Exception而不是Exception。取决于您正在执行的文件所在的命名空间。