WEBVTT 00:02.120 --> 00:07.310 So this time around, what we want to do is to start to work on converting our virtual terminal page 00:07.640 --> 00:14.600 from a form submission to the front end to instead a submission to a protected route on the back end 00:14.600 --> 00:15.350 of the API. 00:15.950 --> 00:21.620 So what I'll do, first of all, is create a route to a non-existent handler and I'm in my reach Dash 00:21.620 --> 00:24.480 API Dutko and I'm inside that mux route. 00:24.620 --> 00:29.690 We created a couple of lectures ago and what I'll do is create a route called Muxtape Post and it will 00:29.690 --> 00:34.310 go to API slash admin, which comes from line 30, as you'll recall. 00:35.060 --> 00:40.130 And we're going to go this time to slash virtual dash terminal. 00:40.520 --> 00:42.140 Dash succeeded 00:44.810 --> 00:51.260 and then we'll go to a existent handler, which will be from a receiver app and I'll just call it virtual 00:51.500 --> 00:56.270 terminal payment succeeded, which is the same thing we called it on the front end. 00:56.750 --> 01:02.690 So now let's go over to our handlers, dash API and start working on that and will be similar to but 01:02.690 --> 01:05.540 not exactly the same as what we have in the front end. 01:06.260 --> 01:11.840 So I'll create a function here with a receiver app pointer to application and I will call that virtual 01:11.840 --> 01:19.550 terminal payment succeeded and it's a handler, so it needs a response writer and a pointer to a request. 01:23.840 --> 01:26.300 Now we're not going to handle a form post. 01:26.390 --> 01:32.250 What we'll do is actually send JSON as the body of the post request from the front end. 01:32.300 --> 01:33.800 So we need someplace to store that. 01:34.550 --> 01:41.240 So I'll do is just create a variable, a struct var talks and data, which is what we called it on the 01:41.240 --> 01:41.780 front end. 01:42.140 --> 01:46.640 But I'll make this a struct and you can of course create your own type if you want to, but I'll just 01:46.640 --> 01:51.920 do it right in here so I know I'm going to have payment amount and I'm going to take that as an input 01:51.920 --> 01:58.460 from the JSON file and in JSON I'll expect that to be called Jason Amount. 02:00.690 --> 02:06.770 And I'm also going to have the currency payment currency, which will be a string, and in JSON we'll 02:06.770 --> 02:08.410 call that currency. 02:10.400 --> 02:17.030 And I'm also going to have the first name, which will be a string, and Jason will call that first 02:17.030 --> 02:17.220 name. 02:17.360 --> 02:23.840 Now, in the virtual terminal may not actually use that, but it's not going to hurt us to put it in 02:23.840 --> 02:24.050 there. 02:24.500 --> 02:28.910 And we'll have the last name, which is Strength and JSON. 02:28.910 --> 02:31.130 I'll call that last name. 02:33.740 --> 02:37.090 And we're also going to have the email, which is your strength. 02:37.250 --> 02:46.940 And Jason, that's called email and the payment intent, which is actually a payment ID, but I'll call 02:46.940 --> 02:50.510 it payment intent and that'll be a string. 02:52.040 --> 02:55.910 And Jason, that'll be called payment, underscore or spelt right. 02:56.480 --> 03:08.510 Underscore intent and the payment method, which again is an ID Jason payment underscore method. 03:12.060 --> 03:16.640 And I'll have the bank return code, we're not getting that from Jason, but we'll be using it shortly. 03:16.650 --> 03:19.860 As you'll see, we're not getting that from the Jason posted with this. 03:19.860 --> 03:23.840 In other words, bank return code. 03:24.570 --> 03:27.960 And I got to put the word code after that and a type. 03:30.360 --> 03:34.950 OK, we're also going to need the expiry month 03:39.540 --> 03:40.830 and that is going to be an inch. 03:41.340 --> 03:53.130 And and in Jason, we'll call the expiry exp expiry month and I'll duplicate that and do the same thing 03:53.130 --> 03:53.760 for a year. 03:56.760 --> 04:01.230 Again, expiry year. 04:06.920 --> 04:12.130 And the last four digits, which will be in the form of spring, which is how we get it from Stripe, 04:12.680 --> 04:20.960 Jason, last for some of this, we're getting posted to us and some of it we're not and will be populating 04:20.960 --> 04:25.580 this transaction data later on to send it back as the response from this handler. 04:26.600 --> 04:28.960 Now, I'm going to do everything in this handler right now. 04:29.150 --> 04:34.130 And of course, if you want to extract these two shorter functions or methods, you feel free to do 04:34.130 --> 04:34.440 so. 04:34.460 --> 04:35.460 But let's get this going. 04:35.510 --> 04:37.690 OK, so we're getting a posted, Jason. 04:37.700 --> 04:43.490 And as we know, we can use our built-In helper from the app receiver called Jason. 04:44.210 --> 04:50.420 And we're going to read W and are the first two required parameters and then we're going to read into 04:50.990 --> 04:51.500 some data. 04:53.990 --> 04:59.540 So if this succeeds, then our variable text and data is populated with everything that was posted to 04:59.540 --> 04:59.800 us. 05:00.110 --> 05:01.130 But there might be an error. 05:01.130 --> 05:03.620 So we'll check for error is not equal to nil. 05:04.310 --> 05:07.100 Then all I'm going to do right now is say Abdon, bad request. 05:07.910 --> 05:12.500 And that requires the response writer, the reader and the error. 05:12.950 --> 05:15.230 And I'll return at this point because I don't want to go any further. 05:16.790 --> 05:22.670 And if we get past that, what we need to do, of course, is to to take advantage of our cards, packages 05:22.670 --> 05:23.930 or a variable called card. 05:24.620 --> 05:26.900 And that's equal to cards dot card. 05:27.830 --> 05:43.430 And we populate its parameters secret with app, dot config, dot stripe, dot secret and it's key with 05:44.240 --> 05:46.230 the config dot straight dot key. 05:48.350 --> 05:52.170 Now we have that variable and we can take advantage of all of its functions. 05:52.850 --> 05:56.270 So first of all, we'll get our payment and content will retrieve that. 05:56.270 --> 05:58.490 So pie for payment, intent and error. 05:58.640 --> 06:05.150 Those are assigned the value of the card to retrieve payment intent and all that requires user and data 06:10.010 --> 06:10.750 payment intent. 06:12.740 --> 06:14.030 And again, we check for an error. 06:14.060 --> 06:18.140 And I'm just going to copy this because I'll be using it periodically through the remainder of this 06:18.140 --> 06:19.520 election, so I'll pass that in there. 06:19.580 --> 06:20.740 Now we have our payment intent. 06:22.130 --> 06:26.810 The next thing we want to do is pretty much the same thing we did on the front end, and that's to get 06:26.810 --> 06:30.560 the payment method, payment method and error or assign the value of card. 06:30.950 --> 06:36.020 And what we call here is get payment method and we hand it and data 06:39.050 --> 06:39.790 payment method. 06:41.840 --> 06:42.900 And again, we check for an error. 06:44.810 --> 06:50.990 So now we can populate some of the fields that were not populated in our time in data variable. 06:51.200 --> 07:01.020 So to and data last for we have that now that's going to be equal to pm dot card dot last for. 07:03.860 --> 07:11.590 And we also have the expiry month fix and data that expiry month and that will be equal to. 07:11.930 --> 07:19.410 And we have to cast this to an end because it comes as you in sixty four p.m. the third month and I 07:19.430 --> 07:25.550 can duplicate that and get the year expiry year in. 07:25.550 --> 07:27.170 This becomes a year. 07:30.730 --> 07:34.360 Now we create a new transaction, just like we did on the front end. 07:35.020 --> 07:38.830 And actually, if I want to, I can go and copy that information, but I'll just type it out here because 07:38.830 --> 07:39.640 there's not very much. 07:40.450 --> 07:42.190 So we'll create a transaction variable. 07:42.700 --> 07:44.830 It's a type models dot transaction. 07:45.900 --> 07:47.260 I just populate its members. 07:47.260 --> 07:50.740 So amount that will be equal to tax and data. 07:53.560 --> 07:55.450 Amount of capital, the 07:58.870 --> 07:59.600 payment amount. 07:59.620 --> 08:06.740 So we need the amount, we need the currency and that's and DataDot payment currency. 08:09.910 --> 08:13.390 We need the last four, which is now tax and data. 08:17.140 --> 08:27.400 Last fall, we need the expiry month and that's tax and data date expiry month and we need the expiry 08:27.410 --> 08:27.760 year. 08:27.760 --> 08:32.830 So I'll just duplicate that and change this tier and change this tier. 08:35.400 --> 08:38.140 And not many more when his bank returned home. 08:38.370 --> 08:43.010 And that comes from, if you recall, PPI or payment intent charges, 08:46.680 --> 08:50.160 DOT data index zero. 08:54.330 --> 08:55.800 And finally, the transactions data. 08:56.490 --> 08:59.300 And we're just going to hard code that to two, which means cleared. 09:00.150 --> 09:04.870 So once we have that popularity, we just save the transaction. 09:04.920 --> 09:06.050 So I don't care about the ID. 09:06.210 --> 09:07.410 I do care about the error. 09:07.740 --> 09:10.380 And those are equal to after the transaction. 09:11.460 --> 09:14.100 And I give it my transaction that I just created. 09:14.800 --> 09:17.940 And again, we check for an error so I can just copy this code. 09:21.030 --> 09:27.090 Paste it in here, and if there is no error at that point, we just returned some Jason. 09:27.390 --> 09:29.840 So what we're going to do is tap right, Jason? 09:30.180 --> 09:35.390 And it requires our response writer some status of status, OK? 09:38.280 --> 09:44.000 And the data which is taken and that should be sufficient. 09:44.010 --> 09:49.740 So if we go back to our roots for the API, our error should be gone because that handler now exists. 09:49.800 --> 09:54.990 OK, so if we did everything right, that's the backend we receive posted Jason. 09:54.990 --> 09:58.270 We process it as required and we send back our response. 09:58.830 --> 10:04.530 So in the next lecture, we'll go modify the front end to instead of posting to the front end, we'll 10:04.530 --> 10:10.260 have it send a post request in the form of Jason to the route we just to find here on line thirty seven 10:10.260 --> 10:11.880 of rootstock API. 10:12.660 --> 10:14.870 So we'll get started on that in the next election.