Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ipianolabcode's avatar

Handling stripe errors in Laravel 5 Billing

I'm trying to handle error cases with Billing in Laravel 5 (declined cards, etc). I'm using a the charge() method as follows within a command handler. The documentation says that the charge() method should return false on failure, however, the application is throwing the following:

Stripe_CardError (in ApiRequestor.php line 153 at Stripe_ApiRequestor->handleApiError('{ "error": { "message": "Your card was declined.", "type": "card_error", "code": "card_declined" } } ', '402', array('error' => array('message' => 'Your card was declined.', 'type' => 'card_error', 'code' => 'card_declined'))) in ApiRequestor.php line 268

How do I go about throwing and catching an exception for the declined charge that I can return to the view? I've tried the following, but it still returns the Stripe_CardError above to the browser:

in handler():


if(!$this->user->charge($price)){
    throw new Exception('Your card was declined');
}

in controller:

try{

    $this->dispatch(new CreateCharge($user));

}  catch (Exception $e){

    return Redirect::refresh()->withErrors(['msg',$e->getMessage])->withInput();

}

Any help would be appreciated.

0 likes
10 replies
bobbybouwmann's avatar

I'm not sure but I think Stripe will throw it's own exception and you can catch that exception

1 like
ipianolabcode's avatar

Here's more of the error trace, maybe that will help someone spot something here (I've changed the token and test keys with placeholders for this post, of course):

in ApiRequestor.php line 153

at Stripe_ApiRequestor->handleApiError('{ "error": { "message": "Your card was declined.", "type": "card_error", "code": "card_declined" } } ', '402', array('error' => array('message' => 'Your card was declined.', 'type' => 'card_error', 'code' => 'card_declined'))) in ApiRequestor.php line 268

at Stripe_ApiRequestor->_interpretResponse('{ "error": { "message": "Your card was declined.", "type": "card_error", "code": "card_declined" } } ', '402') in ApiRequestor.php line 109 at Stripe_ApiRequestor->request('post', '/v1/customers', array('card' => 'tokendata', 'email' => 'email@mail.com', 'description' => 'Registration'), array()) in ApiResource.php line 143

at Stripe_ApiResource::_scopedCreate('Stripe_Customer', array('card' => 'tokendata', 'email' => 'email@mail.com', 'description' => 'Registration'), 'testkey') in Customer.php line 38

at Stripe_Customer::create(array('card' => 'tokendata', 'email' => 'email@mail.com', 'description' => 'Registration'), 'testkey') in StripeGateway.php line 527

at StripeGateway->createStripeCustomer('tokendata', array('email' => 'email@mail.com', 'description' => 'Registration')) in StripeGateway.php line 124

at StripeGateway->create('tokendata', array('email' => 'email@mail.com', 'description' => 'Registration')) in CreateCharge.php line 60

ipianolabcode's avatar
ipianolabcode
OP
Best Answer
Level 2

For anyone encountering this issue -- the createStripeCustomer() method in StripeGateway.php was not catching the Stripe_CardError for some reason. Was able to work around it by placing the Stripe_Customer::create call in its own try/catch block. Should probably be fixed.

1 like
robertschlackman's avatar

I am having a verifycrftoken error when stripe tries to call my webhook controller. This is what I would expect since stripe would not be sending a token. Is there a way around this?

robertschlackman's avatar

I figured this out. I had to change the middleware function in VerifyCsrfToken.php to look like this.

public function handle($request, Closure $next)
{

    //disable CSRF check on following routes
    $skip = array(
                'stripe/webhook'
                );

    foreach ($skip as $key => $route) {
        //skip csrf check on route
        if($request->is($route)){
            return parent::addCookieToResponse($request, $next($request));
        }
    }

    return parent::handle($request, $next);
}

But I'm still getting an error running the test requests from stripe.com.

I set up the webhook route like this:

Route::post('stripe/webhook', 'Laravel\Cashier\WebhookController@handleWebhook');

The error is Class App\Http\Controllers\Laravel\Cashier\WebhookController does not exist.

Anybody run into this?

4 likes
aixguru's avatar

@robertschlackman The router is prepending the App\Http\Controllers namespace onto the Cashier-provided controller. I suggest creating your own Controller that extends the one you're trying to use now and pointing your route there. This also lets you add your own methods to pick up other Stripe webhooks not handled by default.

Please or to participate in this conversation.