WEBVTT 00:01.050 --> 00:06.480 So let's keep working on saving customer information and order information and so on to the database 00:06.480 --> 00:06.950 locally. 00:07.440 --> 00:13.830 And before we move on, you may have noticed last time that here in Brons Dash, planned page Doug HTML 00:14.210 --> 00:20.430 had two typos because they duplicated it a lot and this dot doesn't belong there after get element by 00:20.430 --> 00:24.690 it and I fix that before I posted the source code last time for the last lecture. 00:25.350 --> 00:26.940 But that was just an oversight on my part. 00:27.270 --> 00:34.350 So let's go back to Handler's Dash APCO And we're looking at this function right now, create customer 00:34.350 --> 00:35.580 and subscribe to plan. 00:36.090 --> 00:40.880 And we added some basic error checking and we're down to this part if OK. 00:41.130 --> 00:46.530 And here's where we want to do the same sort of thing we did back in our other handlers where we create 00:46.530 --> 00:50.970 a new transaction, create an order, create a customer, all that sort of thing. 00:51.000 --> 00:51.750 So let's get started. 00:51.780 --> 00:55.520 So the first thing we want is our product I.D. What are we selling? 00:55.530 --> 00:59.580 And in this case, it's a plan, but it still exists as a row in our widgets table. 01:00.180 --> 01:03.390 So I'll get product ID and I'm going to ignore the error. 01:03.550 --> 01:06.930 You shouldn't you should put some error checking in and do whatever is necessary. 01:06.930 --> 01:12.570 But for the purposes of this exercise, I'm going to ignore it because I'm just converting zircons to 01:12.630 --> 01:18.570 my data product ID, which we got from our Jason into an end, which is what I needed to be. 01:20.100 --> 01:21.180 Now I'll create a customer. 01:21.750 --> 01:28.710 And as I said last time around, customer writing error or assign the value of I'm actually assuming 01:28.710 --> 01:36.890 every transaction is a new customer now in a detailed or production quality e-commerce application. 01:36.900 --> 01:42.540 Chances are you'd have your customers sign in and have one customer associated with many transactions. 01:42.540 --> 01:45.270 But that's an exercise that's not that difficult. 01:45.270 --> 01:49.230 And for our purposes, we're going to assume that every transaction is a new customer. 01:49.980 --> 01:53.700 Accordingly, I need to call app dot save customer. 01:53.730 --> 01:59.700 Now, that doesn't exist yet, if you recall and look at it in a minute, that same customer exists 01:59.700 --> 02:02.370 only in the code for the front end, not in the back end. 02:02.970 --> 02:08.190 So we could actually move that to its own package and call the same routine from both places. 02:08.490 --> 02:13.410 But I want to I'm going to assume that things might diverge over time, that I might be doing slightly 02:13.410 --> 02:16.380 different things on the front end than I am on the backend. 02:16.380 --> 02:17.970 So I'm just going to duplicate that code. 02:18.210 --> 02:22.350 But we're going to call same customer and we're going to pass it a few parameters. 02:23.520 --> 02:28.950 And those are dated up first name, dated last name right there. 02:29.550 --> 02:36.180 And the email dated email, just like we do on the front end and I'll check for an error. 02:36.690 --> 02:41.580 So here, I'll say it for our purposes, this is sufficient. 02:42.240 --> 02:46.440 If error is not equal to nil, then I know that's going to work. 02:46.440 --> 02:52.470 So I'm just going to call after error log print line error and I'll return. 02:52.650 --> 02:58.800 Not very friendly, but I know this is going to work and this error checking is an easy thing to revise 02:59.490 --> 03:01.230 to whatever your particular needs happen to be. 03:01.470 --> 03:05.520 So let's create the save customer routine or function. 03:05.790 --> 03:10.320 So what I can do is open up the handlers for our front end and copy that. 03:10.950 --> 03:15.870 So let's find handlers and find Save Customer 03:20.280 --> 03:23.460 and we'll just go to that code and copy it. 03:26.640 --> 03:32.730 And go back to our handlers API and I'll just scroll down and paste that right here. 03:33.660 --> 03:40.410 And of course, we need to import models, customers, so I'll let you do that model customer right 03:40.410 --> 03:44.120 there and then I'll just delete the extra and that takes care of our import. 03:44.760 --> 03:46.810 So back up here, we now have a customer. 03:47.190 --> 03:53.450 So just like we did in the front end, next will save the transaction, will create a new transaction. 03:54.990 --> 03:56.660 And to do that, we need the amount. 03:56.670 --> 04:01.470 And again, I'm going to ignore the error here and you shouldn't amount an error. 04:01.470 --> 04:05.280 And nothing are equal to Starcom API. 04:05.550 --> 04:08.610 And we're going to get data amount, which we got from our Drayson. 04:08.850 --> 04:13.670 So now we have the amount and we'll do the same thing for expiry month and expiry year. 04:13.920 --> 04:15.300 So duplicate, duplicate. 04:15.960 --> 04:23.850 And I'll call this expiry month and I'll call this expiry year. 04:24.480 --> 04:27.150 And those come from expiry month. 04:29.200 --> 04:34.750 And Xperia and of course, these are things that I need in order to create a transaction, so now that 04:34.750 --> 04:37.570 I have all that information, I can create any transaction. 04:37.750 --> 04:44.200 Jackson is assigned the value of models transaction and I'll just populate its members. 04:44.950 --> 04:47.830 So amount is about. 04:50.280 --> 04:56.280 Currency and I'll just hardcoded this to CD, so I'm not actually passing that as a parameter and you 04:56.280 --> 05:02.220 can use whatever currency you want or you can pull that information from the front end as necessary. 05:02.230 --> 05:04.290 But this is sufficient for our purposes right now. 05:05.760 --> 05:08.100 Last for is data at last for. 05:11.850 --> 05:19.680 Expiry month is expiry month and expiry year is expiry year. 05:22.260 --> 05:27.780 Now, the next one we used on the other side, on the front end was bank return to what we actually 05:27.780 --> 05:34.080 don't get a bank return coat, nothing that I can do anything with from the result that's passed to 05:34.080 --> 05:34.380 us. 05:34.500 --> 05:40.530 Knock on the front end, because when a somebody subscribes to a plan and stripe, it actually creates 05:40.530 --> 05:45.400 an invoice and it lets that invoice sit for a little while for reasons I'll never understand. 05:45.510 --> 05:50.700 We don't get a bank return code at this point, so I'm going to leave that out, which means it'll be 05:50.700 --> 05:54.120 an empty string, which is fine, and transaction status. 05:54.510 --> 05:57.120 I'll set the two just like we did on the front end. 05:58.440 --> 06:06.060 Now that I have that scroll up and give us some room, I can get my transaction ID and potentially an 06:06.060 --> 06:10.800 error from Aptos saved transaction, which again doesn't exist yet. 06:10.800 --> 06:17.850 But we'll duplicate that code and we have to hand it the transaction and we'll do an error check, which 06:17.850 --> 06:22.150 I'll just copy here and paste here. 06:24.870 --> 06:26.700 Now let's go back to Handler's. 06:26.700 --> 06:33.960 Don't go and find that safe transaction and copy it and go back to Handler's API and paste that right 06:33.960 --> 06:34.530 down here. 06:36.750 --> 06:42.470 OK, so that should fix the air up here, and it does so that we have the transaction. 06:43.020 --> 06:44.550 So the next step is to create an order. 06:48.390 --> 06:53.770 So the order is just a model stock order and it has certain fields in it. 06:54.180 --> 06:56.070 So we did it in our cases. 06:56.070 --> 06:56.880 Product ID, 06:59.730 --> 07:00.840 transaction ID. 07:03.540 --> 07:13.680 Is an I.D. I just got back up on line one sixty six customer ID is customer ID, which we got a little 07:13.680 --> 07:17.040 earlier in this function status ID heard code. 07:17.040 --> 07:19.110 That one the same as we did on the front end. 07:19.620 --> 07:24.290 Quantity will be one hardcoded, but we're only selling one thing at a time. 07:24.870 --> 07:26.400 Amount is about. 07:28.690 --> 07:35.670 And created it is time now to duplicate that and change it, to update it up 07:38.650 --> 07:41.190 again, again, we're going to call a non-existent function. 07:41.320 --> 07:48.190 We don't care about the result, but we do care about the error is equal to Aptos save order, which 07:48.190 --> 07:50.980 will have to copy and paste in a minute and order. 07:51.130 --> 07:52.870 But let's get our error checking first. 07:54.940 --> 07:56.680 And paste it right in here. 07:59.430 --> 08:08.850 Scroll to the bottom here and go back to handler's stop, go and get safe order, so I copy and paste 08:08.850 --> 08:10.160 it right at the bottom of this file. 08:11.730 --> 08:16.140 So that should take care of this. 08:17.360 --> 08:23.210 Good, so the last thing we're worrying about is message, what is message going to be able to so right 08:23.210 --> 08:24.340 there, I'll get to that comment. 08:24.860 --> 08:28.500 Let's go back up and populate this with a message as necessary. 08:28.700 --> 08:29.780 So back up here. 08:31.790 --> 08:40.100 So up here, I'll create a new variable, which I'll call text message or just text messaging, and 08:40.100 --> 08:47.310 I'll make the assign the value of transaction success, OK? 08:47.990 --> 08:58.850 And down here, let me assign MSG equal to MSG, because if something goes wrong in creating a customer, 08:58.850 --> 09:03.450 we have some value in here and we'll populate this variable with that message. 09:04.370 --> 09:05.920 Same thing down here. 09:06.950 --> 09:16.660 We'll just say at MSG and MSG is now equal to error subscribing customer. 09:19.700 --> 09:23.240 And down here, if we have an error here, we're just returning. 09:23.240 --> 09:25.250 If we have an error here, we're just returning. 09:25.640 --> 09:26.350 Returning. 09:26.660 --> 09:29.200 So this becomes text message. 09:30.750 --> 09:35.510 OK, so if everything went according to plan, I should be able to compile this. 09:35.780 --> 09:38.420 So let's clear a screen make start. 09:41.690 --> 09:42.660 OK, so it's running. 09:42.710 --> 09:49.790 Let's go back to our Web browser, let's go subscribe to a plan and I'll open the JavaScript Journal 09:49.880 --> 09:52.520 terminal so we can see any errors that might occur. 09:53.870 --> 09:55.160 And we'll subscribe. 09:55.460 --> 09:56.000 Bruce. 09:56.240 --> 09:56.840 Bruce Wayne. 09:58.610 --> 10:10.910 Bruce w e dot com for Wayne Enterprises, Bruce Wayne, four to four to four to four to four to four 10:10.910 --> 10:15.830 two four two four two zero one twenty nine seven eight nine. 10:15.920 --> 10:17.440 All right, let's see what we got wrong. 10:18.200 --> 10:19.610 Pay twenty dollars a month. 10:22.320 --> 10:28.030 And we have to get of it by ideas, no, on Brauns line one ninety eight. 10:28.050 --> 10:30.860 Let's see what that is getting by it. 10:30.990 --> 10:35.220 Last name, so let's see what we actually call that. 10:36.240 --> 10:41.910 We'll go back to our front end, look at the bronze plan, hide the terminal. 10:42.550 --> 10:47.790 Let's find the last name I.D. cardholder name. 10:47.790 --> 10:48.930 That should be last name. 10:48.930 --> 10:49.830 So let's fix the. 10:51.540 --> 10:55.150 Save that, OK? 10:55.440 --> 11:02.100 That was a copy and paste error, so let's stop our application of make stop, make start. 11:05.670 --> 11:11.820 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 11:11.820 --> 11:15.150 page and get back to my JavaScript console. 11:15.370 --> 11:26.670 It's clear that this time let's try Clark Kent, Clark Kent, Clark Daily Planet, dot com, Clark Kent, 11:27.150 --> 11:34.080 and for two zero one twenty eight three, four or five. 11:35.070 --> 11:35.660 Let's try again. 11:38.630 --> 11:44.450 Unexpected data at line one of column one of the Jason data, let's see what that error message is, 11:45.600 --> 11:52.470 kind of unmerciful number into a ghost truck field, its payload of type string, of course. 11:53.060 --> 11:54.320 So let's come down here. 11:56.770 --> 11:58.160 Find where we're populating. 11:59.560 --> 12:06.010 It's in our payload, so expiry month and expiry year are coming back as numbers, which means that 12:06.010 --> 12:09.040 I can go back to my handlers API. 12:14.170 --> 12:20.320 See what I have the mention here, I the strings, so I think all I have to do here is change this to 12:20.320 --> 12:29.050 an end and change this to an end, which means I scroll all the way down to where I'm dealing with those. 12:30.190 --> 12:40.420 I can comment these two old and make this data expiry month and make this data expiry year. 12:43.730 --> 12:49.970 OK, let's stop our application next stop start. 12:53.290 --> 13:00.550 Let's try that again, so back to our Web browser, go back to a different page, come back to our subscription 13:00.550 --> 13:02.550 page, clear this out. 13:03.100 --> 13:04.160 Third time's the charm. 13:04.450 --> 13:16.210 So back to St. Lois Lane, Lois Daily Planet dot com, Lois Lane, four to four to 13:19.190 --> 13:21.700 make that twenty nine and make this five, four, three. 13:22.030 --> 13:22.390 All right. 13:22.450 --> 13:23.860 Let's see if it works this time around. 13:29.510 --> 13:37.350 That's encouraging transaction successful, so let's go look at the database and see if we have under 13:37.610 --> 13:40.190 customers and refresh this. 13:43.450 --> 13:55.540 Clear the refresh this, we have Lois Lane and she's customer ID 13, let's go to transactions and we 13:55.540 --> 14:00.010 have here good no bank return code because we have nothing to store in there. 14:00.610 --> 14:01.480 And that's good. 14:01.480 --> 14:10.960 And let's look at orders and we have a refresh this just to make sure to 15 one and customer ID 13 looks 14:10.960 --> 14:11.260 good. 14:11.920 --> 14:12.460 All right. 14:12.640 --> 14:13.940 We're getting closer now. 14:13.950 --> 14:15.090 We still have some work to do. 14:15.100 --> 14:20.400 We want to make sure that this form doesn't stay here after the transaction is complete. 14:20.530 --> 14:25.630 So we'll need to do a little bit of JavaScript magic here to hide this form and display a message. 14:25.630 --> 14:27.660 And we'll take care of that in the next election.