如何在 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());
}
}
}