WEBVTT 00:01.750 --> 00:06.880 So let's keep going right now, we have our handlers doing nothing more than grabbing information from 00:06.880 --> 00:11.720 our transaction and from the form and displaying a receipt to the end user. 00:11.740 --> 00:15.440 And of course, we want to save the transaction and the other information. 00:16.330 --> 00:19.210 Now, if you think this through, if we go back to our form. 00:20.530 --> 00:23.200 So here's our buy widget for right now. 00:23.200 --> 00:25.380 I'm charging a credit card, which is fine. 00:25.390 --> 00:30.400 I don't want to store that information, but I do get the last four digits and I do get the expiry date. 00:31.360 --> 00:35.920 And we have nowhere to put our cardholder name and our cardholder email. 00:35.920 --> 00:39.970 And of course, in production, you might be getting address, information, special instructions, 00:39.970 --> 00:41.170 whatever it may be. 00:41.590 --> 00:46.630 But for our purposes, I'm going to consider a customer nothing more than a name and an email address 00:46.630 --> 00:51.640 and any orders associated with that customer and any transactions associated with that customer. 00:52.750 --> 00:54.490 So we need somewhere to put this information. 00:54.500 --> 01:01.660 So let's go back to our I.D. and let's open our terminal and let's create a new database table. 01:01.660 --> 01:08.590 And let's also at the same time add the expiry month and expiry year to the transaction table. 01:08.680 --> 01:10.380 And that's really easy using soda. 01:10.390 --> 01:12.220 So I'm in the root level of my application. 01:12.350 --> 01:19.360 I type so to generate and then Fiz is the format I want to generate and I'll just call this create a 01:19.360 --> 01:26.400 customer table, so create customer table and that will generate two new migrations up and down. 01:26.860 --> 01:31.150 So let's find our migration's folder and let's open the up migration. 01:31.900 --> 01:36.130 Now, this is going to be creating a table I want I want to do to save some time as our users table 01:36.130 --> 01:37.170 is almost identical. 01:37.180 --> 01:42.270 In fact, we could use the user's table, but I'm not going to I'm going to create things separately 01:42.280 --> 01:43.810 so, so openly. 01:43.990 --> 01:48.010 Create users, table up is select all and copy that. 01:48.850 --> 01:55.510 And then I'll open the new transaction or a new migration, which is create customer table up pastry 01:55.810 --> 01:57.910 and just change this to customers. 02:00.370 --> 02:03.370 And I'll get rid of the password field because we're going to make this simple. 02:03.370 --> 02:04.660 We're not going to have people log in. 02:04.660 --> 02:09.070 Everyone is a new customer and I'll put this to be customers. 02:09.460 --> 02:15.910 And this is just to set the default values for creating an updated to customers. 02:16.060 --> 02:18.640 And I'm not going to insert anything into it, so I'll delete that. 02:18.880 --> 02:19.330 Perfect. 02:20.830 --> 02:23.590 And I will do the down migration, which is good practice. 02:23.890 --> 02:29.110 So we'll say drop underscore table and then it's just customers. 02:30.250 --> 02:31.760 OK, so that one's done. 02:32.200 --> 02:34.000 So let's clear the screen in our terminal. 02:34.000 --> 02:40.690 And so to generate fees and calls to transactions 02:43.060 --> 02:45.850 and we'll open the migration for that. 02:45.850 --> 02:48.070 And actually we have one here that adds a column. 02:48.110 --> 02:50.620 So again, I can copy and paste just to make it simpler. 02:50.620 --> 02:51.600 So I'll copy that. 02:51.610 --> 02:58.270 That's add image to widgets, update fees, and now I'll open the up transaction or migration that we 02:58.270 --> 02:58.930 just created. 02:59.440 --> 03:07.850 And our table is transactions and we want expiry month. 03:09.670 --> 03:11.110 Now what's the type for those? 03:11.110 --> 03:12.390 Go to handlers and find out. 03:13.330 --> 03:18.610 So expiry month right here that comes as a you in sixty four. 03:18.790 --> 03:26.830 I'll just convert it to an end, so I'll leave it back to my migration and call that an integer and 03:26.830 --> 03:34.000 we'll give it a default value of zero and I'll duplicate this row and change that one to expire. 03:34.840 --> 03:41.860 OK, let's do our down migrations and again I'll copy the ad image to widgets down one and just modify 03:41.860 --> 03:51.640 that copy and open the ad calls to transactions down Paiste and I'm going to drop from transactions. 03:53.740 --> 04:01.870 Then I'm going to drop expiry month and I'll duplicate this and drop expiry year. 04:05.840 --> 04:10.790 OK, so now I should be able to run so to my group and there. 04:10.820 --> 04:16.250 So now we have we now have a customers table and we've added those two columns to our transaction's 04:16.250 --> 04:16.550 table. 04:17.750 --> 04:20.060 So that means we need to go over to our models. 04:21.140 --> 04:22.430 So let's find models. 04:23.790 --> 04:27.090 Right here, and I'll find the user model and copy 04:31.080 --> 04:35.130 and paste this in here, so user will become customer. 04:39.840 --> 04:48.710 Customer and customers, and there is no password field, so I'll delete that now. 04:48.730 --> 04:49.390 Are we done? 04:49.590 --> 04:50.860 Let's think this through. 04:51.060 --> 04:54.910 I have a customer, I have a transaction and I have an order. 04:55.860 --> 04:57.630 What kinds of things? 04:57.750 --> 05:02.400 Things am I going to need to associate customers with orders? 05:02.910 --> 05:06.830 Well, if we go to order, we have a transaction ID. 05:06.870 --> 05:11.010 Why don't we just add a column to that as well called. 05:14.850 --> 05:21.600 Customer and in Jason, we'll call that customer underscore ID. 05:25.420 --> 05:32.890 OK, so now we need to add that column, customer IDs to orders so we can get from our customer to an 05:32.890 --> 05:38.870 order by customer ID and then from a transaction from from an order to a transaction by Transaction 05:38.870 --> 05:38.950 I. 05:39.250 --> 05:41.600 And that seems to be a pretty simple relationship. 05:42.160 --> 05:46.690 So that means, of course, we'll have to go and clear our street in the terminal and type. 05:46.690 --> 05:54.070 So to generate fees and customer ID two orders 05:57.700 --> 05:59.560 and that gives me two new migration's. 05:59.560 --> 06:00.550 So we'll open those up. 06:00.970 --> 06:03.490 So we have this one and we have this one. 06:04.480 --> 06:08.650 And I'll just again copy and paste so I'll see you select copy. 06:08.770 --> 06:09.610 Close that. 06:09.760 --> 06:16.270 Finally up transaction remigration for add customers and columns to customers. 06:16.490 --> 06:17.200 Delete that. 06:18.160 --> 06:22.270 And this would be to go to add customer to orders up. 06:22.320 --> 06:25.170 That's the one I'm working with and customer ID two orders up. 06:25.960 --> 06:27.610 So we want to put this into orders. 06:28.630 --> 06:37.330 We want this to be customer ID, we want it to be an integer and it needs to be exactly the same type 06:37.720 --> 06:42.640 as the ID in customers, which is an integer. 06:43.600 --> 06:49.120 So we'll make this unsigned through. 06:50.940 --> 06:53.470 OK, so that should be the correct format for that. 06:54.150 --> 07:05.070 Now, I also want to create a foreign key relationship between the field customer ID in orders to the 07:05.310 --> 07:07.700 field I.D. in customers. 07:08.010 --> 07:12.900 So the syntax for that in Fiz is and underscore foreign. 07:13.680 --> 07:14.910 Make sure you spell that right. 07:15.030 --> 07:16.200 Underscore key. 07:16.920 --> 07:25.260 We want to put this on orders and we want it to be on the field customer ID. 07:28.050 --> 07:37.110 And the relationship is and this is in curly brackets to the table customers, then a colon, then square 07:37.110 --> 07:37.770 brackets. 07:39.030 --> 07:48.300 And that has to be in quotes then between the closing curly bracket and the parentheses, put a comma 07:48.300 --> 07:49.800 and more curly brackets. 07:50.190 --> 07:53.040 And you have two things to handle on delete. 07:53.410 --> 07:56.100 And I'll just say Kaskade. 08:00.380 --> 08:10.820 And on update also Kaskade, OK, that's a little tricky to write, and if you have trouble getting 08:10.820 --> 08:14.450 it, you can always download the source code for this lecture and copy my migration's. 08:14.450 --> 08:19.070 If you're finding it problematic, and I'll be honest with you, I usually make a mistake when I type 08:19.070 --> 08:20.230 of the foreign key. 08:20.240 --> 08:21.710 For some reason I'm not sure why. 08:23.630 --> 08:31.070 OK, so there's our column and now we can go to our down, so I'll copy the down migration from ad calls 08:31.070 --> 08:31.910 to transactions. 08:31.910 --> 08:32.930 I copy one of these 08:35.630 --> 08:43.530 and this down migration will become a drop column orders customer writing. 08:45.350 --> 08:48.380 OK, now if we got everything right, I should be able to run that migration. 08:48.530 --> 08:51.990 So to my right and there it is. 08:52.010 --> 08:55.650 OK, so we've got these set up now that those are set up. 08:55.910 --> 09:03.950 Of course, what's going to happen next is if we go to our handlers or we're going to do here and it 09:03.950 --> 09:07.310 might not be the payment succeeded handler that we use. 09:07.310 --> 09:13.010 Ultimately, we might make a different receipt page and checkout process for customers on the front 09:13.010 --> 09:13.330 end. 09:14.000 --> 09:18.050 But what we're going to do, in essence, is, again, pass the form, grab the information from the 09:18.050 --> 09:19.730 form because we need all this information. 09:20.270 --> 09:24.640 Then we're going to generate a card and we're going to get a payment intent and get the payment method. 09:24.650 --> 09:25.670 None of that will change. 09:26.000 --> 09:32.810 But once we have this information at this point, we're going to save the customer information to the 09:32.810 --> 09:35.990 customer table in the database, which we haven't written yet. 09:36.000 --> 09:41.030 We'll have to write that little bit of school and the associated methods, and that's straightforward. 09:41.600 --> 09:48.500 Then we'll get the customer ID from that insert, then we'll create an order ID and we'll insert an 09:48.500 --> 09:53.740 order, including the customer ID and all the information necessary to populate the orders table. 09:54.170 --> 09:59.000 And when we get that back, then we'll create a transaction and insert the transaction information, 09:59.000 --> 10:00.770 or we might do it in a slightly different order. 10:00.860 --> 10:03.980 But that's the general gist of what we're going to do next. 10:04.610 --> 10:06.260 OK, that's enough for this time around. 10:06.260 --> 10:08.330 We have a place to store this information. 10:08.340 --> 10:12.560 We've updated the tables as necessary and we've updated the models as necessary. 10:13.190 --> 10:17.930 So in the next lecture, we'll get started saving some actual information to the database.