So let's keep working on saving customer information and order information and so on to the database
locally.
And before we move on, you may have noticed last time that here in Brons Dash, planned page Doug HTML
had two typos because they duplicated it a lot and this dot doesn't belong there after get element by
it and I fix that before I posted the source code last time for the last lecture.
But that was just an oversight on my part.
So let's go back to Handler's Dash APCO And we're looking at this function right now, create customer
and subscribe to plan.
And we added some basic error checking and we're down to this part if OK.
And here's where we want to do the same sort of thing we did back in our other handlers where we create
a new transaction, create an order, create a customer, all that sort of thing.
So let's get started.
So the first thing we want is our product I.D. What are we selling?
And in this case, it's a plan, but it still exists as a row in our widgets table.
So I'll get product ID and I'm going to ignore the error.
You shouldn't you should put some error checking in and do whatever is necessary.
But for the purposes of this exercise, I'm going to ignore it because I'm just converting zircons to
my data product ID, which we got from our Jason into an end, which is what I needed to be.
Now I'll create a customer.
And as I said last time around, customer writing error or assign the value of I'm actually assuming
every transaction is a new customer now in a detailed or production quality e-commerce application.
Chances are you'd have your customers sign in and have one customer associated with many transactions.
But that's an exercise that's not that difficult.
And for our purposes, we're going to assume that every transaction is a new customer.
Accordingly, I need to call app dot save customer.
Now, that doesn't exist yet, if you recall and look at it in a minute, that same customer exists
only in the code for the front end, not in the back end.
So we could actually move that to its own package and call the same routine from both places.
But I want to I'm going to assume that things might diverge over time, that I might be doing slightly
different things on the front end than I am on the backend.
So I'm just going to duplicate that code.
But we're going to call same customer and we're going to pass it a few parameters.
And those are dated up first name, dated last name right there.
And the email dated email, just like we do on the front end and I'll check for an error.
So here, I'll say it for our purposes, this is sufficient.
If error is not equal to nil, then I know that's going to work.
So I'm just going to call after error log print line error and I'll return.
Not very friendly, but I know this is going to work and this error checking is an easy thing to revise
to whatever your particular needs happen to be.
So let's create the save customer routine or function.
So what I can do is open up the handlers for our front end and copy that.
So let's find handlers and find Save Customer
and we'll just go to that code and copy it.
And go back to our handlers API and I'll just scroll down and paste that right here.
And of course, we need to import models, customers, so I'll let you do that model customer right
there and then I'll just delete the extra and that takes care of our import.
So back up here, we now have a customer.
So just like we did in the front end, next will save the transaction, will create a new transaction.
And to do that, we need the amount.
And again, I'm going to ignore the error here and you shouldn't amount an error.
And nothing are equal to Starcom API.
And we're going to get data amount, which we got from our Drayson.
So now we have the amount and we'll do the same thing for expiry month and expiry year.
So duplicate, duplicate.
And I'll call this expiry month and I'll call this expiry year.
And those come from expiry month.
And Xperia and of course, these are things that I need in order to create a transaction, so now that
I have all that information, I can create any transaction.
Jackson is assigned the value of models transaction and I'll just populate its members.
So amount is about.
Currency and I'll just hardcoded this to CD, so I'm not actually passing that as a parameter and you
can use whatever currency you want or you can pull that information from the front end as necessary.
But this is sufficient for our purposes right now.
Last for is data at last for.
Expiry month is expiry month and expiry year is expiry year.
Now, the next one we used on the other side, on the front end was bank return to what we actually
don't get a bank return coat, nothing that I can do anything with from the result that's passed to
us.
Knock on the front end, because when a somebody subscribes to a plan and stripe, it actually creates
an invoice and it lets that invoice sit for a little while for reasons I'll never understand.
We don't get a bank return code at this point, so I'm going to leave that out, which means it'll be
an empty string, which is fine, and transaction status.
I'll set the two just like we did on the front end.
Now that I have that scroll up and give us some room, I can get my transaction ID and potentially an
error from Aptos saved transaction, which again doesn't exist yet.
But we'll duplicate that code and we have to hand it the transaction and we'll do an error check, which
I'll just copy here and paste here.
Now let's go back to Handler's.
Don't go and find that safe transaction and copy it and go back to Handler's API and paste that right
down here.
OK, so that should fix the air up here, and it does so that we have the transaction.
So the next step is to create an order.
So the order is just a model stock order and it has certain fields in it.
So we did it in our cases.
Product ID,
transaction ID.
Is an I.D. I just got back up on line one sixty six customer ID is customer ID, which we got a little
earlier in this function status ID heard code.
That one the same as we did on the front end.
Quantity will be one hardcoded, but we're only selling one thing at a time.
Amount is about.
And created it is time now to duplicate that and change it, to update it up
again, again, we're going to call a non-existent function.
We don't care about the result, but we do care about the error is equal to Aptos save order, which
will have to copy and paste in a minute and order.
But let's get our error checking first.
And paste it right in here.
Scroll to the bottom here and go back to handler's stop, go and get safe order, so I copy and paste
it right at the bottom of this file.
So that should take care of this.
Good, so the last thing we're worrying about is message, what is message going to be able to so right
there, I'll get to that comment.
Let's go back up and populate this with a message as necessary.
So back up here.
So up here, I'll create a new variable, which I'll call text message or just text messaging, and
I'll make the assign the value of transaction success, OK?
And down here, let me assign MSG equal to MSG, because if something goes wrong in creating a customer,
we have some value in here and we'll populate this variable with that message.
Same thing down here.
We'll just say at MSG and MSG is now equal to error subscribing customer.
And down here, if we have an error here, we're just returning.
If we have an error here, we're just returning.
Returning.
So this becomes text message.
OK, so if everything went according to plan, I should be able to compile this.
So let's clear a screen make start.
OK, so it's running.
Let's go back to our Web browser, let's go subscribe to a plan and I'll open the JavaScript Journal
terminal so we can see any errors that might occur.
And we'll subscribe.
Bruce.
Bruce Wayne.
Bruce w e dot com for Wayne Enterprises, Bruce Wayne, four to four to four to four to four to four
two four two four two zero one twenty nine seven eight nine.
All right, let's see what we got wrong.
Pay twenty dollars a month.
And we have to get of it by ideas, no, on Brauns line one ninety eight.
Let's see what that is getting by it.
Last name, so let's see what we actually call that.
We'll go back to our front end, look at the bronze plan, hide the terminal.
Let's find the last name I.D. cardholder name.
That should be last name.
So let's fix the.
Save that, OK?
That was a copy and paste error, so let's stop our application of make stop, make start.
And we'll go back to our Web browser and I'll just go back to the buy one widget page and back to this
page and get back to my JavaScript console.
It's clear that this time let's try Clark Kent, Clark Kent, Clark Daily Planet, dot com, Clark Kent,
and for two zero one twenty eight three, four or five.
Let's try again.
Unexpected data at line one of column one of the Jason data, let's see what that error message is,
kind of unmerciful number into a ghost truck field, its payload of type string, of course.
So let's come down here.
Find where we're populating.
It's in our payload, so expiry month and expiry year are coming back as numbers, which means that
I can go back to my handlers API.
See what I have the mention here, I the strings, so I think all I have to do here is change this to
an end and change this to an end, which means I scroll all the way down to where I'm dealing with those.
I can comment these two old and make this data expiry month and make this data expiry year.
OK, let's stop our application next stop start.
Let's try that again, so back to our Web browser, go back to a different page, come back to our subscription
page, clear this out.
Third time's the charm.
So back to St. Lois Lane, Lois Daily Planet dot com, Lois Lane, four to four to
make that twenty nine and make this five, four, three.
All right.
Let's see if it works this time around.
That's encouraging transaction successful, so let's go look at the database and see if we have under
customers and refresh this.
Clear the refresh this, we have Lois Lane and she's customer ID 13, let's go to transactions and we
have here good no bank return code because we have nothing to store in there.
And that's good.
And let's look at orders and we have a refresh this just to make sure to 15 one and customer ID 13 looks
good.
All right.
We're getting closer now.
We still have some work to do.
We want to make sure that this form doesn't stay here after the transaction is complete.
So we'll need to do a little bit of JavaScript magic here to hide this form and display a message.
And we'll take care of that in the next election.