【发布时间】:2020-02-28 13:56:22
【问题描述】:
我在我的项目中使用条带支付网关。当用户输入过期的卡号时,我试图显示异常错误。但不是向我显示异常错误,而是向我显示 laravel 错误。 注意:它不适用于任何类型的例外,不仅是过期的卡号。
我正在使用 Stripe 提供的异常。
public function recharge(Request $request)
{
$this->validate($request, [
'amount' => 'required',
]);
$amount = $request->input('amount');
\Stripe\Stripe::setApiKey('key_here');
try {
$token = $_POST['stripeToken'];
$charge = \Stripe\Charge::create([
'amount' => $amount * 100,
'currency' => 'usd',
'description' => 'Example charge',
'source' => $token,
]);
$user = User::find(Auth::user()->id);
$user->deposit($amount);
Session::flash('success', 'Your Wallet is recharged!');
return back();
} catch (\Stripe\Error\Card $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\InvalidRequest $e) {
return "error";
} catch (\Stripe\Error\Authentication $e) {
return "error";
} catch (\Stripe\Error\ApiConnection $e) {
// Network communication with Stripe failed
return "error";
} catch (\Stripe\Error\Base $e) {
return "error";
} catch (Exception $e) {
return "error";
}
}
我想在 catch 块中显示我定义的错误。
【问题讨论】:
-
什么“Laravel 错误”?
-
我的意思是 laravel 的默认错误。 Stripe \ Exception \ CardException 您的卡已过期。
标签: laravel laravel-5 stripe-payments