0

Cartalyst/Stripe Laravel で例外をキャッチして on メッセージにリダイレクトするにはどうすればよいですか。以下のコードにcatchブロックを追加しようとしましたcatchブロックでエラーをキャッチしたい

<?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());
        }         
    }
}
4

2 に答える 2

0
   

// 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());
        

    } 

        
    

    }
    }
于 2020-10-07T06:39:56.057 に答える