So our next task is to tie everything together.
And that means stripe notifying our back end API.
Once a payment has been received, and then we can match that to the order and confirm the payment has
been received as well.
So let's head back to VS code to configure this.
And I'll close all the current tabs down.
And we'll open up our payments controller to do this.
And inside here we're going to create another endpoint.
And it's going to be an HTTP post.
And we'll call it just simply web hook.
This is what stripe will use to call our API.
So we'll use public async task.
And all we need to do to confirm with stripe that we've received their webhook and processed.
It is just to return an okay response.
So we'll just use an I action result for this particular endpoint and we'll call it Stripe Webhook.
And inside here the way that stripe is going to send this request.
We need to access the request body.
And to do that we're going to need to use a stream reader.
So we'll use var JSON.
As we'll need to format this request into something that we can use inside our controller.
And we'll use Await new stream Reader.
And we'll pass in the request dot body.
And then we'll use the read to end async so that we're effectively reading that request body.
And then we'll add a try catch block.
And inside here we'll say var stripe events equals.
And we'll create a method to construct the stripe event.
And we'll pass in the JSON here.
And we'll put our cursor inside this method.
And we'll generate this method.
And inside here we will return effectively an event which we get from stripe.
so please ensure you bring stripe in as well at the top.
So we've got access to this functionality.
So inside this construct stripe event we'll also do this inside a try catch block as well.
And we will use or we will return from this method.
We will return event's utility again something we use from stripe.
And we'll construct the events.
And we'll pass in JSON as a second parameter.
Here we need to pass a stripe signature header.
And this is what we use to ensure our API can trust the request that is coming in from stripe.
So we're going to look for a request header.
And we'll specify request dot headers.
And inside here we're going to specify stripe dash signature.
That's what we're going to look for.
And then to the right of the square brackets we're going to read from our configuration to get what
this value needs to match.
So we're going to need access to our configuration inside here.
Let's just bring this down.
And I'm going to add on to this the AI configuration.
Ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay ay.
And call it config.
And back down in our code.
Let's move this down onto the next line.
And we're going to compare this to the config.
And we're going to look for stripe settings colon w h secret.
And this is another secret that we need to get from our stripe dashboard.
We don't have it yet but we will do soon.
And in the catch block let's just remove system and say exception x.
And we'll also bring our logger into this actually so we can output what this is into our console.
So I'll specify the ilogger.
And this is going to be logging against the payments controller.
And we'll call it logger.
And I'll bring that down as well.
And then we'll scroll back down to our construct Stripe event method.
And inside the catch block here we'll just use our new logger dot log error.
And the first parameter can be the exception.
And we'll just say failed to construct stripe events.
And we'll throw a new stripe exception and just say invalid signature.
So this is the part where we effectively confirm that we trust what is coming from stripe.
Because what we're doing here is confirming that we've received payment on our back end API from stripe.
So once the user has confirmed their payment, then stripe is going to send a notification to our API.
And we'll use this to mark the order as payment received.
And then we kick off another process that we're not covering on this course, but that would then kick
off the next process to start packing the order in the warehouse and then shipping it to the customer
and so on and so forth.
But it's important that we trust what we get back from stripe, and using this system ensures that we
can trust stripe.
So back up to our stripe webhook line.
And now we have the stripe events.
We'll check the stripe event.
So if the stripe events dot data dot object and we can use is not payment intent intents, then we can
return a bad request and just say invalid.
Event data.
And then we can further check this.
And using this approach here gives us access to the payment intent using the word intent.
So we can check to see if the intent status.
Is equal to succeeded.
And do we get any intellisense for this?
Oh, no.
But that's what we're looking for here.
We're looking for the word succeeded, and it doesn't look like we're going to get any compiler support
here.
So important to be careful with this particular spelling of this word.
And if it is succeeded then we're going to await and we're going to create a method called handle payment
intent succeeded.
And we're going to pass in the intent to that method.
Otherwise we'll add an else and we'll use await and handle payment intent failed.
And we'll pass the intent through to that method.
So let's put our cursor inside the first method and generate that and do the same for the second method
as well.
And below this we can return okay.
And let's just catch the exception and populate this block of code as well.
We'll use exception x again.
And we'll use our logger inside here as well.
In fact let's check for the 40 stripe exception first and call this x and we will use logger and log
error and specify x and just say stripe webhook error.
And if we do have a stripe exception then we're going to return a status code.
And we're effectively going to return a 500 internal server error from this.
But we need to get this from status codes, which is something that we bring in from DotNet.
And we can use the status 500 internal server error and just say webhook error.
And this is what we're sending back to stripe.
So effectively in the stripe dashboard with the webhook system.
Then we'll be able to see if the webhook did have an error and whilst we can't throw an exception to
stripe, we can return a 500 error and that's what we would do in this scenario and then we'll catch
any other type of exceptions below this.
So we'll have another catch, and we'll use this one for just any other type of exception and say x
for this as well.
And I'll just copy what we're doing here and paste it in this catch block.
And if it's not a stripe error then it's going to be an unexpected error.
So I'll specify an unexpected error has occurred.
And I'll just say unexpected error.
Okay.
So that's halfway there.
We've still got a couple of methods to populate what we want to do if the intent fails and what we want
to do if the intent succeeded.
And we'll populate these two methods in part two of this lesson.