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

rrrankin's avatar

Cashier Invalid Coupon Code Errors

I am using Laravel 5.2/Cashier 6.0. I can configure a coupon code in Stripe and if I enter the valid coupon code, all is well. The problem I am having is when an invalid coupon (like BADCOUPON) is entered I receive the following error:

InvalidRequest in ApiRequestor.php line 103: No such coupon: BADCOUPON

My Form

{!! Form::open(array('route' => 'plans.coupon', 'class' => 'form-horizontal')) !!}
 <input type="text" name="coupon" class="form-control" id="coupon">
<button class="btn btn-info" type="submit">Save</button>
{!! Form::close() !!}

My Routes

Route::get('/invoices', ['as' => 'invoices', 'uses' => 'SubscriptionsController@invoices']);
Route::post('/plans/coupon', ['as' => 'plans.coupon', 'uses' => 'SubscriptionsController@applyCoupon']);

My Controller

public function applyCoupon(Request $request){
        $code = $request->get('coupon');
        try{
            Auth::user()->applyCoupon($code);
            return redirect()->back()->withMessage('Coupon Applied');
        } catch (Exception $e) {
            return redirect()->back()->withError($e->getMessage());
        }
    }

I am thinking I should see the messages from the try/catch block, but ...

Any thoughts?

0 likes
5 replies
ahuggins's avatar

what are the contents of the applyCoupon method? Seems like the issue is happening in there.

rrrankin's avatar

@ahuggins - Thanks for replying. I'm not sure I understand your question. The applyCoupon method is above - or are you wanting to see something different?

rrrankin's avatar
rrrankin
OP
Best Answer
Level 4

I figured out the problem. Actually, I had two:

The first was a namespacing problem and adding a slash in front of Exception resolved that issue.

try {
   // 
} 
catch(\Exception $e) {
   // 
}

The second issue was that I was calling withError and not withErrors in the applyCoupon method.

ahuggins's avatar

I was trying to find out what was happening in the applyCoupon method in this line:

Auth::user()->applyCoupon($code);
rrrankin's avatar

Ok, I see. That method is found in \vendor\laravel\cashier\src\Http\Billable.php file. It looks like this:

public function applyCoupon($coupon)
    {
        $customer = $this->asStripeCustomer();

        $customer->coupon = $coupon;

        $customer->save();
    }

Please or to participate in this conversation.