Okay.
Our last task for this particular section is to test our new webhook that we have now created.
And in order to do that, well, how do we do that?
Because stripe is a service out there on the internet.
Our application is an application running on our local developer machine on localhost, which means
stripe cannot access our local developer machine unless we did some significant routing jiggery pokery.
But we're not going to do that.
But stripe do provide a way that we can test this.
And if we go back to stripe itself and let's go to inside the more section and in fact not the more
section, this does change sometimes.
So we need to go to our developer section on the bottom left hand side.
At the time of recording it could change, but do hunt around for a link that takes you to developers,
and then we can get to the webhooks section and I'll just turn off their advert for whatever that is.
And inside here we're going to create an event destination.
And this is to automatically trigger business workflows in our application to response to events sent
from stripe, so we can send stripe events to webhook endpoints.
And that's what we're going to do.
So we'll create an event destination.
And we've got an option to test in a local environment.
Now in order to do this we have to download the stripe CLI.
So let's click this link and see what we need to do.
Because this is going to be different between Mac, Windows and Linux.
But there is options for all three operating systems.
So we need to install the stripe CLI.
Now if you can use homebrew on your computer, if you're on a mac or Linux then this would be an option.
If you're on windows, Sqoop is an option.
And if you're on windows as well, then there's an option to install this directly without Sqoop.
But if you're on Linux or Mac, I'd suggest using homebrew if you've got that installed already.
Very simple to just use brew install to get this.
And if you're on windows, then Skype is probably the easiest way to get this installed.
Once we have this installed, then we can log into the stripe CLI and that's what we'll do.
And do I have this available?
Well I'm just going to copy this link for homebrew.
Go back to VSCode and go to my terminal.
The app is asking to restart.
I'm going to say yes to this anyway, but then I'm going to go to a spare tab and I'm going to install
because I do have homebrew installed on my computer, the stripe CLI, and that will probably take a
moment or two.
So I'll pause the video and then I'll come back once this is available for use.
Okay.
So that did take a little while, but now that is available to install.
And if we go back to the documentation, our next step is to log in and authenticate with our stripe
user account to generate a set of restricted keys.
So we'll use the stripe login next.
And I'll use this command.
And what this typically does is open up stripe in the browser, and I'll do that and log in in a separate
window.
And once you've gone through their authentication process, then you'll be logged in and you'll see
something like this.
And we get 90 days after which this key will expire.
And we need to re-authenticate.
So once we have authenticated then we can listen for stripe events using the stripe CLI.
Because obviously stripe on the internet cannot reach our API server running locally on our developer
machine.
So in order to listen to events we use stripe listen.
And then we can forward events to our API server.
So we use the dash F switch to forward any events and tell it where we want them to go.
So it's going to be https forward slash forward slash localhost colon 5001 forward slash API forward
slash Payments.
Forward slash webhook.
And obviously this needs to be accurate if you want this to work and needs to exactly match the endpoint
address.
And then we can specify which events we're interested in listening to.
Now there's two events that we're interested in here.
So we specify dash e.
And the first event is payment underscore intent period succeeded.
And then we add a comma.
And then we can specify the second event we're interested in which is going to be payment underscore
intent period payments underscore failed.
Now I believe you'll get an error if you've got a typo in the event.
And let me just test that theory, I'll make a very obvious spelling mistake here and see if I do get
notified that this is not a genuine event.
And sure we do.
So it tells us which one is not a valid event, but it still starts up.
And also what it gives us is our webhook sign in secret.
And that's what we'll need to configure as well in our application, so that we can trust stripe when
it comes to receiving that webhook event.
So I'm just going to stop this.
I just wanted to confirm that we do get information about an incorrect event that we're trying to listen
to.
So I'll put it in correctly this time and press return.
And now this is listening.
So the webhook secret doesn't change between stopping and restarting this.
So it's pretty consistent on what we use here.
So I can copy this webhook sign in secret into my clipboard.
And we can go to our app Settings development.
Jason and I can put in here the secrets and paste in the key.
And obviously this is exactly what we specified in our payments controller.
And if we take a look at the stripe or construct stripe event then we've got stripe settings colon w
h secret layer.
What this should all mean is that when I create a new order after the payment has been confirmed, then
stripe should send us the event.
And let's test that theory.
So I'll just go back to the developers tab and I'll click done because we've got the stripe listener
and we're forwarding it to.
So let's go back to our application.
I'll close the spare tab I had open for this.
We'll do this properly I'll go back to the catalog.
I've got one item in my basket just now.
Let's add something else to the cart as well.
I'll click on the baskets.
I'll click on checkout.
I'll keep the same address.
Click next.
Add the card number of 42, 42, 42, 42, 42, 42 and one to some date in the future.
And then one, two, three.
I'll click next and then I'll pay, and I should get redirected to the checkout success page.
And I do, and I see the amount there.
If I then go and view my order, what I would expect to see is that payment has been received.
But first of all, before I do that, let's just double check the logs, because what we should see
inside the logs is the event being received and then forwarded to our webhook.
And we can see that we've got a 200 back from our webhook, a 200.
Okay.
That means that it has processed this specific event.
And if we go back and check our browser and then view this order, then we can see that the payment
has been received.
So all of our ordering functionality appears to be working.
Let's see what happens if I do not succeed in the payment.
So I'll just go back to the catalog.
I'll add an item into the cart.
I'll go to the basket.
I'll go to checkout.
And I'll click next.
And I'll pick a failing card this time.
And I'll go back to the test cards and pick a declined payment card and insufficient funds, and go
back to the store and paste this in.
And once again, some random dates in the future.
And the security code, I'll click next and click pay.
So our payment goes to stripe.
But we do get a declined payment.
And if I go and take a look at my orders then what we should see is that we've got the payment failed
inside the orders there, which is what we would expect to see.
And let's go back to the basket and the checkout.
I just want to make sure that we can resubmit this order with a valid card and ensure that we get the
payment received so it gets updated.
So I'll click next and say 42, 42, 42 all the 40 twos, which we know is a valid card.
And then click next and then click pay and we go back to stripe.
And this time we get the order correctly created.
And if we view the order then we'll see that we've got the updated status of payment received.
And if I go back to all of my orders then ah, that's been cashed of course because of RTK query.
So that is something that we could correct.
Because when we receive our orders, we're obviously receiving this from cash.
And that could confuse the user.
But obviously if we refresh the page then we're not going to receive it from cash.
And we get the payment received inside there.
So I'll let the facts that that's staying in cash to my to do list for something to consider looking
at before we wrap up this course, but please do feel free to see if you can get this updating inside
the cache based on what we've done previously.
I'll leave that up to you for now.
I've added this to my to do list because it's not something that I considered before I started recording
this lesson, but it is on my to do list now.
And that is the final action of this section.
I'm just going to quickly check the console, make sure there's nothing unexpected hanging around in
the console.
From what we've done here, that looks fine.
Still ignoring this stuff on the right hand side from stripe that I cannot do anything about.
And we're going to consider this the end of section.
So once again we've reached the milestone.
I've still got my stripe CLI running.
I can leave this running.
It doesn't do any harm.
And I'm going to go to the version control tab and stage the changes and give this a commit message
of end of section 11.
And I'm going to commit and sync the changes up with GitHub.
And we'll wrap up with a summary.