So let's continue working on our handler and saving customer information, transaction information,
order information, all that sort of thing.
But before we do that, there's something that's been irritating me for a while and I want to take care
of it right now.
So on our application in our Web browser, if I click on the home link, I got a page not found and
that's irritating.
So let's go fix that.
We'll just put a Stubbe page in there for now.
So over to our templates folder.
Inside our Web folder, we'll create a new file which I'll call home page dot each HTML and I'll just
open up the receipt page, which has succeeded.
Select all copy close and open home page and paste it in and get rid of this content in here and we'll
just put the title home or maybe widgets
and up here we'll put widgets as well.
And then if we go to our handlers and just go to the top and copy, say, the virtual terminal one and
paste it in here and change it to home and change the comment.
Displays the homepage, and this doesn't need the stripe, just partial, so we'll simply delete this
because it is an optional parameter and change the template we render to home.
And the last thing we have to do is go to route's duplicate, the top route for virtual terminal, change
it to be nothing more than a slash for the home page and point it to home.
And if we restart our application, make stuff like start.
Go back to our Web browser and reload that page.
We should have the homepage.
Perfect.
That was just bothering me.
OK, now let's go back to our handler and think about the kinds of things we're going to have to do
here.
So back in our idy, looking at handlers dog go, which is inside the Web folder.
This is the one we're talking about right now.
So if we scroll down a bit, we're going to definitely pass the form.
We're going to get the posted data.
We're going to create the car, get the payment content and the payment method.
And then we save all this information.
So down here, I'll put it right here.
We can move it later if we have to.
Here's the things we want to do.
First of all, we're going to want to create a new customer.
Then we're going to want to create a new order, and then we're going to want to create a new transaction.
So that's the kind of thing we're going to want to do.
Now, we're not going to put all this logic right in this handler.
Instead, we'll create functions so we can reuse them whenever we have to and wherever we have to.
So we're definitely going to want to do that.
And down here, you might have noticed this comment that I put in a while ago should write this data
to session and then redirect user to a new page.
I don't know when there's a question mark there, because it's not a question.
This is an observation.
And that's something you typically do after a form submission.
And the rationale is pretty simple.
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
name Jack Jones, Jack at Jones dot com and put it in a credit card number that I know is going to work.
And put an expiry date and some CBC once I charge this saltworks.
And it's great and it takes me to the payment succeeded page, but if I try to reload this page for
whatever reason, I get this warning.
Are you sure you want to resubmit this data?
Now, if this was a comments on a blog post, that might not be a big deal.
Still bad practice, but it's not a huge deal.
But in this instance, it's actually going to charge the credit card a second time and we don't want
that to happen.
So what we want to do instead is to go back to our code and right here where it says should write this
date at a session and then redirect the user to a new page.
That's exactly what we want to do.
We'll store the information necessary to display the receipt page or whatever it is we need to receipt
in the session.
Then we'll do an HTTP redirect.
So if they reload the page, they'll just reload the page with the receipt or whatever we're showing
them and it won't repost form data.
So we probably should do that.
So let's go back to our main dot, go inside the web folder right here and we're going to make some
changes to this.
Well, first thing we're going to do is we're going to import a session manager package.
And if I go back to my Web browser, the one we're going to use is the one that I use a lot.
And it's this one.
It's Alex Edwards C.
S, and it's a good session manager, written very well and extremely easy to use.
So right down here, it shows me the installation instructions.
So let's go get that.
Copy this.
I'll go back to my ID, open the terminal and I'll make stuff just so nothing is running.
Clear the screen and paste that in and it goes and gets it for us.
OK, now how do we use this?
Well, there's actually quite a few configuration options, but we just want to get it running.
And here's an example of how to use it.
So it says create a session manager and set a lifetime.
And really, that's all you need to do to get started with this part of it anyway.
So let's go back to our idea and look at Main Dot.
Go right here.
In the main function, I'll do it right after I get my database connection, so I'll stay here, set
up session.
So let's create a variable session.
Actually, let's create a package level variable just to make things a little bit easier for us.
So up here, I'll create a new variable, which I'll call for session.
And it's a type pointer to exact session manager right there.
And hopefully this will do the importance for me, too.
And it did.
OK, so now that's a package level variable.
And what we'll do is come down our main function and we'll use that package level variable session and
make that equal to X new just like the example.
And then the only other thing I really need to worry about is setting up a lifetime session lifetime.
And that will be equal to, say, twenty four hours.
Twenty four times time.
Hour.
OK, now I have this session set up.
There's still a bit of work to do, but what I'm going to do is add that to my application config.
So I'll add a new field up here and I'll call it session with a capital s, so it's exported.
Try that again.
And that will be a type pointer to start session manager right there and let's format this, OK, and
come down here.
And now that we have the session set up, we can actually add it to our application.
So it's available anywhere we need it.
Session is going to be equal to session format it.
OK, so that's the first thing that we need to do.
Now there is one more bit of work we need to do.
If you come back here to the documentation, one of the things that it has to do is to use the load
and save method.
And you can read about it right here in the loading and saving sessions.
And the easiest thing to do is to write a bit of middleware that just calls load and say, and that
takes care of all of the magic of making sure that the sessions are kept track of the cookies are issued
as necessary, that cookies are read as necessary.
And that's pretty trivial to set out.
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
in packaging.
And we'll just create a very simple bit of middleware.
So we'll create a function and we'll call it session load, colour, whatever you want.
But such a load makes sense because this is middleware.
It's going to receive an HTTP dog handler, modify it as necessary and then return and it should be
handler.
So next, which is what it's typically called in middleware, HDP handlers, the type it receives and
it returns.
And a handler.
And this is one line of code and I guess I'd better let it do the imports for me.
I will just delete this and I will take care of that.
So now we have our input and all we're going to do is return like it, right, like it says in the documentation
session.
And because that's a package level variable and I have access to it load and save and it takes next
as its parameter.
OK, so we've created the middleware where now we need to use it, so we go to our roots and all we
have to do in here is take our mux and say use Sushila.
And with that one change, we now have access to a session which will be using when we go to that payment,
succeeded Handler and do an HTP redirect because we can redirect, but we need to save some information
before we do it.
OK, so that gets us set up.
And the next step is to well, we're going to have to write that little bit of school code, that database
functionality to save certain information.
The customer, for example.
I think that's the one we have left to do.
And then we'll start implementing in the handlers these three functions.
So we'll create a new customer, get the ID, created a new order and put the order information, including
the customer ID into that, and then create the new transaction and put all the necessary information
in that.
And we'll get started on that in the next lecture.