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

Presto's avatar

Pre Validate Stripe Coupon Before Checkout

I have a function setup to validate a Stripe coupon code, this function is called via an Ajax call when the coupon field on my checkout form loses focus. The function has been working grate, but today a user let me know that the coupon returned false, I tested it and got the same thing, the coupon code is in my Stipe dashboard and it is under the live tab. Not sure if the Stipe package got updated and broke my code or what, I'm not getting any on screen errors or errors in my log.

public function isCouponValid($coupon)
{
    \Stripe\Stripe::setApiKey( env('STRIPE_SECRET') );
    try {
        $status = \Stripe\Coupon::retrieve( $coupon );
        if($status->amount_off) {
            $price = currencies( strtoupper( $status->currency ) ) . ' ' . $status->amount_off . ' ' . $status->duration;
        } else {
            if( $status->duration == 'repeating' ) {
                $duration = '(' . $status->duration_in_months . ' months)';
            } else {
                $duration = '(forever)';
            }
            $price = $status->percent_off . '% for ' . $duration;
        }
        return ['status'=>'true','msg'=>$price];
    } catch(\Exception $e) {
        return ['status'=>'false'];
    }
}
0 likes
1 reply
Presto's avatar
Presto
OP
Best Answer
Level 6

Really, I mixed up my Stripe keys, stupid... stupid... stupid... Well peeps, always confirm your Stripe keys is the lesson for today lol.

Please or to participate in this conversation.