条带异常不起作用,给出laravel错误而不是异常错误

pn9klfpd  于 2023-02-20  发布在  其他
关注(0)|答案(3)|浏览(148)

我正在使用条纹支付网关在我的项目。我试图显示异常错误时,用户输入过期的卡号。但不是显示我的异常错误,它显示我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块中定义的错误。

kcwpcxri

kcwpcxri1#

errors上的条带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
}
u1ehiz5o

u1ehiz5o2#

您没有捕获Stripe\Exception\CardException异常,也可能实际上没有捕获Exception,除非您在文件顶部使用了别名Exception
在顶部的类声明之前添加use Exception;,或者将catch中的Exception调整为\Exception
看起来stripe-php库的较新版本从Stripe\Exception引发异常,并且不再具有命名空间Stripe\Error FYI。
Stripe API Reference - Handling Errors

nfzehxib

nfzehxib3#

通过包含use Exception;修复了此问题

相关问题