Okay, it's time to fire up the debugger and see what's going on.
Now, we did give ourselves a clue by giving ourselves different error messages depending on where it
got to in the process, and this is the message we got back from the 400 seconds.
So if we go back to our code and we take a look at the payments controller, then we can see that it's
list line problem creating payment intent which matches.
Oh no.
In fact it's a problem updating the basket with the intent.
Ah, so I think what's going on here is that our basket didn't get updated.
It stayed the same.
So when we got to list line the save changes this was zero, which means we didn't have any changes
to save into our database.
So that request would have actually worked.
And we can confirm this in the stripe dashboard.
Actually, if I refresh this page, then what we should find is that that amount has increased to 300
because stripe is separate to our database.
And sure, we can see that's 300, but it's incomplete.
So what do we do about this then?
Because we do need to save changes if there have been changes.
But if we already have the payment intent ID, then we do not set it with a new intent ID so there's
no change there.
Same goes for the client secret.
But then we've got this call to save changes to our API.
So I think we know what the problem is and we'll deal with that soon.
But let's add a breakpoint inside our payments controller.
Let's take the opportunity to see what's happening as we update a payment intent.
So I'm going to go back and I'm going to restart my API regardless.
And I'll just use control R to restart it before I start the debugger.
I'd prefer not to be any hangovers from hot reloading, although I don't think there would be on this
occasion anyway.
And once it has restarted, let's go and attach the.
I'll close the others Verse down and just keep this one open and we'll go and attach our debugger to
the process.
So I've got the dotnet core attached.
I'll hit the play button, search for API, click on API and let the debugger start up.
And I'll head back to postman and let's go and add.
I'll just keep adding an additional one of these items to the basket.
So now that's going to raise the price to 400.
And I've still got an old breakpoint in here.
So I'm just going to play this request.
I didn't want to stop on this one.
So I just hit play and go back to postman so that I get the item added to the basket.
Now we've got the quantity of three, and then I'll go and click the create Payment intent.
Click send and off we go to our code inside the debugger.
So this is going to go and get our basket.
If we go and step over this then we can see inside here we have our baskets.
We've already got the client secret and the payment intent, and we've got our item inside here, which
is a quantity of three.
Great.
And we'll step over the intent or step over the basket equals null.
And then we've got our code to go and get our payment intent.
Let's use step inside so that we get taken to our payment service.
On this occasion.
And step over and step over again.
This will give us our payment intent service.
That's fine.
This is going to create a new empty payment intent.
Okay.
That's fine as well.
Work out our subtotal.
If we take a look at this one and hover over the subtitle we can see that's for 500 0 or $450.
And that should mean our delivery fee is going to be zero, which it is.
And we should step over this one, this if statement and go on to the update one.
So we do and we go to the else statement which is going to update our payment intent with its options.
And if we take a look at the options, then there's a whole bunch of options we can send up with this.
But our needs are quite simple.
Just the amount is all we're interested in there.
And then we can step over again and again and go back to our payments controller now that we have the
payment intent.
So these are the problematic lines.
If we go and step over this then our basket isn't going to be updated by anything that we do here.
If we do take a look, we can already see that we have these values inside our basket.
So if we step over this and step over this, then nothing changes inside this entity.
So what are our options then for this.
Well we could use a conditional before we get to the saving of the changes.
If we do have the payment intent ID and the client secret, then there's no need to call this line because
this line gives us the problem.
If I step over this line, then what we see as the results is false, because we haven't saved any changes
into our database.
So perhaps a conditional is the approach we can take here, and what we'll do to check to see if we
should save changes.
We can use the Entity Framework change tracker to see if there are any changes that could be saved in
memory, and then we can call our save changes method.
So just above the results here I'll add an if statement.
And we will use the context change tracker.
And we'll use its has changes method.
And if it does then we'll use our two statements here to save the changes into the database.
And if not then we're going to skip over that and just return the basket DTO as it is.
So if we go back and test this, in fact, let's just use control R just in case Hot Reload is playing
up.
And I'll go back to postman and we'll just repeat the request to add an item to the basket.
So I'll click send and we get the 201 created.
And I've gone up to quantity of six because I was doing a bit of off screen troubleshooting.
And we're returning the client secret and the payment ID.
And what we should see if we go back to stripe is we now have an updated payment intent ID in my case
$750.
So that's the outcome that we're hoping for.
And we've now set up the payment intent system on our API server.
So the next step and the more significant steps in what we're doing inside this section is on the client
side.
And we're going to set up our checkout and use all the stripe goodies that we get related to payments
and addresses, so that we can make a payment for this order.
And we'll start taking a look at that next.