In this episode, we focus on how to properly log payments that come in from Stripe so we can manage payouts for referrals. We start by cleaning up the database, clearing out any old subscriptions and payments to start fresh. Then, we jump into the process of listening for successful payment events from Stripe using a webhook handler in our Laravel application.
The episode walks through the logic of tying the payment that comes in to the right subscription and referral code. We set up (or review) the relationships between subscriptions and referral codes, making sure that we can always trace who referred whom. One key challenge we address here is ensuring that payments are only logged once, even if Stripe sends us the same webhook event multiple times (which happens!). To handle that, we use firstOrCreate
when making our payment records, based on the unique Stripe payment ID.
We also discuss edge cases, like when webhooks arrive out of order—say, the payment comes in before the subscription is fully created—and we use retries to make sure the relationships are there before logging the payment. As the code comes together, we do some test runs and check the logs and the database to confirm everything's working: payments are attached to the correct users, referral codes, and amounts, and duplicates aren’t being created.
By the end of the video, we’ve built a robust system for tracking incoming payments through Stripe and ensuring every successful payment gets logged against the right referral so we can handle pay outs accurately later on.