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.
15 episodes1 hr 11 mins

Overview

Let's do payments right with the Stripe Payment Intent API, and build an app where customers can pay to access a members area.

We'll cover setting up a Payment Intent, process payments correctly with authorization, handle declines, and securely respond to a Stripe webhook to upgrade a member.

This course is for you if:

  • You want to process payments to upgrade customers for access
  • You need to learn how to properly process one-off payments with Stripe
  • You need a refresher on the Payment Intents API
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.