This episode is for members only

Sign up to access "Build a Pay Once For Access App" right now.

Get started
Already a member? Sign in to continue
Playing
11. Handling a successful payment intent

Transcript

00:00
Now that we have our webhooks being delivered, we actually need to handle the correct method
00:05
and we're going to future proof this in case you need to listen to any other webhook events. Of course at the moment we just have the one but in the future you'll probably want to handle some others. Okay, so the first thing that we need to do within our webhook controller is go ahead and actually grab the content or the payload of the request. Now we can see this if we just come over
00:27
to the webhook page here by clicking on any of these requests that have been sent, webhooks that have been sent and you can see the pretty much all of the data that is sent through this webhook. Now what we want to grab here is the type and then we want to defer this to a method within this controller and that means that we can set up a specific method to handle the payment
00:50
intent succeeded event type, but we can also create other methods within this controller to handle others if we need them in the future. So to do this the first thing that we need to do is grab the payload. To do this we're going to go ahead and JSON decode from the request and we're going to use get content and we're going to pass true in here and that's going to give us back
01:12
an object or an array that we can use to start to grab this data from. Now what we want to do like I said is create out a method in here for each handle for each type. So we want to prefix this with handle and for payment intent succeeded we want this to be pretty much exactly this but without the dot and without the underscore and we want this as a studly case. So we want this
01:38
to be handle payment intent succeeded like so. So to test this works we can just go ahead and maybe log out info in here and just say handled and we don't necessarily need to pull in the log class here but we need now need to work out this method so we can actually call it properly. So let's go ahead and define our method variable in here. We know that each of these methods that
02:07
we want to handle starts with the handle word and then in here we want to go ahead and grab the payload type which is what we can see just here. We want to replace out the dot on the underscore so let's go ahead and do that now. We can use string replace the native php function to do this and we want to go ahead and replace out a dot with an underscore and then what we can do
02:36
is use Laravel's studly helper with the string class to grab that and what we could actually do is maybe log this method just to make sure that's working properly before we continue. So let's go through the process of just making a payment again just to check this out so let's go ahead and enter in our car details and make sure they are
03:08
properly inputted again and there we go. If we head over to our Laravel log there is the method that we want to call and you'll notice that's exactly the method that we defined. So now that we have that correct method we just need to check if this method exists on this class so this and method then we want to go ahead and invoke it passing through the payload.
03:37
So we can go ahead and return this we can go ahead and dynamically call that method and we can pass in the payload so now in here we can accept the payload in and do what we want. So let's actually dump out the payload go through the process of paying again and just making sure this works. So if we go over to our Laravel log and just clear this out if we head over to our
04:03
payment section again and we enter our details hit make payment and come over to that Laravel log we should see this update with the payload with all of that information in and you'll also notice that we've got that user id in there so we can go into that to look that user up and then upgrade them and that's what we're going to be doing in the next episode.

Episode summary

In this episode, we dive into handling successful payment intents from Stripe using webhooks. Now that our webhooks are being delivered, we set ourselves up to not just handle the current event (like payment_intent.succeeded), but also to be able to handle more events in the future without much refactoring.

We start by grabbing the payload from the webhook request and breaking down how to extract the event type. Then, we set up a system in our webhook controller to automatically route each event type to its own method - for example, handlePaymentIntentSucceeded for a payment_intent.succeeded event. We use some handy string manipulation to transform the event type into the appropriate method name, and then call that method dynamically if it exists.

Throughout the episode, we add checks and log output to make sure everything is working as expected. Once we trigger another payment, we check our logs and see that the correct method is getting called and the full payload is available, including the user ID—setting us up perfectly for the next episode where we'll use this data to actually upgrade a user after they've paid.

So, we've just built a neat event handling system that makes it super easy to expand as we need to listen to more Stripe webhook events. Next up: taking action when a payment is successful!

Episode discussion

No comments, yet. Be the first!