WEBVTT

00:00.020 --> 00:03.470
Okay, it's time to fire up the debugger and see what's going on.

00:03.470 --> 00:09.650
Now, we did give ourselves a clue by giving ourselves different error messages depending on where it

00:09.650 --> 00:14.330
got to in the process, and this is the message we got back from the 400 seconds.

00:14.330 --> 00:20.990
So if we go back to our code and we take a look at the payments controller, then we can see that it's

00:20.990 --> 00:24.950
list line problem creating payment intent which matches.

00:24.980 --> 00:25.460
Oh no.

00:25.460 --> 00:28.730
In fact it's a problem updating the basket with the intent.

00:29.090 --> 00:34.280
Ah, so I think what's going on here is that our basket didn't get updated.

00:34.280 --> 00:35.390
It stayed the same.

00:35.390 --> 00:41.660
So when we got to list line the save changes this was zero, which means we didn't have any changes

00:41.660 --> 00:43.100
to save into our database.

00:43.100 --> 00:45.890
So that request would have actually worked.

00:45.890 --> 00:48.410
And we can confirm this in the stripe dashboard.

00:48.410 --> 00:53.960
Actually, if I refresh this page, then what we should find is that that amount has increased to 300

00:53.990 --> 00:56.330
because stripe is separate to our database.

00:56.330 --> 01:00.930
And sure, we can see that's 300, but it's incomplete.

01:01.080 --> 01:02.910
So what do we do about this then?

01:02.910 --> 01:06.990
Because we do need to save changes if there have been changes.

01:06.990 --> 01:12.840
But if we already have the payment intent ID, then we do not set it with a new intent ID so there's

01:12.840 --> 01:13.830
no change there.

01:13.860 --> 01:15.990
Same goes for the client secret.

01:15.990 --> 01:21.180
But then we've got this call to save changes to our API.

01:21.210 --> 01:24.630
So I think we know what the problem is and we'll deal with that soon.

01:24.630 --> 01:28.920
But let's add a breakpoint inside our payments controller.

01:28.920 --> 01:34.230
Let's take the opportunity to see what's happening as we update a payment intent.

01:34.710 --> 01:39.420
So I'm going to go back and I'm going to restart my API regardless.

01:40.620 --> 01:44.100
And I'll just use control R to restart it before I start the debugger.

01:44.100 --> 01:49.140
I'd prefer not to be any hangovers from hot reloading, although I don't think there would be on this

01:49.140 --> 01:50.550
occasion anyway.

01:50.820 --> 01:55.080
And once it has restarted, let's go and attach the.

01:55.290 --> 02:00.900
I'll close the others Verse down and just keep this one open and we'll go and attach our debugger to

02:00.930 --> 02:01.470
the process.

02:01.470 --> 02:02.850
So I've got the dotnet core attached.

02:02.850 --> 02:10.650
I'll hit the play button, search for API, click on API and let the debugger start up.

02:11.250 --> 02:14.910
And I'll head back to postman and let's go and add.

02:14.940 --> 02:18.750
I'll just keep adding an additional one of these items to the basket.

02:18.750 --> 02:23.550
So now that's going to raise the price to 400.

02:23.910 --> 02:26.070
And I've still got an old breakpoint in here.

02:26.070 --> 02:27.300
So I'm just going to play this request.

02:27.300 --> 02:29.070
I didn't want to stop on this one.

02:29.070 --> 02:34.440
So I just hit play and go back to postman so that I get the item added to the basket.

02:34.470 --> 02:39.630
Now we've got the quantity of three, and then I'll go and click the create Payment intent.

02:39.630 --> 02:45.870
Click send and off we go to our code inside the debugger.

02:45.870 --> 02:48.030
So this is going to go and get our basket.

02:48.030 --> 02:53.970
If we go and step over this then we can see inside here we have our baskets.

02:53.970 --> 02:59.890
We've already got the client secret and the payment intent, and we've got our item inside here, which

02:59.890 --> 03:01.000
is a quantity of three.

03:01.030 --> 03:01.870
Great.

03:01.870 --> 03:06.730
And we'll step over the intent or step over the basket equals null.

03:06.730 --> 03:10.060
And then we've got our code to go and get our payment intent.

03:10.090 --> 03:13.840
Let's use step inside so that we get taken to our payment service.

