How can I catch exception and redirect to my on message in Cartalyst/Stripe Laravel. I have Tried to add catch block in below mentioned code I want to catch errors in catch block

<?php

 namespace App\Http\Controllers;

 use Cartalyst\Stripe\Laravel\Facades\Stripe;

 use Illuminate\Http\Request;

 use Illuminate\Support\Facades\Redirect;

 class BuyNowController extends Controller

{
public function buyNow(Request $request)
{
    try {
$charge = Stripe::charges()->create([
            'amount' => $request->total,
            'currency' => 'CAD',
            'source' => $request->stripeToken,
            'description' => 'Description goes here',
            'receipt_email' => $request->email,
            'metadata' => [
                'country'=>$request->country,
                'address'=>$request->address,
                'postal code'=>$request->postalcode,
                'data1' => 'metadata 1',
                'data2' => 'metadata 2',
                'data3' => 'metadata 3',
            ],
        ]);
}
catch (CardErrorException $e) {
        // save info to database for failed
       return back()->withErrors('Error! ' . $e->getMessage());
        }         
    }
}
2

There are 2 answers

0
Yasir Minhaj On BEST ANSWER
   

// solve by adding \Cartalyst\Stripe\Exception\ in 



// catch(\Cartalyst\Stripe\Exception\CardErrorException $e)

    <?php

 

    namespace App\Http\Controllers;

 

    use Cartalyst\Stripe\Laravel\Facades\Stripe;

 

    use Illuminate\Http\Request;

 

    use Illuminate\Support\Facades\Redirect;

 

class BuyNowController extends Controller

{

    public function buyNow(Request $request)
    {
    

    try {
    $charge = Stripe::charges()->create([
            

    'amount' => $request->total,
            

    'currency' => 'CAD',
            

    'source' => $request->stripeToken,
            

    'description' => 'Description goes here',
            

    'receipt_email' => $request->email,
            

    'metadata' => [
                

    'country'=>$request->country,
                

    'address'=>$request->address,
                

    'postal code'=>$request->postalcode,
                

    'data1' => 'metadata 1',
                

    'data2' => 'metadata 2',
                

    'data3' => 'metadata 3',
            

    ],
        

    ]);
    }

  

// solve by addind \Cartalyst\Stripe\Exception\ before ecxeption name

    catch (\Cartalyst\Stripe\Exception\CardErrorException $e) {
        

    // save info to database for failed
       

    return back()->withErrors('Error! ' . $e->getMessage());
        

    } 

        
    

    }
    }
0
Nothehi On

you should create an object in the form of throw new CardErrorException() in try block.

   try {
$charge = Stripe::charges()->create([
            'amount' => $request->total,
            'currency' => 'CAD',
            'source' => $request->stripeToken,
            'description' => 'Description goes here',
            'receipt_email' => $request->email,
            'metadata' => [
                'country'=>$request->country,
                'address'=>$request->address,
                'postal code'=>$request->postalcode,
                'data1' => 'metadata 1',
                'data2' => 'metadata 2',
                'data3' => 'metadata 3',
            ],
        ]);

if ({charge_error_condition}) {
   throw new CardErrorException();
}

}
catch (CardErrorException $e) {
        // save info to database for failed
       return back()->withErrors('Error! ' . $e->getMessage());
        }