WEBVTT 00:02.350 --> 00:07.870 So this time around, we want to get started setting up a handler to get a payment intent and adding 00:07.870 --> 00:14.170 the associated root to our roots file, but before we do that, there's a few things in the API go the 00:14.170 --> 00:18.580 main entry point for our back application that we probably can get rid of. 00:18.580 --> 00:21.040 They're just leftovers from printed copy and paste. 00:21.050 --> 00:23.230 And you probably noticed these and get rid of them on your own. 00:23.230 --> 00:24.590 But if not, let's do it right now. 00:25.240 --> 00:31.000 So, for example, online 14, we don't need CSA version because we're not going to be actually sending 00:31.000 --> 00:31.660 Web pages. 00:31.660 --> 00:38.170 We're just sending JSON, at least for now, which means down here we don't need a template cache because 00:38.170 --> 00:42.540 we're not going to be creating templates and that's in the type application struct. 00:42.550 --> 00:47.020 So let's get rid of that back up in the config struct. 00:47.020 --> 00:52.090 We don't need this API field because this is the API, so we don't need the URL to the API. 00:52.120 --> 00:52.900 So that's gone. 00:53.620 --> 00:58.960 And the last thing is I created this variable version online 13. 00:58.960 --> 01:00.010 So let's actually use that. 01:00.010 --> 01:04.140 We'll just add that to the application config right down here in the main function. 01:04.720 --> 01:06.970 So I'll say version and version. 01:07.540 --> 01:09.370 All right, just a bit of cleanup. 01:10.390 --> 01:14.550 So let's now create our handlers for the API. 01:14.560 --> 01:15.550 We'll get started on that. 01:15.550 --> 01:18.110 We might not finish at this time around, but we'll at least get started. 01:18.700 --> 01:24.160 So in my API folder, I'll create a new file and I'll use the same naming convention I did for Route's. 01:24.160 --> 01:32.160 I'll call it handlers dash API Dutko and we'll give it a package declaration package making. 01:33.640 --> 01:39.100 And the first thing I'll do is just set up a couple of types because I know that I'm going to be receiving 01:39.100 --> 01:44.890 information in JSON format and I know I'm going to be sending data back in JSON format. 01:44.890 --> 01:48.450 So let's create a couple of types just to make things simpler for us. 01:49.120 --> 01:52.660 So a creative type, first of all, for what I'm going to get from the front end and I'll call that 01:52.660 --> 01:58.240 and call it whatever I want, but I'm going to call it stripe of payload because it's information about 01:58.240 --> 02:01.240 Stripe and it's the payload that's coming to us and it's a struct. 02:01.960 --> 02:04.550 And what kinds of things do I know that I'm going to need? 02:04.600 --> 02:09.390 You can always modify this later on, but I know I'm going to need to know what currency I'm using. 02:09.970 --> 02:11.380 So currency could be a string. 02:11.380 --> 02:15.430 And in Jason, I'll call that currency all lowercase. 02:16.150 --> 02:22.090 And we're also going to have to have the amount which will actually receive in the form of a string 02:22.210 --> 02:24.730 and we'll cast it to what we needed to be after the fact. 02:25.300 --> 02:29.740 And in JSON, I'll call that not surprisingly about. 02:32.080 --> 02:37.750 So that's what we're getting and what are we going to send back, I'll call that type Jason response 02:37.900 --> 02:38.980 and it will be a struct. 02:39.790 --> 02:41.080 And what are we going to have in there? 02:41.110 --> 02:44.210 Well, we'll just put in the things that I think I might need right now. 02:44.230 --> 02:47.530 First of all, did everything work OK to be a boolean? 02:47.680 --> 02:49.150 And in Jason, I'll call that. 02:49.240 --> 02:55.210 OK, that will be true if things work the way that we expected and we can send a valid response and 02:55.210 --> 03:00.790 it will be false if it's not, which means we probably want a message, which we'll call a string, 03:01.840 --> 03:08.430 and then Jason will call that message and there might be some content I have to send. 03:08.450 --> 03:11.040 So I'll call that string content string. 03:11.110 --> 03:13.990 Jason will be content. 03:17.160 --> 03:22.410 And finally, I often need to deal with an idea of some sort, I mean, this time around, but I'm going 03:22.410 --> 03:23.730 to put it in there, I can always delete it. 03:23.730 --> 03:26.040 If I don't need it, that will be an integer. 03:26.610 --> 03:28.200 And in JSON, I'll call that. 03:30.280 --> 03:36.090 OK, so let's format this good and now let's create a stub handler. 03:36.160 --> 03:41.250 So I'll create a function and it will have the receiver of app type pointer to applications. 03:41.250 --> 03:45.570 So we have access to our application configuration and so forth. 03:46.290 --> 03:49.020 And I'll call this get payment content. 03:49.590 --> 03:53.180 And since it's a handler has to have a response writer and a request. 03:53.610 --> 03:55.500 So W will be my response writer. 03:55.560 --> 03:56.420 W4 Right. 03:56.820 --> 04:03.600 HDB response writer and our request is a pointer to HDB dot request. 04:04.860 --> 04:12.240 OK, so for the moment let's just send some manually built JSON back to the end user. 04:12.330 --> 04:15.320 So I'll create a variable which I'll call J. 04:15.960 --> 04:22.320 And that will be of type Jason response and we'll just populate its fields and for now we'll just say 04:22.320 --> 04:24.270 OK is true. 04:26.920 --> 04:31.360 And let's just convert that to Jason and send it back, so I'll use the variable out because it's what 04:31.360 --> 04:36.580 I'm writing out and I'll check for an error, that's a sign the value of from the JSON package and I'll 04:36.580 --> 04:39.420 use Marschall indent just to make it easier to look at. 04:40.300 --> 04:47.440 And we're going to Marshall J and Prefix and we'll put a few spaces on the indent check for an error 04:47.680 --> 04:49.400 if error is not equal to nil. 04:49.690 --> 04:59.770 I'll just use my error log from the app variable after error log print on the error and we just write 04:59.770 --> 05:00.020 it out. 05:00.040 --> 05:02.140 So let's set the header and our response rate. 05:02.140 --> 05:14.120 Or we call the method header and we call set and we set the content type to be applications JSON, OK, 05:15.340 --> 05:16.150 and we write it out. 05:16.840 --> 05:17.150 Right. 05:17.260 --> 05:21.970 And I won't even bother checking for the error because this is all going to change when we're done. 05:22.180 --> 05:23.180 So what are we writing? 05:23.200 --> 05:23.930 We're getting out. 05:26.500 --> 05:28.680 So now we have a get payment content handler. 05:28.720 --> 05:33.450 Let's go to our API roots and set one up for that. 05:34.060 --> 05:44.140 So we'll say Mux to get four right now and we're going to get API to be in quotes API. 05:45.340 --> 05:49.300 I'm going to prefix all of my API routes with API, which kind of makes sense. 05:50.110 --> 05:52.450 And we'll call this payment Desh Intent. 05:53.600 --> 05:54.630 Now down the road. 05:54.640 --> 05:59.110 This will probably become a post, but for now I just want to make sure it works and I'm going to call 05:59.110 --> 06:00.130 after it, get payment. 06:01.600 --> 06:03.520 OK, so let's try this. 06:03.520 --> 06:07.170 Let's open our terminal and we're in the correct directory. 06:07.840 --> 06:16.450 I am I'm writing my ghostwrite project and I'll type go run dot slash cmd API and see if it all compiles. 06:19.010 --> 06:23.360 And it does so it's on par four thousand one, so I can go to my Web browser and I should be able to 06:23.630 --> 06:27.470 hit the API payment Dasch intent. 06:27.470 --> 06:40.400 So I'll go to Firefox and I'll type in HTP Colon's localhost, Kolan four thousand and one API and get 06:41.540 --> 06:46.650 payment Dasch content and we get some JSON back. 06:46.670 --> 06:47.420 So there it is. 06:47.690 --> 06:52.030 Firefox gives it to me in this format, but we can look at the raw data and there it is. 06:52.040 --> 06:55.460 So we have an OK set to true and everything else has the default values. 06:55.820 --> 06:57.020 In other words, they're empty. 06:58.130 --> 06:59.110 So that works really well. 06:59.870 --> 07:05.990 Now it's going to be a pain to be stopping and starting this application, this back end application 07:05.990 --> 07:06.980 all the time. 07:07.550 --> 07:12.270 If I need to have things like the Stripe's Secret key, for example. 07:12.290 --> 07:14.000 So here's where we're going to use a makefile. 07:14.000 --> 07:18.110 And this is why we installed make way back when now I'm on a Mac. 07:18.110 --> 07:20.660 As I record this, I'm going to stop this application. 07:21.170 --> 07:26.840 But in the course resources for this lecture, you'll find two versions of Makefile, one for Mac and 07:26.840 --> 07:27.800 one for Windows. 07:28.550 --> 07:35.240 So download the Makefile and make sure it starts with a capital M and the rest of the file name is lowercase, 07:35.270 --> 07:36.680 just like I have right here. 07:36.680 --> 07:41.600 Makefile, put that in the root level of your application and you'll be able to do things like this. 07:41.900 --> 07:49.250 So I'll clear the screen and I'll type make start underscore back and it will run the back. 07:49.430 --> 07:50.800 It will build it and run it. 07:51.440 --> 07:56.150 Now the front end I'm typically going to be using er when I work on it, when I'm working on the front 07:56.150 --> 07:57.680 end and I'll have the back end running this way. 07:57.680 --> 08:02.840 And it's just really easy to start at using Mac and I can actually it's running in the background so 08:02.840 --> 08:04.580 I can return if I want to stop it. 08:04.580 --> 08:10.760 I can say make stop underscore back and you'll see, let's open this makefile up and have a look at 08:10.760 --> 08:10.880 it. 08:12.020 --> 08:16.040 This is a really convenient way of doing common tasks that you have to do. 08:16.730 --> 08:18.710 Now, this is a really simple make file. 08:19.550 --> 08:25.490 First thing to note, at the very top, you have to have your stripe secret, your test secret and your 08:25.490 --> 08:27.140 stripe key, your test key. 08:27.230 --> 08:28.280 Mine are in there right now. 08:28.280 --> 08:32.990 And as I said a while ago, before I make this course alive, I'm going to regenerate those keys so 08:32.990 --> 08:34.490 they won't work and you can't use them. 08:34.490 --> 08:38.870 And the version that you're downloading has nothing after the equal sign for the first two lines. 08:39.650 --> 08:46.580 So get your test key and put it in the appropriate place and get your test secret and put it in the 08:46.580 --> 08:53.570 appropriate place and make sure that you get the secret key next to stripe secret and secret keys. 08:53.570 --> 09:01.250 Always start with České Underscore and your test key and put that next to stripe underscore key and 09:01.670 --> 09:03.350 test keys or keys. 09:03.350 --> 09:06.590 Always start with pick your publishable key. 09:06.800 --> 09:08.750 OK, so make sure you get them in the right place. 09:08.750 --> 09:12.980 If not, you'll just get error messages when you start working with Stripe and I often get them in the 09:12.980 --> 09:14.540 wrong place, but try to avoid that. 09:15.380 --> 09:19.160 The next two lines give you your ports go stripe is the front end. 09:19.160 --> 09:20.120 I want that to listen. 09:20.120 --> 09:21.260 I'm four thousand. 09:21.260 --> 09:22.610 You can use whatever you wish. 09:22.610 --> 09:23.510 And the back end. 09:23.570 --> 09:25.640 I want to listen on port four thousand and one. 09:26.510 --> 09:30.410 So we're looking at the Mac or Linux version of this right now. 09:30.410 --> 09:33.440 The Windows version will look different, but it has the same basic commands. 09:33.920 --> 09:37.010 So you can say make clean to clean all the binaries. 09:37.040 --> 09:42.830 You can say make build front, which will compile the front end, make build back, will compile the 09:42.830 --> 09:44.570 back end and so on and so forth. 09:45.140 --> 09:49.460 Then you have simple ones like START, which will start both the front and the back end. 09:49.670 --> 09:56.480 And since start calls start front, start front will actually build the front end first, then start 09:56.480 --> 10:01.370 it and start back, will build the back end and then start it and so on and so forth. 10:01.430 --> 10:03.110 This is really, really straightforward. 10:03.650 --> 10:05.420 So what I'm working on the front end. 10:06.080 --> 10:10.190 I'll usually run the front end with air and run the back end. 10:10.190 --> 10:11.450 I'll just use make to keep it going. 10:11.720 --> 10:16.340 And when I'm working on the back end, I'll run the back end with air and keep the front end going. 10:17.000 --> 10:18.800 So that's entirely up to you. 10:18.800 --> 10:21.200 Whether you use this or not, it's certainly optional. 10:21.200 --> 10:24.710 But I find that the makefile makes your life ever so much easier. 10:25.400 --> 10:28.700 So that's what I'm going to be using for the remainder of this course. 10:28.700 --> 10:30.680 I'll be using this makefile a great deal. 10:31.340 --> 10:34.850 And if you choose to do so, it I think it will make your life easier. 10:34.850 --> 10:36.140 But again, it's optional. 10:36.740 --> 10:42.440 So right now we have a stub handler for getting a payment intent in the back end and we have a route 10:42.440 --> 10:44.060 to it and then we have a new makefile. 10:44.060 --> 10:49.550 So let's move on and actually write the necessary code to make this handler functional.