- 00:00:01 welcome back everyone we're nearing the
- 00:00:03 end of this serious so to say and our
- 00:00:07 shopping cart application here looks
- 00:00:09 really good already we will get our
- 00:00:11 products here and can add multiple
- 00:00:14 products to the cart then we can go to
- 00:00:16 our shopping cart you can check out we
- 00:00:19 can enter some testator here then like
- 00:00:22 this enter any credit card number here
- 00:00:25 and then a valid expiry date and you see
- 00:00:27 we see anything about by now now well
- 00:00:30 then we're buying this and of course
- 00:00:32 technically all are doing here is making
- 00:00:35 a charge with stripe and then we're
- 00:00:37 clearing the shopping cart so one big
- 00:00:40 thing that a certainly missing is kind
- 00:00:43 of well storing the order right now
- 00:00:46 while if the customer purchases
- 00:00:48 something we're not informed we can't
- 00:00:51 see the order it's not stored in our
- 00:00:53 database therefore yes we get the money
- 00:00:55 but the customer isn't getting that much
- 00:00:57 and we have no way of knowing what the
- 00:01:00 customer actually bought therefore I
- 00:01:02 want to conclude this series by making
- 00:01:05 sure that we're actually storing the
- 00:01:07 purchase in the database that we're
- 00:01:09 restoring an order and yes I know you
- 00:01:11 could add thousands of our things you
- 00:01:13 could add a whole admin dashboard which
- 00:01:16 allows us to create products and so on
- 00:01:17 but keep in mind this is a series about
- 00:01:19 creating a shopping cart and for me a
- 00:01:22 shopping cart means adding items to the
- 00:01:24 cart buying something and I'm storing
- 00:01:26 the order so that's what I'm focusing on
- 00:01:28 next so that we're actually able to see
- 00:01:31 what the customer purchased now since I
- 00:01:34 was talking about orders all the time
- 00:01:35 that's what I'll start with I'll create
- 00:01:38 a new order for that I'll head over in
- 00:01:41 my project and here I'll open up my
- 00:01:43 terminal which again is just a normal
- 00:01:45 terminal just included here in my IDE
- 00:01:47 and navigated into my project folder
- 00:01:49 I'll use the artisan command line tool
- 00:01:51 to make a new model at the model I wanna
- 00:01:54 create is the order model which yeah
- 00:01:58 allows me well to store orders i will
- 00:02:01 also store a migration fall with the
- 00:02:03 minus m tag here to make sure that I
- 00:02:06 couldn't configure the database in a way
- 00:02:07 I need it to be configured so I'll hit
- 00:02:10 enter and that will create this
- 00:02:12 migration form
- 00:02:13 I'll just bring down this when they were
- 00:02:16 here and then we can have a look at our
- 00:02:18 newly created file we got this order
- 00:02:21 file here and in the database folder
- 00:02:24 under migrations we got our migrations
- 00:02:26 file here which allows us to configure
- 00:02:28 the database I'll start in this order
- 00:02:31 migration file here and let's think
- 00:02:34 about what order should actually contain
- 00:02:37 or how we want to store an order I think
- 00:02:41 it should certainly contain a reference
- 00:02:43 to the customer who made this order so I
- 00:02:46 will store an integer pointing to a user
- 00:02:49 ID and for that of course we will need
- 00:02:51 to force the user to log in before
- 00:02:53 buying something currently remember it's
- 00:02:56 anonymous so everyone is able to hit
- 00:02:58 checkout but we don't need the chair to
- 00:03:00 change this the next step is I also want
- 00:03:04 to store the card and you could create a
- 00:03:09 whole lot of the frame table storing the
- 00:03:11 individual items off the card and so on
- 00:03:14 but here I'll make it easy I'm going to
- 00:03:17 serialize this card which basically
- 00:03:19 means I'll use a PHP function which
- 00:03:21 takes my model my object my PHP object
- 00:03:25 and converts it into a string and then I
- 00:03:28 also get a function to deserialize it
- 00:03:30 later on when Anita again that way I'm
- 00:03:33 able to actually store this card in the
- 00:03:38 same well table as my order are in and I
- 00:03:41 don't need to create thousands of
- 00:03:42 different tables so that will be a text
- 00:03:45 field because will contain a string or a
- 00:03:47 long string in the end and I'll name it
- 00:03:50 cart again this will hold my serialized
- 00:03:54 object and you will see what I mean with
- 00:03:56 that later on the next field I wanna
- 00:03:59 have is the user address now here of
- 00:04:03 course you could have multiple fields –
- 00:04:05 one for the city one for the street but
- 00:04:08 to keep it simple here I'll have one
- 00:04:10 single field address where I assume that
- 00:04:12 everything is entered and that matches
- 00:04:15 my checkout form where I also have
- 00:04:17 one address line the next field I wanna
- 00:04:21 store is the name of the customer
- 00:04:23 because the customer or the user to
- 00:04:26 which I'm linking with this user ID only
- 00:04:29 has an email and a password remember
- 00:04:31 that's how we set up the user here in
- 00:04:34 the user migration file so in order to
- 00:04:36 get the name of the customer which I
- 00:04:38 would need to put it on the well
- 00:04:40 shipping batch and put onto on the
- 00:04:43 package I ship for example in order to
- 00:04:46 get that name I do have this extra form
- 00:04:49 filled in a checkout form and I want to
- 00:04:51 store the name here so this will be a
- 00:04:53 string name now finally there's an extra
- 00:04:59 field which will hold my payment ID and
- 00:05:07 what does payment ID mean well whenever
- 00:05:10 I make a purchase on stripe this payment
- 00:05:13 has an ID and I'm going to show you that
- 00:05:16 in my stripe account so if I had I were
- 00:05:18 to stripe here and I should already be
- 00:05:22 logged in yes I can open my dashboard
- 00:05:24 and in this dashboard you see him at a
- 00:05:27 bunch of testing purchases here
- 00:05:30 yesterday basically I can hit where I
- 00:05:33 can have a look at my payments here at
- 00:05:35 the transaction payments and then you
- 00:05:37 see all the test charges I made and if I
- 00:05:39 click on one of the charges you
- 00:05:41 basically see this ID here now that idea
- 00:05:45 is important because this ID will also
- 00:05:48 be sent with the reply to my SDK to my
- 00:05:52 PHP function which makes the charge as a
- 00:05:54 response so I can store this ID on my
- 00:05:58 server order in the database and I want
- 00:06:01 to store it here so that I'm able to
- 00:06:02 match payments with orders so that I see
- 00:06:06 in my stripe account I got this payment
- 00:06:07 and I can see it that's this ID have a
- 00:06:10 look at my own orders table here and see
- 00:06:13 the same ID to match the order at the
- 00:06:16 payment so that's just something which
- 00:06:18 allows me to do that matching with that
- 00:06:21 my orders table is set up at the next
- 00:06:24 step is of course to work on the
- 00:06:26 order model here in the order model I
- 00:06:29 want to set up a relation to the user
- 00:06:31 because I know that each order will be
- 00:06:34 related to one user so I can basically
- 00:06:36 set up this relation here I have my user
- 00:06:39 and I know that
- 00:06:41 well again each order has one user so
- 00:06:45 this will be a one-to-many relationship
- 00:06:47 because each user may have many orders
- 00:06:51 so I set up belongs to here Kochi
- 00:06:55 because again each order belongs to one
- 00:06:57 user so this belongs to app user and of
- 00:07:01 course in the other hand in the user
- 00:07:03 model here I'll also create a new
- 00:07:06 function a new relation where I have
- 00:07:08 many orders which is why this is plural
- 00:07:11 and here I will return this has many and
- 00:07:16 then here I want to have app order so
- 00:07:20 refer to my order object with that I got
- 00:07:23 this relation set up in the way I needed
- 00:07:25 to have set up so with that the order
- 00:07:28 model is set up the order table is set
- 00:07:31 up that's a huge important first step
- 00:07:33 the next step of course is to then go to
- 00:07:36 the product controller and use this
- 00:07:38 orders object or this order model to
- 00:07:41 actually save the order wants to use or
- 00:07:44 makes the purchase that will be the next
- 00:07:46 step so in order to save this order
- 00:07:51 whenever you make a purchase I'm going
- 00:07:53 to go to my product controller here and
- 00:07:56 here you can see my well API key and so
- 00:08:00 on and I'm going to change this after
- 00:08:02 recording this video of course and here
- 00:08:05 currently what we're doing is we're just
- 00:08:07 cleaning or clearing the cart the
- 00:08:10 session is to lead a session forgets the
- 00:08:13 cart so that's all we're doing we're not
- 00:08:15 saving the order so before clearing the
- 00:08:18 cart or clearing the session card here I
- 00:08:20 should save this order of course the
- 00:08:24 right place to save to order though is
- 00:08:26 not here but instead it's here in this
- 00:08:30 try block because I only want to save an
- 00:08:33 order if we are successful with the cart
- 00:08:35 and with the charge excuse me so I
- 00:08:37 certainly don't want to do it here
- 00:08:39 because
- 00:08:39 we're reaching this point here no matter
- 00:08:41 if we're successfully charging or not I
- 00:08:43 also said that this charge create method
- 00:08:47 here will give us a response which for
- 00:08:50 example contains the payment ID so in
- 00:08:53 order to use this I'm going to assign
- 00:08:55 this to a variable charge this will
- 00:08:57 contain the response stripe sends me
- 00:09:00 back with that I can now create a new
- 00:09:03 order here for that of course I'll first
- 00:09:06 need to import it so I'm going to add an
- 00:09:08 import to app or appear to be able to
- 00:09:13 use the order object and stand down here
- 00:09:17 and opposed checkout function again and
- 00:09:20 try block after while making the charge
- 00:09:23 here I'm going to create a new order
- 00:09:26 like that now I could also mass assign
- 00:09:30 my values here if I configured key model
- 00:09:33 appropriately but here I'll do it one by
- 00:09:35 one to make this as explicit as possible
- 00:09:38 so my order will store the cart remember
- 00:09:42 that's how I set up this migration file
- 00:09:44 I want to store the whole cart for that
- 00:09:47 I will basically simply serialize my
- 00:09:51 cart
- 00:09:51 I'm already extracting it up here so all
- 00:09:54 I have to do here is I'll call the PHP
- 00:09:56 function serialized that's a bolt and
- 00:09:58 PHP function has nothing to do with
- 00:10:00 Larabee and all well serialize my card
- 00:10:04 here that will take my PHP object
- 00:10:07 convert it into a string and then
- 00:10:10 basically stored as string in the
- 00:10:11 database and later on I'll have the
- 00:10:13 uncie realized method here which
- 00:10:16 basically knows how to take that string
- 00:10:19 and create the old object again that's a
- 00:10:22 convenient way of well storing an object
- 00:10:24 in a database the next step is I also
- 00:10:28 want to set the address
- 00:10:31 like this and for that I need to get the
- 00:10:35 address in the first place well if we
- 00:10:37 have a look at our view at the checkout
- 00:10:40 view here in the shop you see we have
- 00:10:44 this address input though currently the
- 00:10:47 input has no name so we can't really use
- 00:10:49 it on the server so I'm going to assign
- 00:10:51 this name attribute here and that is
- 00:10:54 equal to address and watch some on it I
- 00:10:56 also want to assign the name attribute
- 00:10:59 to this well name input field here where
- 00:11:02 I fetched name of the customer because
- 00:11:04 all you store that – I'll just name this
- 00:11:07 input name so with these two name
- 00:11:11 attributes assigned to the inputs I can
- 00:11:13 go back to my product controller and
- 00:11:15 starting with the address I can take my
- 00:11:18 request which I inject into this
- 00:11:20 controller action here and simply
- 00:11:23 retrieve an input and the first input I
- 00:11:26 want to retrieve here is of course the
- 00:11:27 address and then I'm going to duplicate
- 00:11:30 this and I also want to fetch the name
- 00:11:34 here and store the username like this
- 00:11:37 the name of the customer so the next
- 00:11:40 figure when a store here is the payment
- 00:11:42 ID so payment ID like that and as I said
- 00:11:47 I can get this from this response I'm
- 00:11:50 getting on making the charge and here
- 00:11:52 I'll simply have an ID field where I can
- 00:11:54 retrieve this payment at you from and
- 00:11:56 how do I know that I can retrieve from
- 00:11:58 the ID field here well from the stripe
- 00:12:01 documentation you can find a detailed
- 00:12:03 documentation of how this response looks
- 00:12:05 like there and then you will see that
- 00:12:07 actually the ID field will hold the
- 00:12:09 payment ID so with the order object
- 00:12:12 configure here I then of course need to
- 00:12:14 create a relation or make the mapping
- 00:12:16 between this order and the appropriate
- 00:12:18 user I know that the appropriate user
- 00:12:21 will be D locked in user remember
- 00:12:23 currently I'm not forcing the user to
- 00:12:25 log in so that will definitely be the
- 00:12:27 next step but here I'll already assume
- 00:12:32 that the user is logged in I'll retrieve
- 00:12:34 the logged in user on the office aid
- 00:12:37 like this in order to be able to use
- 00:12:39 this I'll need to go I need to go to the
- 00:12:42 top of my file and I'll add
- 00:12:45 the import to disperse aid just by
- 00:12:47 adding use of here without that we could
- 00:12:50 we would get an error with that in place
- 00:12:53 I can retrieve D locked and user like
- 00:12:56 that and then on that user I want to
- 00:12:58 access the orders and here I need
- 00:13:02 parentheses after orders because I want
- 00:13:05 to build a query because I want to well
- 00:13:07 make query to the database I want to
- 00:13:09 save something to the database and to
- 00:13:11 something I wanna save is the world that
- 00:13:14 is how you save related objects to the
- 00:13:16 database first access D well the user in
- 00:13:19 this case access the orders of the user
- 00:13:21 and let's say when you order on this
- 00:13:23 well orders connection with that I'm
- 00:13:28 actually saving the order though
- 00:13:30 currently we would get an error if we
- 00:13:32 try to check out what's not being locked
- 00:13:35 in because off user would then be null
- 00:13:38 and therefore we couldn't successfully
- 00:13:39 saved us therefore we would get an error
- 00:13:42 but since well I can log in and see if
- 00:13:47 that works
- 00:13:47 theoretically I want to see if it well
- 00:13:50 works theoretically so let's have a look
- 00:13:53 as a first step only to migrate my
- 00:13:56 orders table here or my yeah order
- 00:13:59 migration fault of database so since I'm
- 00:14:01 using whom stat here I have a terminal
- 00:14:03 navigated into home state and a
- 00:14:05 homestead and in 2d project folder and
- 00:14:07 there I'm running PHP artisan migrate
- 00:14:10 like that so that created my orders
- 00:14:12 table and I also brought up sequel Pro
- 00:14:16 here which allows me to have a look at
- 00:14:18 this database as you can see here's the
- 00:14:19 orders table empty right now
- 00:14:21 here we got the products the users I
- 00:14:24 created and so on so with that I can go
- 00:14:27 back to my shopping cart application add
- 00:14:30 an item to the cart and I remember I
- 00:14:32 need to sign in otherwise it would get
- 00:14:34 an error so I will quickly do that and
- 00:14:37 later on of course we will add
- 00:14:38 functionality to force the user to sign
- 00:14:41 in so I'll sign in go to shopping cart
- 00:14:44 here click on checkout now enter some
- 00:14:47 dummy data whatever you like also we
- 00:14:50 need a credit card number a convenient
- 00:14:52 way to get one is to head over to stripe
- 00:14:54 calm and then go to their documentation
- 00:14:57 and here in the very first example we
- 00:14:59 gather dummy credit card number we can
- 00:15:02 use then enter any expiration date as
- 00:15:05 long as it is in the future any CVC code
- 00:15:08 click buy now and looks good let's have
- 00:15:13 a look at the database if I update this
- 00:15:16 table you see we got a new order here
- 00:15:18 and I'll increase the window size here a
- 00:15:21 bit as you can see we got while the date
- 00:15:24 the user ID one which is the right user
- 00:15:26 the address name the payment ID year
- 00:15:29 which is this idea mentioned earlier
- 00:15:32 stored in the strife account and we also
- 00:15:34 got this card field if we have a log
- 00:15:37 here that's exactly what I meant we have
- 00:15:39 this strange-looking string which
- 00:15:41 basically just is our card object from
- 00:15:45 before convert to a string and PHP will
- 00:15:49 be able to well recon word that to a
- 00:15:52 card object if we tell it to do so which
- 00:15:55 we'll do later on with that I well have
- 00:16:00 the possibility to make words than to
- 00:16:02 store them in the database which is a
- 00:16:03 great improvement of course still we
- 00:16:07 have this bug that only a logged in
- 00:16:09 users can make a purchase or well can
- 00:16:12 make it without getting errors but
- 00:16:14 technically everyone can still go to the
- 00:16:16 checkout page right now and also well
- 00:16:20 we're not really showing the user to
- 00:16:23 purchase anywhere it would be nice if we
- 00:16:25 go to let's say the user profile to see
- 00:16:28 all our purchases here so that will be
- 00:16:32 the two things I'll work on next