WEBVTT 00:01.810 --> 00:07.810 So this time around, we want to finish up or at least continue working on our get payment intent handler 00:07.810 --> 00:12.940 that will actually contact Stripe, get a payment intent and do all of the things we needed to do. 00:14.080 --> 00:17.610 Before we do that, there's one slight change I want to make. 00:17.620 --> 00:23.080 If you recall, a while ago we created inside the internal folder a package called KO'd. 00:23.560 --> 00:29.710 And we're going to run into a naming conflict between the card that exists within the strike package 00:29.710 --> 00:31.960 and the card type that we've defined ourselves. 00:32.410 --> 00:33.300 So I'm going to fix that. 00:33.310 --> 00:34.240 I'm going to do it. 00:34.240 --> 00:40.360 And this is one of the disadvantages of Visual Studio Code, is it doesn't refactor packages very well, 00:40.360 --> 00:41.380 but we can do it manually. 00:41.410 --> 00:46.200 So in the internal package, create a new folder and just call it cards, plural. 00:47.440 --> 00:55.690 OK, then open up the cargo, select all and copy and inside the cards folder, create a new file called 00:55.930 --> 00:56.890 Kurtzberg. 00:56.890 --> 01:04.930 Go and paste that in there, then go to the very top of that new file and add an S to cards and now 01:04.930 --> 01:12.100 we can delete the entire card directory off the cards plural the card singular and this will get around 01:12.340 --> 01:16.590 that problem we're going to run into with the name. 01:17.230 --> 01:18.770 OK, so I'll close that cards file. 01:19.780 --> 01:21.280 Now back inside my handler. 01:21.460 --> 01:22.890 What I know what's going to happen. 01:23.380 --> 01:26.970 This is going to be called not using GIs as it is right now. 01:26.980 --> 01:32.170 It's going to be called using post and it's going to consist of the body of the request or the body 01:32.170 --> 01:35.530 of the request that we get will actually consist of adjacent file. 01:35.740 --> 01:39.850 And that Jason file will conform to the standard that we have in straight payload. 01:39.970 --> 01:47.530 So let's create a variable, which I'll call our payload of type straight pallet, and then we want 01:47.530 --> 01:51.390 to get the body of that request and decoded into our variable payload. 01:51.520 --> 01:57.040 We can do that really easily and go error is assigned the value of from the JSON package. 01:57.040 --> 02:03.430 We want a new decoder and we're going to be decoding our body, the request body, and we're going to 02:03.430 --> 02:06.430 decode that into a reference to payload. 02:06.940 --> 02:12.940 And if everything went as expected, we now have our payload variable populated with both currency and 02:12.940 --> 02:13.270 amount. 02:14.440 --> 02:17.500 So we'll check for an error if error is not equal to nil. 02:18.100 --> 02:24.370 And right now I'm just going to say APTA error log, print one and I'll print out the error and then 02:24.370 --> 02:26.280 I'll return, which is not very polite. 02:26.290 --> 02:31.000 We should be sending back a properly formatted JSON error message, but we'll take care of that later. 02:31.180 --> 02:35.560 OK, now if you'll recall, actually you don't have to recall. 02:35.560 --> 02:36.790 It's right there in front of us. 02:37.120 --> 02:40.540 The currency is a string and the amount is a string. 02:40.540 --> 02:43.610 And I need that amount to be a numeric value and that's pretty easy. 02:43.750 --> 02:51.520 So what I'll do is simply say amount and again, we'll check for an error is assigned the value of and 02:51.520 --> 02:56.890 we'll use the Sterkel package A2 Alpha to integer. 02:56.900 --> 03:00.760 How we're going to convert payload don't amount. 03:01.270 --> 03:05.320 And again we'll check for an error and I'll just copy and paste this error handling code which will 03:05.320 --> 03:08.000 be replaced with something better later on in any case. 03:08.920 --> 03:12.560 So now we have the amount, so we have the currency and we have the amount. 03:13.300 --> 03:18.640 So now we're going to take advantage of that cards dot card type that we have. 03:18.640 --> 03:24.130 So we'll create a variable, which I'll call card and it's going to be assigned the value of from the 03:24.130 --> 03:31.030 cards package and we want card and I might have to do the manual import number, seem to get it for 03:31.030 --> 03:33.310 me and we'll populate its various fields. 03:33.640 --> 03:35.500 So we need to put in the secret. 03:38.290 --> 03:43.540 And that will come from our app, dot config, dot stripe, dot secret. 03:46.200 --> 03:56.940 We need the key and our key comes from Aptos Config Strachey, and our currency comes from currency. 03:58.470 --> 03:59.560 Payload currency. 04:00.690 --> 04:01.230 There it is. 04:01.260 --> 04:06.840 So now we have our card type and of course, that means that we actually have access to all of the necessary 04:06.870 --> 04:08.020 methods that are on that. 04:08.190 --> 04:15.240 So so at this point, we've created an instance of that card type and we have access to all of the methods 04:15.240 --> 04:16.000 that are built into it. 04:16.530 --> 04:23.010 So first thing I'm going to do is I'm going to declare a variable, which I'll call OK, and I'll assume 04:23.010 --> 04:25.320 that's going to be true to start with. 04:25.350 --> 04:26.890 So everything's going to be all right. 04:27.270 --> 04:34.350 Now, I'll call that function charge in cards and get the three variables that are returns. 04:34.380 --> 04:36.370 The first one, if you recall, is payment intent. 04:36.390 --> 04:41.760 So I call upon the second one is a message which is just a string and the third one is an error. 04:41.980 --> 04:47.650 And those are all going to be populated by calling card dot church. 04:47.670 --> 04:49.560 I'll use the alias because I think it's more readable. 04:50.010 --> 04:51.810 And that requires two variables. 04:52.020 --> 04:58.950 The currency which comes from payload currency and the amount which we just created ourselves. 05:00.720 --> 05:02.010 Now we're going to check for an error. 05:02.160 --> 05:08.490 If error is not equal to true to nil, then I'm not going to just send back a principled error message 05:08.490 --> 05:09.860 and return instead. 05:09.870 --> 05:12.600 In this case, I'm going to say OK is equal to faults. 05:12.600 --> 05:18.360 In other words, something went wrong because in this case it may not be an error, it could be a card 05:18.360 --> 05:19.280 decline message. 05:20.160 --> 05:21.900 So let's just see what we're going to do here. 05:21.900 --> 05:25.860 If OK, if everything is OK, then we should charge the credit card. 05:25.860 --> 05:29.740 We have our payment intent, in other words, otherwise we're going to do something else. 05:30.060 --> 05:36.170 Now, if we've actually succeeded in our message, then we want to send some Jason back. 05:37.170 --> 05:39.090 So let me scroll up here a little bit. 05:39.690 --> 05:44.400 And what I'm going to send back, if the charge succeeded, is the actual payment intent, because that's 05:44.400 --> 05:45.990 what I want on the other end. 05:45.990 --> 05:46.700 On the front end. 05:46.740 --> 05:53.970 OK, so all I have to do is say out an error or assign the value of Jason Mercial and I'll mercial in 05:53.970 --> 05:55.140 debt to make it readable. 05:55.170 --> 06:01.350 OK, pie with no prefix and some spaces to make it all format nicely. 06:03.290 --> 06:05.960 And again, I'll find my error handling code up here. 06:06.530 --> 06:07.190 Copy that. 06:08.210 --> 06:12.980 Come down here and paste it and then I'll set the header. 06:13.210 --> 06:14.630 I can copy that from down here. 06:14.990 --> 06:16.100 Copy these two lines. 06:16.760 --> 06:19.940 Come back up here and put it in there. 06:23.920 --> 06:31.360 So once we call card charge, and that's the one that actually connects to Stripe for us, we're going 06:31.360 --> 06:37.120 to get a payment in 10 back if our payment intent comes back with no error, that OK is true. 06:37.130 --> 06:42.460 So on line 50, we check for that and a line fifty one, we create our Jason. 06:42.460 --> 06:45.300 We want to send back and then we just set the header and send it out. 06:45.760 --> 06:47.940 That's if things work as expected. 06:48.400 --> 06:55.390 Now if things don't work as expected and I'm going to take all of this, cut it out of there, 06:58.300 --> 07:01.710 paste it into here and modify it appropriately. 07:02.860 --> 07:09.130 So first thing is our Jason response is going to be, OK, false because things didn't work as expected. 07:09.470 --> 07:13.510 The credit card didn't get approved or whatever the case may be. 07:13.900 --> 07:21.640 The message will be MSG, which we got back up there on line forty five when we called card charge and 07:21.640 --> 07:28.120 the content, I'll just leave empty lot that I don't have to do that, but I'm going to just to make 07:28.120 --> 07:34.370 it very readable that we do a commercial indent, we send it off and set our header and write it out 07:34.370 --> 07:35.020 to the browser. 07:35.030 --> 07:38.060 So this is at least a good start for what we want to do. 07:38.440 --> 07:42.700 Now, there's one other thing I want to take care of here, and that is right at the very top. 07:43.750 --> 07:51.010 If you recall when we looked at the output from the browser last time we saw, OK, and that was set 07:51.010 --> 07:51.490 to true. 07:51.820 --> 07:56.530 And then we saw empty values for these and empty string and empty string and the value zero. 07:56.530 --> 07:57.580 And then a lot of cases. 07:57.580 --> 08:02.290 In fact, in most cases, if there's no value for a particular field, you don't even want to bother 08:02.290 --> 08:03.310 putting it in JSON. 08:03.700 --> 08:05.260 And you can do that and go really easily. 08:05.260 --> 08:07.810 You put a comma after what you want to call it in Jason. 08:08.140 --> 08:09.790 And then the keyword limit. 08:10.420 --> 08:12.010 Empty, empty what? 08:12.310 --> 08:20.230 I'm going to copy and paste it for the next two as well, and that will make things ever so much nicer. 08:20.710 --> 08:25.570 OK, so let's make sure this compiles, I'll say make a stop to make sure everything is stopped and 08:25.570 --> 08:29.760 it is and make start underscore back. 08:30.490 --> 08:31.510 Let's hope it compiles. 08:33.870 --> 08:35.100 It does perfect. 08:35.130 --> 08:42.420 OK, so in the next lesson, we'll go modify that route to be a post instead of aget and we'll go write 08:42.420 --> 08:46.770 some JavaScript on the front end and see if we can get a single transaction to go through.