WEBVTT 00:02.770 --> 00:08.620 So let's continue working on our handler and saving customer information, transaction information, 00:08.620 --> 00:11.070 order information, all that sort of thing. 00:11.100 --> 00:14.560 But before we do that, there's something that's been irritating me for a while and I want to take care 00:14.560 --> 00:15.300 of it right now. 00:15.310 --> 00:22.270 So on our application in our Web browser, if I click on the home link, I got a page not found and 00:22.270 --> 00:23.080 that's irritating. 00:23.110 --> 00:24.090 So let's go fix that. 00:24.100 --> 00:25.900 We'll just put a Stubbe page in there for now. 00:26.380 --> 00:28.480 So over to our templates folder. 00:28.480 --> 00:36.130 Inside our Web folder, we'll create a new file which I'll call home page dot each HTML and I'll just 00:36.130 --> 00:38.620 open up the receipt page, which has succeeded. 00:39.910 --> 00:47.350 Select all copy close and open home page and paste it in and get rid of this content in here and we'll 00:47.350 --> 00:51.580 just put the title home or maybe widgets 00:57.280 --> 00:59.130 and up here we'll put widgets as well. 01:02.830 --> 01:10.780 And then if we go to our handlers and just go to the top and copy, say, the virtual terminal one and 01:10.780 --> 01:17.530 paste it in here and change it to home and change the comment. 01:19.740 --> 01:27.630 Displays the homepage, and this doesn't need the stripe, just partial, so we'll simply delete this 01:28.650 --> 01:33.600 because it is an optional parameter and change the template we render to home. 01:34.470 --> 01:40.560 And the last thing we have to do is go to route's duplicate, the top route for virtual terminal, change 01:40.560 --> 01:45.280 it to be nothing more than a slash for the home page and point it to home. 01:46.890 --> 01:53.400 And if we restart our application, make stuff like start. 01:57.150 --> 01:59.970 Go back to our Web browser and reload that page. 02:00.000 --> 02:01.090 We should have the homepage. 02:01.110 --> 02:01.590 Perfect. 02:02.160 --> 02:03.270 That was just bothering me. 02:03.420 --> 02:08.580 OK, now let's go back to our handler and think about the kinds of things we're going to have to do 02:08.580 --> 02:08.900 here. 02:08.910 --> 02:14.970 So back in our idy, looking at handlers dog go, which is inside the Web folder. 02:16.080 --> 02:18.020 This is the one we're talking about right now. 02:18.150 --> 02:21.170 So if we scroll down a bit, we're going to definitely pass the form. 02:21.180 --> 02:22.630 We're going to get the posted data. 02:22.650 --> 02:25.980 We're going to create the car, get the payment content and the payment method. 02:26.790 --> 02:28.530 And then we save all this information. 02:28.530 --> 02:32.220 So down here, I'll put it right here. 02:32.220 --> 02:33.820 We can move it later if we have to. 02:34.920 --> 02:36.120 Here's the things we want to do. 02:36.300 --> 02:39.210 First of all, we're going to want to create a new customer. 02:40.590 --> 02:47.410 Then we're going to want to create a new order, and then we're going to want to create a new transaction. 02:48.360 --> 02:50.730 So that's the kind of thing we're going to want to do. 02:50.750 --> 02:54.000 Now, we're not going to put all this logic right in this handler. 02:54.540 --> 02:58.890 Instead, we'll create functions so we can reuse them whenever we have to and wherever we have to. 02:59.340 --> 03:00.740 So we're definitely going to want to do that. 03:00.750 --> 03:05.550 And down here, you might have noticed this comment that I put in a while ago should write this data 03:05.550 --> 03:08.490 to session and then redirect user to a new page. 03:08.520 --> 03:11.100 I don't know when there's a question mark there, because it's not a question. 03:11.410 --> 03:12.540 This is an observation. 03:12.900 --> 03:16.020 And that's something you typically do after a form submission. 03:16.020 --> 03:17.270 And the rationale is pretty simple. 03:17.280 --> 03:24.060 If I go back to my Web browser and say I want to buy a widget, so I buy one widget and I put in my 03:24.060 --> 03:33.570 name Jack Jones, Jack at Jones dot com and put it in a credit card number that I know is going to work. 03:35.600 --> 03:43.100 And put an expiry date and some CBC once I charge this saltworks. 03:44.950 --> 03:50.230 And it's great and it takes me to the payment succeeded page, but if I try to reload this page for 03:50.230 --> 03:52.500 whatever reason, I get this warning. 03:52.510 --> 03:54.490 Are you sure you want to resubmit this data? 03:55.150 --> 03:58.570 Now, if this was a comments on a blog post, that might not be a big deal. 03:58.570 --> 04:00.750 Still bad practice, but it's not a huge deal. 04:00.760 --> 04:05.650 But in this instance, it's actually going to charge the credit card a second time and we don't want 04:05.650 --> 04:06.260 that to happen. 04:06.730 --> 04:13.270 So what we want to do instead is to go back to our code and right here where it says should write this 04:13.270 --> 04:16.120 date at a session and then redirect the user to a new page. 04:16.150 --> 04:17.740 That's exactly what we want to do. 04:18.100 --> 04:23.170 We'll store the information necessary to display the receipt page or whatever it is we need to receipt 04:23.350 --> 04:24.270 in the session. 04:24.700 --> 04:27.180 Then we'll do an HTTP redirect. 04:27.430 --> 04:31.780 So if they reload the page, they'll just reload the page with the receipt or whatever we're showing 04:31.780 --> 04:33.790 them and it won't repost form data. 04:34.540 --> 04:35.960 So we probably should do that. 04:36.640 --> 04:42.010 So let's go back to our main dot, go inside the web folder right here and we're going to make some 04:42.010 --> 04:42.950 changes to this. 04:43.000 --> 04:47.140 Well, first thing we're going to do is we're going to import a session manager package. 04:47.590 --> 04:52.600 And if I go back to my Web browser, the one we're going to use is the one that I use a lot. 04:52.600 --> 04:53.500 And it's this one. 04:53.500 --> 04:56.060 It's Alex Edwards C. 04:56.230 --> 05:01.540 S, and it's a good session manager, written very well and extremely easy to use. 05:02.110 --> 05:05.440 So right down here, it shows me the installation instructions. 05:05.440 --> 05:06.430 So let's go get that. 05:06.610 --> 05:07.330 Copy this. 05:08.380 --> 05:15.640 I'll go back to my ID, open the terminal and I'll make stuff just so nothing is running. 05:16.180 --> 05:21.340 Clear the screen and paste that in and it goes and gets it for us. 05:21.400 --> 05:22.790 OK, now how do we use this? 05:22.840 --> 05:26.470 Well, there's actually quite a few configuration options, but we just want to get it running. 05:27.100 --> 05:28.970 And here's an example of how to use it. 05:29.290 --> 05:32.620 So it says create a session manager and set a lifetime. 05:32.620 --> 05:36.880 And really, that's all you need to do to get started with this part of it anyway. 05:37.390 --> 05:41.170 So let's go back to our idea and look at Main Dot. 05:41.170 --> 05:44.170 Go right here. 05:46.100 --> 05:53.240 In the main function, I'll do it right after I get my database connection, so I'll stay here, set 05:53.240 --> 05:53.950 up session. 05:57.230 --> 05:59.140 So let's create a variable session. 05:59.320 --> 06:03.210 Actually, let's create a package level variable just to make things a little bit easier for us. 06:03.230 --> 06:07.700 So up here, I'll create a new variable, which I'll call for session. 06:08.270 --> 06:13.880 And it's a type pointer to exact session manager right there. 06:13.880 --> 06:15.950 And hopefully this will do the importance for me, too. 06:15.950 --> 06:16.910 And it did. 06:17.120 --> 06:18.980 OK, so now that's a package level variable. 06:19.760 --> 06:26.600 And what we'll do is come down our main function and we'll use that package level variable session and 06:26.600 --> 06:30.200 make that equal to X new just like the example. 06:30.770 --> 06:36.020 And then the only other thing I really need to worry about is setting up a lifetime session lifetime. 06:36.830 --> 06:38.900 And that will be equal to, say, twenty four hours. 06:38.900 --> 06:40.640 Twenty four times time. 06:40.940 --> 06:41.340 Hour. 06:42.470 --> 06:44.920 OK, now I have this session set up. 06:44.930 --> 06:50.390 There's still a bit of work to do, but what I'm going to do is add that to my application config. 06:50.630 --> 06:56.260 So I'll add a new field up here and I'll call it session with a capital s, so it's exported. 06:56.430 --> 06:57.110 Try that again. 06:58.490 --> 07:07.670 And that will be a type pointer to start session manager right there and let's format this, OK, and 07:07.670 --> 07:08.660 come down here. 07:10.010 --> 07:14.120 And now that we have the session set up, we can actually add it to our application. 07:14.570 --> 07:16.040 So it's available anywhere we need it. 07:16.730 --> 07:21.160 Session is going to be equal to session format it. 07:22.120 --> 07:24.420 OK, so that's the first thing that we need to do. 07:24.560 --> 07:26.740 Now there is one more bit of work we need to do. 07:27.200 --> 07:35.210 If you come back here to the documentation, one of the things that it has to do is to use the load 07:35.210 --> 07:36.350 and save method. 07:36.350 --> 07:39.590 And you can read about it right here in the loading and saving sessions. 07:39.590 --> 07:44.000 And the easiest thing to do is to write a bit of middleware that just calls load and say, and that 07:44.000 --> 07:48.800 takes care of all of the magic of making sure that the sessions are kept track of the cookies are issued 07:48.800 --> 07:51.170 as necessary, that cookies are read as necessary. 07:51.950 --> 07:53.530 And that's pretty trivial to set out. 07:53.870 --> 08:03.950 So what we'll do is go back to our Web folder and we'll create a new file model where it go and that's 08:03.950 --> 08:04.700 in packaging. 08:09.530 --> 08:11.870 And we'll just create a very simple bit of middleware. 08:12.170 --> 08:17.730 So we'll create a function and we'll call it session load, colour, whatever you want. 08:17.730 --> 08:20.390 But such a load makes sense because this is middleware. 08:20.390 --> 08:28.040 It's going to receive an HTTP dog handler, modify it as necessary and then return and it should be 08:28.040 --> 08:28.490 handler. 08:28.490 --> 08:35.990 So next, which is what it's typically called in middleware, HDP handlers, the type it receives and 08:35.990 --> 08:36.620 it returns. 08:36.620 --> 08:38.080 And a handler. 08:39.560 --> 08:43.610 And this is one line of code and I guess I'd better let it do the imports for me. 08:47.480 --> 08:49.860 I will just delete this and I will take care of that. 08:50.240 --> 08:56.780 So now we have our input and all we're going to do is return like it, right, like it says in the documentation 08:57.260 --> 08:57.880 session. 08:57.890 --> 09:04.130 And because that's a package level variable and I have access to it load and save and it takes next 09:04.130 --> 09:04.880 as its parameter. 09:06.890 --> 09:12.860 OK, so we've created the middleware where now we need to use it, so we go to our roots and all we 09:12.860 --> 09:18.830 have to do in here is take our mux and say use Sushila. 09:21.110 --> 09:27.560 And with that one change, we now have access to a session which will be using when we go to that payment, 09:27.560 --> 09:33.770 succeeded Handler and do an HTP redirect because we can redirect, but we need to save some information 09:33.770 --> 09:34.460 before we do it. 09:35.540 --> 09:38.020 OK, so that gets us set up. 09:38.210 --> 09:43.640 And the next step is to well, we're going to have to write that little bit of school code, that database 09:43.640 --> 09:45.900 functionality to save certain information. 09:45.940 --> 09:47.060 The customer, for example. 09:47.060 --> 09:48.590 I think that's the one we have left to do. 09:49.100 --> 09:54.040 And then we'll start implementing in the handlers these three functions. 09:54.320 --> 09:59.960 So we'll create a new customer, get the ID, created a new order and put the order information, including 09:59.960 --> 10:05.270 the customer ID into that, and then create the new transaction and put all the necessary information 10:05.270 --> 10:05.660 in that. 10:05.660 --> 10:08.180 And we'll get started on that in the next lecture.