如何捕获异常并重定向到 Cartalyst/Stripe Laravel 中的 on 消息?我想在 catch 块中捕获错误
How can I catch exception and redirect to my on message in Cartalyst/Stripe Laravel? I want to catch errors in catch block
如何捕获异常并重定向到 Cartalyst/Stripe Laravel 中的消息。我试图在下面提到的代码中添加 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());
}
}
}
你应该在 try 块中创建一个 throw new CardErrorException()
形式的对象。
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());
}
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>
// 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());
}
}
}
如何捕获异常并重定向到 Cartalyst/Stripe Laravel 中的消息。我试图在下面提到的代码中添加 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());
}
}
}
你应该在 try 块中创建一个 throw new CardErrorException()
形式的对象。
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());
}
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>
// 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());
}
}
}