【问题标题】:Stripe exception is not working .giving laravel errors instead of exception errors条纹异常不起作用。给出 laravel 错误而不是异常错误
【发布时间】: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


【解决方案1】:

关于错误的 Stripe API 文档几乎包含了我们所能获得的一切。这是您在使用条带库时可以放置的代码块。

try {
  // Use Stripe's library to make requests...
} catch(\Stripe\Exception\CardException $e) {
  // Since it's a decline, \Stripe\Exception\CardException will be caught
  echo 'Status is:' . $e->getHttpStatus() . '\n';
  echo 'Type is:' . $e->getError()->type . '\n';
  echo 'Code is:' . $e->getError()->code . '\n';
  // param is '' in this case
  echo 'Param is:' . $e->getError()->param . '\n';
  echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}

【讨论】:

    【解决方案2】:

    您没有捕捉到Stripe\Exception\CardException 异常。除非您在文件顶部添加了别名 Exception,否则您可能实际上也没有捕捉到 Exception

    在顶部的类声明之前添加use Exception;或将catch中的Exception调整为\Exception

    看起来较新版本的 stripe-php 库从 Stripe\Exception 抛出异常,并且不再具有命名空间 Stripe\Error 仅供参考。

    Stripe API Reference - Handling Errors

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多