Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe exception is not working .giving laravel errors instead of exception errors

I am using stripe payment gateway in my project. I am trying to display exception errors when a user entered expired card number. but instead of showing me error of exception it shows me laravel error. Note: it is not working with any kind of exception not only expired card number.

I am using the exception provided by 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";
    }
}

I want to display the error defined by me in the catch block.

like image 545
Bilal Arshad Avatar asked Jan 30 '26 04:01

Bilal Arshad


2 Answers

Stripe API Document on errors has pretty much everything we can get. here is the Code block you can put while using stripe library.

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
}
like image 197
Nikunj Dhimar Avatar answered Feb 02 '26 06:02

Nikunj Dhimar


You are not catching the Stripe\Exception\CardException exception. You are probably not actually catching Exception either, unless you have aliased Exception at the top of your file.

Add use Exception; before the class declaration at the top or adjust Exception in the catch to \Exception.

Looks like the newer version of stripe-php library throws Exceptions from Stripe\Exception and no longer has a namespace Stripe\Error FYI.

Stripe API Reference - Handling Errors

like image 40
lagbox Avatar answered Feb 02 '26 06:02

lagbox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!