WEBVTT 00:01.890 --> 00:08.040 So the next step we need to follow is to actually create the handler for this fetch statement online 00:08.040 --> 00:10.740 157 and of course, this is calling the back end. 00:11.070 --> 00:17.280 So let's open our API folder and open our Roots API and let's create a route that points to a non-existent 00:17.280 --> 00:17.780 handler. 00:17.790 --> 00:18.720 And I'll just put it right here. 00:19.680 --> 00:26.210 So Muxtape post, because this is a post request and we're looking for API and it's a long one. 00:27.060 --> 00:34.690 Create customer and subscribe to plan and that's going to call after. 00:34.740 --> 00:43.620 And we'll create a handler called Create Customer and subscribe to the plan and that doesn't exist. 00:43.650 --> 00:51.480 So let's go over to our API handlers, which is the file handlers Dash API Dargo in the API folder and 00:51.480 --> 00:58.200 at very bottom I'll create a new handler and it will take the receiver app application. 00:58.350 --> 01:01.740 And it's called Create Customer and Subscribe to Plan. 01:02.100 --> 01:10.290 And we give it the W, which is an EDP response writer and ah, which is a pointer to an IEP request. 01:11.550 --> 01:17.500 And if I do that, just that much should make the arrow go away and route's dash API and it does where 01:17.520 --> 01:18.580 everything is named properly. 01:19.770 --> 01:22.410 So what do we want to do in this handler? 01:23.130 --> 01:28.190 Well, to start with, we're going to have a payload in the form of JSON passed to us. 01:28.210 --> 01:32.880 So we'll create a variable called data and it will be in the form of straight payload. 01:33.420 --> 01:41.160 Now, if we scroll up to where the payload is defined, we have here the only things I'm expecting to 01:41.160 --> 01:44.040 find in stright payload are currency and amount. 01:44.040 --> 01:46.050 And I'm actually not passing either one of those. 01:46.500 --> 01:52.380 If we go back to the bronze plan page, I'm actually passing it in my payload. 01:53.910 --> 01:55.230 Where is my payload? 01:57.150 --> 01:57.930 There it is right there. 01:58.470 --> 02:01.620 A plan, a payment method, an email and allows for. 02:01.710 --> 02:08.430 So let's go back to Handler's API and we'll add to this a payment method. 02:10.540 --> 02:20.530 Which is a string and that will be in Jason called payment underscore method of duplicate that we're 02:20.530 --> 02:21.570 also handling it. 02:21.590 --> 02:26.770 An email which is in the string of Jason email is a string. 02:26.820 --> 02:28.510 And then Jason, I'm calling it email. 02:29.200 --> 02:33.040 Then I'm passing last for the last four. 02:33.460 --> 02:36.880 And then Jason, I'm going to call that last underscore four. 02:37.630 --> 02:41.980 And finally, we're ending the plan, which is a strike plan, the ID for it. 02:42.700 --> 02:44.320 And in this off, just call a plan. 02:44.950 --> 02:50.300 OK, so let's format this and scroll back down to our new handler and start doing things with it. 02:50.980 --> 02:56.900 So the first thing I'll do is get my Jason or get the values out of Jason into something I can use. 02:56.920 --> 03:05.770 So error is assigned the value of Jason's new decoder and I'm decoding the request body and I'm going 03:05.770 --> 03:10.360 to decode it into ampersand data variable and declare it on line. 03:10.360 --> 03:14.050 One of four will check for an error if error is not equal to nil. 03:14.350 --> 03:21.090 And all I'll do right now is after error log print one error and return. 03:22.510 --> 03:26.380 As I've said before, not very friendly, but sufficient for my purposes at the moment. 03:27.910 --> 03:34.100 Now let's just see if we get those values after info log gridline and I'll just print a few out. 03:34.120 --> 03:43.830 So I'll printed, say, dated or email and dated last four and dated dot o payment method and data plan 03:44.580 --> 03:50.140 to print that information out because that's where we're be past and I'll just give them a hard coded 03:50.140 --> 03:50.860 response. 03:50.860 --> 03:51.850 So I'll set a variable. 03:51.850 --> 03:58.540 OK, equal to true and I'll set a variable message which will default to an empty string and then I'll 03:58.540 --> 04:01.030 use my Jason response type. 04:01.030 --> 04:03.910 So response is assign the value of Jason resp. 04:04.600 --> 04:05.920 It's been a while since we've used that. 04:05.920 --> 04:14.470 So it's Jason response and we'll just set OK to OK and message to MSG. 04:15.520 --> 04:19.560 And let's write this information out as Jason gives some kind of response back. 04:19.570 --> 04:26.380 So I wrote an error or assign the value of Jason Marshall indent and a commercial rest. 04:26.980 --> 04:33.190 I'll give it no prefix and a few spaces for indentation and I'll copy my error checking code. 04:36.200 --> 04:46.430 Paste it in here and then write things up, so w dot header, dot set and we'll set content type two 04:47.120 --> 04:52.700 application slash Jason and W dot right at. 04:54.590 --> 04:59.150 And that should give us at least enough to make sure that everything is wired up correctly, so let's 04:59.150 --> 05:00.080 see if things are wrong. 05:00.960 --> 05:02.030 I'll say make stop. 05:04.110 --> 05:05.700 Good make start. 05:08.330 --> 05:10.740 Everything combined, let's go back to our Web browser. 05:10.880 --> 05:13.220 So let's go to our subscription page. 05:13.850 --> 05:18.680 Open the terminal window, the JavaScript terminal, just to make sure things are working as expected. 05:19.170 --> 05:20.180 Let's just see what happens here. 05:20.210 --> 05:24.840 OK, so I'll clear the console and I'll put in some test data. 05:24.860 --> 05:35.900 So Sam Smith, Sam Smith, the CEO, Sam Smith, and some credit card number four to four to four to 05:35.900 --> 05:41.720 four two and make it expire in the future with some CVC. 05:42.140 --> 05:44.330 And let's see what we get back, if anything. 05:46.910 --> 05:55.610 So I got my OK, true back, and if I go back and look at my I.D., I actually have Sam Smith the last 05:55.610 --> 05:59.660 four digits, the payment method and the plan I want to subscribe to. 05:59.780 --> 06:00.350 Perfect. 06:00.990 --> 06:06.950 Now, obviously, we need to do more in this create customer and subscribe to plan handler than just 06:06.950 --> 06:09.520 pass them, Jason, and print some information out. 06:09.830 --> 06:15.440 We actually need to create that straight customer and we need to subscribe that customer to a plan. 06:15.740 --> 06:18.830 And we're going to do that not here in Handler's API. 06:18.840 --> 06:23.160 Of course, we'll do it over in our internal package named Cards. 06:23.400 --> 06:24.180 Cards Don't Go. 06:24.650 --> 06:27.170 So we'll get started on that in the next lecture.