Setup Stripe, create Stripe Customers and Products - clearbnb - Part 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back this is episode 10 uh where we're building clear bnb a vacation rental platform on ruby on rails to go through the process of just building out a bunch of different features in the last episode we did some image uploading with active storage in this episode what i wanted to do was build out uh work on our listing detail page just a little bit and add a button so that someone could actually book a listing so right now we don't actually have prices for listings we don't have anything about cleaning fees or any of that and so what i want to do is add some data that we can use to track the price for a night at a listing and then a button to book the listing um and then just clean up the listing show page so what i'm what i'm going to do is use stripe checkout which is a stripe hosted thing that you'll like redirect customers to and they can pay directly through stripe checkout in practice you should be able to set a different nightly rate for every single date because based on demand you might want to save have like a really high price for fourth of july weekend but a really low price for just a random weekend in september or something right and so uh you that's like a really common practice in the industry is to have to have the ability to set prices per day in sort of a calendar we don't have any calendaring systems yet so what we're going to do is for now we're just going to store the price for a night on the listing model itself so let's do that first so rails g migration add price to listing and price is going to be an integer and we're going to store it in cents so um that we don't have to do any like so that math is easier because working with floating point numbers and math is no fun uh so let's just make sure this looks good yeah we're adding christ it's an integer i guess it could only be positive but we can add validations like inside of the model um uh default no i don't know let's just let's just leave it as it so rails db migrate so i was thinking we could make the default zero but then we don't want someone to accidentally publish a listing and make it bookable for zero dollars a night um you know what the other thing let's do rails db roll back because the other thing i think we're going to want is to like distinguish between the nightly price or the per night price and the cleaning fee so we might also want like taxes or something so let's let's add price to listing let's get this a little bit let's make this a little fancier so nightly price and then cleaning fee and then a cleaning fee is going to be a flat fee for the entire stay and then tax hmm yeah tax percentage and that should be a float uh should we do let's just skip taxes for now we'll skip taxes for now all right so we're gonna have a nightly price it's gonna be an integer if you stay three nights we're gonna do like the nightly price times three plus the cleaning fee that's it boom that's what you pay right um all right and then yeah okay so rails db migrate i'm going to run bundle exec annotate models again so that we can update all of our models with a comment that shows which fields are in there we'll jump into our listing here and inside of our listing we now have a nightly price and a cleaning fee we want to say validates that the nightly price numericality is greater than zero and less than um i think there's i mean okay we'll just not have a a less than there are limitations to like how much you can charge when you're inside of stripe but it's a very very high number and it's not going to be what people are paying per night so whatever we also want to do the same thing for cleaning fee um i think again in practice we're gonna want some model that is separate from the listing that tracks the prices and cleaning fees and the nights et cetera et cetera et cetera but let's just work on getting our getting our feet underneath us in terms of booking and figuring out how to pay and stuff like that again this is going to be incense i probably should have said like nightly price incense but whatever that's fine so listing edit so inside of our listing edit form let's just add a price i guess this is a nightly price nightly nightly price i mean that could sort of be part of the feature right uh of this sort of clear bnb transparent pricing platform uh is to have just a one fixed price right um but yeah i'm not sure yeah whatever and then this is going to be cleaning fee and cleaning fee cleaning fee the minimum one step one all right so then if we come back over here to our listings and we pull one up and we say edit the details we now have a nightly price and a cleaning fee and we need to go to our controller host listings controller and in here we need to allow the nightly price and the cleaning fee to be passed in on update i think i want to make those also pass like something you can pass in on create so let's just grab those and come over to the new view and also add them here and i think we like dressed up the new view a little bit more but whatever okay so let's say that our nightly price is 100 our cleaning fee is 50 we're going to update the listing see if that takes nightly oh right and this is also in cents so we need to add zeros in the end so here's our nightly price one hundred dollars and our cleaning fee is fifty dollars okay so now we're tracking the f the pricing for a listing on the listing itself that is fantastic let's go over to the listing show view for normal listings so this is all the published listings i guess we want to maybe we'll just grab listing 102 and oh it's not published so um i'm actually just gonna say what are we gonna do what are we gonna do um listing dot update are like yeah listing.all or like to update all does that work status published or i basically want to make them all published so that i don't keep running this 404 while we're testing um okay so on our listing show page on our public listing show page we want to have the price so here we can say uh we'll just yeah let's say this is class text 2xl font bold um margin bottom four then we'll give it a price here when i'm looking for semantic tags i always like get so confused about like which ones i should use or i get stuck on like analyzing whether or not i should use one and then i just end up giving up and using divs and p tags everywhere i know it's not a good practice but uh it's yeah uh it allows you to move quickly right um okay so there's another thing that you can use inside of rails which is like a currency formatter so rails currency i can never remember uh yes number two currency um which is super cool so yeah like you can say that's just like in the view we could say like instead of having the dollar sign out front right here right we can say uh number two currency and then we can pass in our number and it should just give us back a currency and it does and right now it has like the cents are showing but we could say take off take off the precision so precision is zero or something right and so now it only just shows like in dollars which is cool um the other thing we can do is if we yeah like i think uh the unit yeah so if i say unit unit like eur does that work let's see oh no okay so i thought i'd be able to pull out like i wonder if the locale would do that no it looks like that just changes the format uh unit separator delimiter unit is that currency whatever okay fine um we're starting off with usd and then like later on we can mess around with other stuff okay so we've got you you 100 this is like per night and then we also want the cleaning fee and this is going to be cleaning fee all right so we've got our per night fee and we've got our cleaning fee and we have where it's at the about us how many beds or rooms or whatever we've got some pictures and we've got some details about the host down below and for now let's just put the email again oftentimes the host will be asked to like write a little profile about themselves or whatever and that we can show later on and now let's also add a button to book so we'll just say um uh um how do we want this to work so we want to reserve it right and long term we want to make the reservations for specific dates but for now let's just say this is going to send a post request to reservations um i'm trying to think if we need to preview anything or if we can just say like reserve uh not sure okay but what we do want to do is we want to pass back the listing so input type is hidden so i'm trying to think through how we could set this up so basically we want to create some concept of a reservation between the current user as the guest and between the listing as where they're going to stay and we also want to somehow charge the money for this reservation and so right now what i'm thinking is we can cr we'll have a form that just has a button that says reserve when you click on reserve you'll be redirected to stripe checkout where you'll be prompted to pay that listing's nightly price plus cleaning fee we also need to create a reservation model internally and that reservation model should be tied somehow back to the listing and also back to the payment the checkout payment so one way we could do that is we could like nest the reservations end point under listing so it's like slash listing slash 101 slash reservations and that's like how you make the post request and that's how you get the listing id for the for which the reservation is for but i'm thinking about just passing the listing the id for the listing inside of a hidden field here and we'll call it reservation listing id and this will have the value which is the listing.id and let's see so we've got a reserve button we click on this it's opposed to reservations we don't even have a reservations controller yet we have no concept of a reservation so let's work on that rails g controller or no rails remodel reservation and this is going to reference the listing and that reservation is also going to have some sort of payment related to it so i think what we can do is store the checkout session id from stripe so session session id which is a string what else is the reservation going to have this is going to have reference back to the guest references guest i think i can't remember if it's reference or references references listing references guest and the guest in this case is going to be a user so we're going to have to like set the foreign key thing again or like tell it which table it's for and then also we need to have a reservation is generally going to have a status um [Music] and that status can be one of like pending booked or cancelled i think but again we're going to use an enum for this and we'll just set up a status to start a reservation will also have generally like a number of guests number of guests um a reservation typically will also have like the check-in date and the checkout date and um some conversation back and forth like you know between the host and guests communicating what is involved um but for now let's start with this and let's not even put the number of guests like we can add that stuff later so here we go we're generating a reservation model uh references with unknown type listing reference listing what is that unknown type listing this is totally a type right references guest let's take this one listing i don't know why that's not working just take a look at the database migration so we definitely have tdot references yeah so this should be like tdot references listing and the the one that i was curious about was okay so here we have references host and then we have the foreign key to table users because when we're naming the column differently then what we are naming we're naming the column different from what the underlying table is so in this case like any user of clearbnb can be a guest on a reservation and so we're going to call the column in the database guest underscore id but that will point at a user and it's nice to have this as guest so now that we can now when we're looking at the reservation it's clear which user that is that's the guest user that's not the host that's not the you know cleaner or whatever that is the guest so that is convenient and while we do that i'll say default to zero here i did get feedback that when you're using an enum field that you shouldn't set the default in the migration and again i'm torn about whether or not i agree with that um we could uh so as we're thinking through other things that are related to this migration one thing you might think is like oh let's create a uniqueness constraint between or like some sort of index that has like guest and listing and that'll like be a way that we can like really quickly look up the reservations and in fact you might like feel like maybe you want to make it unique so a guest can only book a listing or there's like no conflict and we only create one reservation for the guest in that listing but in practice like a guest might come back year after year or quarter after quarter and book the same listing and so i would just leave off those constraints for now and we'll call that good rails db migrate hopefully this works i don't know what the deal was with that reference thing not working before so now we can look at our reservation and this is going to belong to a listing and belong to a guest and we'll do our annotate deal again to get that up and running okay so now we should be able to create reservations actually you know what i think we need a reservations controller right reservations controller yeah we don't have one of those yet so let's make one of those rails g controller reservations so we are gonna have the guest reservations controller this is where we're going to create reservations it'll also be i guess where the guests can see their own reservations um that they've booked and then on the host side we'll want to be able to see like which guests are you hosting that are upcoming so for the red for this reservation controller this is going to be the top level one for guests so we want to have like an index we want to have a show we want to have a uh we might want an edit view i don't know we'll just keep it like this for now go to our rats file resources reservations i'm not going to add any constraints yet because i'm not sure exactly how flexible we want this to be reservations controller inside of the create action we want to create a reservation so at reservation is equal to um current user.reservations.new reservation params we need to make sure that the user model has many reservations has many reservations and we have to set the foreign key foreign key is guest id and okay so um the reservation params are going to be params.rt reservation.permit um the listing id and i think that's what we that's all we have in our form right like listing show we've got our form here and it just has the listing id and i think that's that's it so if um reservation.save then we want to like create a stripe check out session um else we want to say flash errors or maybe like yeah flash yeah flash errors reservation.errors.full messages and redirect bat redirect to um listing path for params reservation listing id okay and then otherwise if if we were able to save the reservation then we want to create a checkout session for this so we're going to say session is or like checkout session is stripe check out session.create we need to give it a success url cancel url and um payment or the mode is going to be payment and then the line items are going to be uh this is interesting so we've got to build the line items and we will have one that is going to be price the price data we're going to use price data to define a price ad hoc and the unit amount for this is going to be the nightly price for that listing so i guess we might want to say like listing is equal to at now and the unit amount here is going to be listing.nightly price and then the quantity uh for now we'll just put one but that's going to be like how many nights they booked and then the currency is going to be usd and the product so the product is technically going to be the id of well we could define a product on the fly here okay so we could either define a brand new product on the fly every time someone books or when the listing is first created we could um we could use the stripe api to create a product for that listing so that we always tie back every single payment four nights back to that product i think that's what i want to do so it's going to be something like listing.stripe product id and then we also want to pass a price data so we're going to dynamically pass in the um this is going to be like the number of nights num nights which we can derive from the reservation eventually like once we figure out how to pass in like the start date and end date of the booking so the unit amount here is going to be something currency is going to be usd product again is going to be oh gosh should we have a separate product for the cleaning fee should we have a product that is related to the the listings cleaning fee or have that be a separate product i think maybe we just have one top level product for all of clearbnb that represents the cleaning fees anyways once we have the checkout session then we can say redirect to checkout session.url uh the other thing we want to do here is we want to pass in the customer and the customer is going to be current user.stripe customer id okay so we're kind of hacking around on a bunch of stuff and i'm trying to think through all of this at the same time but what i think um okay so what what happens is as soon as we create the reservation we need to build out this checkout session which is the stripe checkout session that's what the customer or the guest is going to see when they redirect to pay when they redirect to pay we want to make sure that it shows like the listings nightly price and that the cleaning fee for the for the stay but we also want it to associate those things back with the right stripe prices and products so that we can have we can build reports and stuff later based on those those purchases so we have a couple things we need to do number one we have to go add a customer id to the user object a stripe customer id and then figure out how to populate that and create a stripe customer through the api when users are created or saved or whatever and then we separately have to do this like something similar for the listing so listing stripe product id and there's a couple different ways we could do this with we could use active record callbacks and we can use a bunch of fanciness but like in practice what i think we want to do for the customer id is right when a user signs up or maybe even when a user logs in we want to check to make sure that they have a striped customer id and if they don't we want to use the stripe api to create a stripe customer for them and then we'll always have a stripe customer related to each user and then when a listing is updated we can make sure that it has a stripe product id and if it does then we can make sure that well i was also thinking in to myself like as we add images it would be super cool if we could add those images to the stripe product so that when you redirect it shows the picture of the listing in the checkout session so all right so for the success url this is where the customer will be redirected after they successfully pay and i think we want that to be like the reservation show page so i think we want like reservation url we want to use url and not path because this needs to be like the fully qualified url with the domain and everything and the url in this case is going to be back to at um oops uh so reservation url for at reservation and then if they cancel we should just bring them back to the listing show page so listing url for listing okay so we need to add stripe customer id to user so let's go do that rails g migration add stripe customer id to users stripe customer id is a string let's open this up that looks good rails db migrate i don't think i've set up any jobs yet in this application we've done jobs in other projects so i think we're going to do this stuff synchronously for now but in practice i would do like creating the customer for the user and creating the product for the listing in the background as part of background jobs so we also need to do that for the listing right add stripe product id to listings and this is going to be stripe product id because i didn't spell listings right there i don't think this worked let's see listings okay uh railsdb migrate actually you know what for both of those we want to add a uniqueness constraint yeah whatever it doesn't matter i think we want them to be unique per like we want the customer id to be unique per user and we want the product id to be unique per listing but it it's whatever we'll just let that be flexible and fancy free for now okay so what do we want to do in the user model actually yeah let's do our annotate situation again and that crashed and burned because reservations controller we don't have all of our stuff figured out yet uh where are we oh unit amount here should be listing dot cleaning fee also um stripe is expecting these as integer sense also so that's like a nice convenient thing oh i guess we need to do rails db migrate it looks like we only annotated the user model here and so i'm guessing we didn't run the migration for the listings also which it looks like we didn't so we'll run this again now we've got it on the listing so let's go to the user model and what i want to do is here i'm going to add a rails after um yeah commit callback and we want it on update and create after commit okay so let's see so after commit maybe create strife customer uh on create update do or actually no i then i need to find a method def maybe okay and then in here what we want to say is like return if stripe customer id if the stripe customer id is not blank or if the stripe customer id yeah if the striped customer id is populated we want to return early and not do anything otherwise what we want to do is we want to create a new customer which is going to be stripe customer.create and we're going to pass in some stuff some basic stuff so email email i guess we could pass in like their name name name and then metadata so inside of the metadata i'm going to pass in the like um i guess we can call it clearbnb id and that will be self.id like the current user's actual id what's nice about this is if you ever if there's ever some issue and you have data in stripe that's pointing back at your users but for some reason you lost track of that customer id you can find these by their metadata by their clear b b app and you can kind of like query by that using the search api um so then here we want to say like self dot stripe customer id or like self.update stripe customer id is equal to customer.id so this should um pull or this should hit the stripe api create a new customer when it comes back we're going to update our local stripe customer id to be that customer uh okay so that should create a customer um and now if we come over here and we say like inside of rails console um oh actually you know what we haven't even installed the stripe gem or anything so let's go to the gem file and say gem stripe bundle install um i'm kind of like doing this from memory and i didn't really like plan out all the different pieces in the order so hopefully you're hopefully you're tracking along so inside of the stripe dashboard i'm going to create a brand new account called clear bnb it's going to be based in the united states i'm going to say create account i'm just associating this with like my demo stuff so and we're only going to use test mode for now we're not actually going to activate anything so then i'm going to go to the developers tab and under api keys i'm going to grab the api keys here for the secret key and i'm going to set that in my credentials by saying rails or like set my editor and then say rails credentials colon edit this is going to open up my credentials file and i'm going to set my stripe api keys so here i'm going to say stripe and then secret p is the thing i just copied okay so now i'm going to go into my initializers so config initializers i'm going to add a new one called stripe.rb and in here i'm going to set stripe.api key is equal to rails.application.credentials.dig stripe secret key now that should globally set my api key so that no matter where i'm at if i for instance say rails console here and then i'm expecting that if i update a user so if i say like u equals user.last now right now stripe customer id is nil and if i look in the stripe dashboard here if i go back to the developers tab and look at my logs we have no logs and so i'm expecting that if i say u.name equals like raymond demore and then if i say u.s save bang this should hit that after commit like it should right to the database hit the after commit callback make an api call to stripe to create the customer save it in save it save the customer data in stripe send back a customer id that should get saved locally we'll also see here that the user id is 21 so we're expecting the metadata to show like clear bnb metadata one and boom look at that okay so uh let's just look at this we can already see like a bunch of stuff right okay so we see the begin update commit begin update commit so there's two things happening first the commit that we're making is setting the name second the commit that we're making is setting the stripe customer id to this id cuss underscore blah blah blah that is an id from the stripe api so if we come back over here and we refresh our logs we should now see sometimes it's a little delayed but yeah okay so here we see a post request to v1 customers and we can scroll down and inside of the metadata here clearbnb21 we got the name we got all kinds of great stuff so this is completely transparent to the customer they don't actually need to see any of this stuff but what this tells me is that i think that style of update is gonna work fine um again like it would be my preference for this because this is uh because this is an api call this is an external api call i would prefer this to happen in the background but since we haven't talked about jobs or setting any of that up yet whatever it's going to happen synchronously and that's totally fine so we're going to do the same thing but over here it's going to be called maybe create stripe product the name the reason why i'm calling the method maybe i'm starting with maybe is that it's not always going to create a striped customer so instead of calling it create stripe customer and then inside of here saying like well if we already have the id technically just like return early uh then by calling the method maybe underscore i'm saying that like we may try to create a striped product we may try to create a striped customer but only if we haven't already so maybe create a striped product if the stripe product id is blank and it's actually going to look pretty similar so it's going to be something like product is equal to stripe product dot create uh and then we're gonna have like uh self.update stripe product id is product.id and when we're creating products i need to like actually look at the api reference here let's see i don't recall exactly all of the stuff that products can take so let's dig in and take a look so products can be created with name active description so the name here is going to be self.title or just title i guess i don't know i don't actually need to use self. all these places but it's kind of nice to be explicit whatever that'll make it look a little bit cleaner so we also want metadata clear bnb id is going to be id what else do we want i guess the description our description doesn't have any limitations i wonder if this one is going to matter let's leave it off the description for now for images images images accepts an array of urls that represent this product and so what i'm thinking we should do is look at photos and grab the first one photos.first um yeah photos. well there might not be any photos so what should we do there uh i think we want to do an update we i think we want to do an update that says like if there's photos and there's a striped product id already then update the product and set the the images so whatever ooh a url of a publicly accessible web page so we have that here so we can say uh oh gosh how do i get the url from inside a model url for object inside model uh rails yeah how do i do this url for it's like rails application route url helpers yes dot url4 okay i think this is actually like an anti-pattern but it's convenient and again we're taking a little bit of a shortcut here so that we can just make this happen synchronously and we've got the right stuff set up so in this case the url is going to be for self because we're gonna we're trying to generate the url for the listing and yeah that should be fine i think what else what else can we pass tax code i don't know what this is uh i guess we need to figure out like vacation or like rental or property something um guest i don't know like this is going to be some sort of real estate something residential oh residential cleaning services uh hey this is the tax code we want for the the other thing which we haven't created yet which we can do actually let's do that through the dashboard so inside of the stripe dashboard we're going to go to products we're going to create a new product and it's going to be called a cleaning fee and this product is going to be a cleaning fee for the entire stay um it's going to be right now like we have to create a price but we're not actually going to use the prices that we add here because we're going to ad hoc create the prices based on the settings of the listing so here i'm just going to set this as like one dollar it's a one-time thing and it's not going to have any images we'll just say that well this is our this is going to be our cleaning fee so we're going to copy this product id and then inside of our reservations controller this is going to be this product right here so that's our cleaning fee that's like just going to be a hard-coded id every um every charge that is related to that product we'll just say clear bnb cleaning fee that's totally fine whatever like that's that's what we want um otherwise we want to create the stripe product it's going to have a name it'll have the url it'll have the metadata and it doesn't need anything else for now and yeah so we'll call that good now let's test this out so if we say reload bang prod or like a listing dot last um l equals listening dot last l dot cleaning l dot cleaning fee is a hundred dollars a night l dot nightly price is 299. uh a knight l dot save bang and this should again this should like make the api call the stripe create the product come back save it on the listing and it failed missing host to link to okay so this is the weirdest thing but like anytime you want to generate a fully qualified url where you you have the domain that is included in the url that you're redirecting to or generating you have to specify that that domain or like the first part of the url as part of your environment there's so many different options who knew okay i thought this was all just like part of um action mailer and for a long time i feel like it was but i'm like yeah i'm pretty confused by why there's so many different ways to set the domain there we go okay that's exactly what we want all right now we want to go back in because we want to say um l is listing dot last l dot um postal code is one two three four five l dot save bang okay hits the stripe api stripe api comes back we've got a product boom if we come back over here to our dashboard and we go to products we should now have a new product for this new listing fantastic okay and there's no prices that's totally fine because the price is going to be created dynamically or ad hoc when we check out so that's cool all right so now what we want to do is go back over to our reservations controller and we've got our checkout so now our checkout session should have the current user it should have the stripe product id and it should have the cleaning fee and all that jazz and all the amazingness and i don't know let's just let's give it a whirl so if we come over to uh localhost oh look at that okay uninitialized constant stripe oh i guess we gotta restart the server here and also why is it firing that off just when we're reloading the page i don't know but i'm guessing that they're if we look in the logs yeah i'm i bet when we're logging in we're like touching the user field and because we're touching the user field what are we doing um we are updating the user for some reason as part of login like maybe we're tracking like last logged in or something and that is causing this maybe stripe customer to fire which saved the stripe customer id and now if we refresh the page it should not do that again because we've already got a striped customer id now for the user that i'm logged in as so if we go to listings find a listing uh this does not work if we don't have a nightly price so we'll say real c listing dot update all nightly or listing dot where nightly price is nil dot update all nightly price to i don't know 200 a night and cleaning fee to 60 dollars actually let's make these yeah whatever that should be fun refresh all right so this this uh tenutour seat libro has a two hundred dollar a night and sixty dollar cleaning fee if we click reserve uh oh gosh current user number one is nil and number two doesn't have reservations so let's go to the user model and say a user has oh it has many reservations okay so then back over here we just need to say before action um oh gosh what is it again controller uh authenticate user authenticate authenticate user i think i need to make like a shortcut for that because i want to use that everywhere basically um all right so let's log in so we're logging in as wave at cgi dev sign in we're signed in and now we're gonna go to listings we're gonna go to the first one we're gonna reserve it oh crash and burn rails could not find a valid model for guest association right okay so reservation guest doesn't know what type of model it is we don't have a guest model in the database we have a user model so here we have to say class name user just like this if we refresh what do we get what do we get you must either specify product or product data when creating a price and so my guess is that this listing does not have a stripe product id and dot stripe product id does not okay so what we want to do is uh what is this which listing was this listing two listing dot find two dot touch let's see if that causes it boom all right so dot touch is a way of just saying like just update something on this model or like marked this model as updated recently and so you'll see that the actual sql was to update the updated at field with a new timestamp and when it updated it and committed that caused a check on maybe create strike product and so now we have a straight product so now if i refresh this continue the form submission it should create a stripe checkout session we're redirected and holy guacamole that's amazing so now we see the nightly rate here and we see the cleaning fee the product now that i'm looking at it like i wonder if we should have the description be like um per night or something like listing so when we create the product we could put the description and set this equal to like um oh you know what we want well okay i'm i'm giving a little bit of pause because i think technically when someone is checking out if they have several nights and they're different prices we want to show different prices per night so okay uh whatever that's fine so let's take this product id though and we can say stripe product dot retrieve and we can pass in that product id and that should give us back the stripe product you'll notice that no images are set right now also you'll notice that like the email is already pre-populated to wave at cgi dev the user that's because this checkout session will be associated with that customer's information so here what i'm going to do is i'm going to fill in a test card number there's several test card numbers and here i'm just going to say wave see jav and then blah blah blah save my info for okay so you can just you could save it if you wanted i'm just going to click pay we're going to pay in test mode 260 for that reservation we're redirected back to reservations slash two okay so there's a whole bunch of stuff going on here number one how do we know that we actually got paid for that reservation and that it's actually booked number two how do we tell the user that they're like the status of the reservation and relate that back to the booking well we have we have a couple we have a couple things we need to do right here we have this session id but we're not doing anything with it yet so let's go back to the reservations controller and when we create the checkout session before we redirect let's update the reservation to have the session id be equal to the checkout session.id that will make it so that we can look up the checkout session based on the reservation and vice versa the other thing we can do is set metadata on the checkout session directly that has the reservation id and we can set this equal to at reservation.id recall that we already saved the reservation here it's already saved in the database and yeah so now it'll also be saved on the checkout session the other thing that i want to do so the checkout session just configures what the customer is going to see when they check out right but in practice what i think is really helpful is when you're creating these checkout sessions is to associate the payment back with the payment the underlying payment or it's called a payment intent and that is all like handled for you inside of checkout but a nice thing that you can do when you're creating the checkout session is you can pass metadata for that nested object so inside of payment intent data we can set the um the metadata for the payment intent data to also have that reservation id so payment intent data metadata is also going to have the same reservation id that way we have the reservation attached to the checkout session we have it attached to the ultimate payment and this is kind of like the tool that we'll also want to use later when we're doing if we were going to do any refunding or anything like that we could send the receipt email somewhere if we wanted and yeah shipping address statement stricter to do transfer data okay all of that looks cool um okay let's just look through these and see if there's anything else that we might find useful here discounts expires at if we wanted to create some tool that was like if you don't finish checking out by a certain time then we expire your checkout session um but whatever okay so this feels pretty good so far i think um let's go through it one more time and go back to listings slash 101 and this one has some images cool whatever we say reserve and oh gosh this listing doesn't have it also doesn't have it's what you call it so let's go to host listings and we'll open one up this is 103. let's edit some photos we'll add a photo boom upload and i think maybe that should have set the actually you know let's just uh listing.find103.touch okay um all right now when we click reserve we should be redirected to stripe checkout and we are and we see the cleaning fee we see all this other stuff over here um right okay next thing we want to do inside of our uh inside of our reservation model right um we want to set the status enum thing to um something similar to what we have over here for the status and it's going to start off a res reservation is going to start off in pending and then it will go to um like booked or something reserved and then it might go to cancelled but for now everything is going to start off in pending and then we will mark it as booked as soon as we get a successful payment for that reservation and the way we're going to do that is with web hooks and i think we're going to do that in another episode because this one is running a little bit long so we're basically going to listen for stripe web hooks and process them as they come in and when we process the web hook that tells us that a checkout was successful and someone paid we'll be able to look up the reservation by the id of the reservation on the metadata or by the checkout session we have like several ways we can look up the reservation we'll mark it as booked and then we will um i guess we could also like make a cancelled process where we refund the user too um and then ultimately we need to do the other side of the equation where we're paying out hosts there's a ton of fun stuff we've got to do but for now let's um let's rock it just like this and we will uh we'll leave it here and then yeah we'll pick up in the next episode thank you again so much for your time and attention hopefully this was useful we're kind of like at a point where we're almost able to book and pay for a reservation and then like mark it as booked so um yeah so we'll kind of get back at it in the next one see ya [Music] you
Info
Channel: CJ Avilla
Views: 163
Rating: undefined out of 5
Keywords: cjav_dev, web development tutorials, web development for beginners, vim, ruby, rails, javascript
Id: yxv0q2_t1so
Channel Id: undefined
Length: 57min 41sec (3461 seconds)
Published: Fri Dec 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.