03:13.870 --> 03:15.310
On this occasion.

03:15.310 --> 03:18.640
And step over and step over again.

03:18.640 --> 03:20.860
This will give us our payment intent service.

03:20.860 --> 03:21.910
That's fine.

03:22.150 --> 03:25.030
This is going to create a new empty payment intent.

03:25.060 --> 03:25.510
Okay.

03:25.540 --> 03:26.710
That's fine as well.

03:26.740 --> 03:28.270
Work out our subtotal.

03:28.270 --> 03:36.100
If we take a look at this one and hover over the subtitle we can see that's for 500 0 or $450.

03:36.100 --> 03:40.570
And that should mean our delivery fee is going to be zero, which it is.

03:40.810 --> 03:45.370
And we should step over this one, this if statement and go on to the update one.

03:45.370 --> 03:52.300
So we do and we go to the else statement which is going to update our payment intent with its options.

03:52.300 --> 03:56.650
And if we take a look at the options, then there's a whole bunch of options we can send up with this.

03:56.650 --> 03:58.810
But our needs are quite simple.

03:58.810 --> 04:01.450
Just the amount is all we're interested in there.

04:01.450 --> 04:11.320
And then we can step over again and again and go back to our payments controller now that we have the

04:11.320 --> 04:12.550
payment intent.

04:12.550 --> 04:14.440
So these are the problematic lines.

04:14.440 --> 04:21.160
If we go and step over this then our basket isn't going to be updated by anything that we do here.

04:21.160 --> 04:26.170
If we do take a look, we can already see that we have these values inside our basket.

04:26.170 --> 04:34.540
So if we step over this and step over this, then nothing changes inside this entity.

04:34.540 --> 04:39.280
So what are our options then for this.

04:39.280 --> 04:44.470
Well we could use a conditional before we get to the saving of the changes.

04:44.470 --> 04:50.530
If we do have the payment intent ID and the client secret, then there's no need to call this line because

04:50.560 --> 04:51.920
this line gives us the problem.

04:51.920 --> 05:01.070
If I step over this line, then what we see as the results is false, because we haven't saved any changes

05:01.070 --> 05:02.600
into our database.

05:02.600 --> 05:08.720
So perhaps a conditional is the approach we can take here, and what we'll do to check to see if we

05:08.720 --> 05:10.190
should save changes.

05:10.190 --> 05:17.060
We can use the Entity Framework change tracker to see if there are any changes that could be saved in

05:17.060 --> 05:20.030
memory, and then we can call our save changes method.

05:20.030 --> 05:23.480
So just above the results here I'll add an if statement.

05:23.480 --> 05:27.230
And we will use the context change tracker.

05:27.230 --> 05:30.050
And we'll use its has changes method.

05:30.050 --> 05:39.110
And if it does then we'll use our two statements here to save the changes into the database.

05:39.110 --> 05:46.760
And if not then we're going to skip over that and just return the basket DTO as it is.

05:46.760 --> 05:53.870
So if we go back and test this, in fact, let's just use control R just in case Hot Reload is playing

05:53.870 --> 05:54.740
up.

05:55.670 --> 06:01.250
And I'll go back to postman and we'll just repeat the request to add an item to the basket.

06:01.250 --> 06:04.430
So I'll click send and we get the 201 created.

06:04.430 --> 06:09.350
And I've gone up to quantity of six because I was doing a bit of off screen troubleshooting.

06:09.350 --> 06:13.130
And we're returning the client secret and the payment ID.

06:13.340 --> 06:19.940
And what we should see if we go back to stripe is we now have an updated payment intent ID in my case

06:19.940 --> 06:21.620
$750.

06:21.620 --> 06:24.650
So that's the outcome that we're hoping for.

06:24.680 --> 06:28.460
And we've now set up the payment intent system on our API server.

06:28.460 --> 06:34.520
So the next step and the more significant steps in what we're doing inside this section is on the client

06:34.520 --> 06:34.910
side.

06:34.910 --> 06:41.210
And we're going to set up our checkout and use all the stripe goodies that we get related to payments

06:41.210 --> 06:45.680
and addresses, so that we can make a payment for this order.

06:45.680 --> 06:48.080
And we'll start taking a look at that next.
