In this episode, we're tackling the whole checkout flow now that our plans and referral system are set up. First, we make sure clicking a plan takes you to a proper checkout session (with a bit of UI tidy-up to make things look neat). Then we focus on implementing the actual checkout process using a controller where users must be signed in.
We wire things up so the correct plan data goes to Stripe, and we handle redirection back to the dashboard after success or cancellation (super handy so users don’t get lost after payment). Laravel Cashier is doing a lot of the heavy lifting here, so we don’t need to worry about manually updating our subscriptions table.
Now, for the cool part: handling referral coupons. If a user has a referral code in their cookie, we want to apply a coupon for them automatically. We check if the referral code exists and is valid, and grab the appropriate coupon from config (so you can change it easily if needed). When everything’s set up, Stripe correctly offers the coupon discount at checkout!
But there’s a catch—when Stripe sends us a webhook after successful payment, we don’t have browser access, so we need to make sure the referral code is passed as metadata in the subscription. This way, we can always know which code was used when handling webhooks. This episode wraps up with all that working: you get clean checkout flows, automatic referral discounts, and everything’s ready for handling post-checkout logic in webhooks next.