code_legend
code_legend

Reputation: 3285

If any php error is returned, direct user to x page

I have the following code:

$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;
}

I have the following problem if the charge is valid than no problem, but if the charge is invalid, many times some odd errors will appear:

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

and hence when this happens no other code gets executed. is there any way to make this more global, where for any php error that occurs, takes the user to the following page:

 $location2 = "failedPayment.php";

  echo $location2;

Update:

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;
}

still not working unforunatetly

errors that are returned:

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

Upvotes: 0

Views: 84

Answers (1)

Jason Byrne
Jason Byrne

Reputation: 1619

You might want to do \Exception instead of Exception. Depending on what namespace the file you are executing is in.

Upvotes: 2

Related Questions