We just install the striped J s library to the Express side of our project.
Let's now flip back over to our code editor inside of the building route's file and start to set up
the Strype library.
So first thing we'll do is import Strype at the very top so at the very top will say Can stripe equals
require strike like so.
Now before we do anything else inside here I want to point you back to the documentation very quick.
Notice the kind of peculiar peculiar signature here.
When we require in the Strype library we require in Strype And then as a second call.
So second set of parentheses we pass in our secret key.
So the secret key that we had already signed up for from the Strype API.
And remember we've already set this API key up across our project.
So we're going to not only require the thing in but also pass in the key with a second set of parentheses.
Now remember how we get access to our keys inside of the server side project.
We have to require that config keys don't J.S. file and then we can freely make use of all the different
keys inside of there inside of this billing route's file.
So right above the stripe require statement.
We'll say Konst keys is require that we're going to go up one directory into config and get the keys
file.
So of one config Keets.
OK so now we've got the keys object.
Remember this has the correct or the appropriate set of keys no matter whether we are in the development
or production environments.
Now as a reminder inside of these files either the dev or Prod. we had named the secret key specifically
Strype secret key.
So we need to make sure we pull in the key using the correct property name.
So back inside of billing routes we're going to put on the second set of parentheses after the strike
requires statement and we'll say Ki's dot stripe secret key strike secret key.
OK.
So that looks good.
OK.
So we've now got access to the key.
Now we are going to move into our post request handler where we all put together the actual logic to
take the credit card bill it and do all that kind of stuff.
Now before we do we're going to run into just a little bit of an issue just a little bit of an issue.
So as a reminder inside of our client side application we are making a post request to our back and
server containing all the different credit card information or everything that was inside that token.
So if you open up the network request tab go through the payment thing and then click on the request.
You can then scroll down to the very bottom and inside of here you should see a request payload and
that will contain everything about the credit card that was just entered by the user.
So clearly this request object contains the actual token.
Like here's the ID.
This is the token that we actually want to use to create the actual charge.
Now the problem here and this is something that is a real gotcha with express a huge gotcha when you
make post requests to use Express Server Express does not by default parse the request payload.
So we have to install another little module on top of express that instructors express that whenever
someone sends in a post request it should take the request body parse it and then make it available
to everything inside of our application.
So little bit of a gotcha just to give you a little bit a little bit of a better idea of what this thing
is going to do for us what this extra little step is going to do.
Let's go back over to NPM G-S dotcom and we'll look up the documentation for it.
Just so you can see that I'm not like pulling your leg here.
OK.
So over NPM J us we're going to search for the module body parser and then here it is body parsing middleware
and yeah that's pretty much it parse incoming request bodies.
So that's the actual request body is available inside of our handlers as the rec body property.
So after we wire up this module right here all that token information will be available on the rack
body property.
And remember Reyk right here is talking about this rock object inside of our handler that represents
the incoming request.
I know that all these little configuration odds and ends on Express get a little bit tiring after a
while but its one of those things where you just kind to you got to memorize it you got to just keep
it in your head and whenever you work on your own project youve got to remember to bring the stuff in.
Ok so lets install this library all wired up we'll take a quick break and then try it out in the next
section.
So inside my terminal my server is still dead over here.
I'm inside the server directory and we will install dash dash save body parser OK that's a pretty quick
little thing so we'll just flip right back to our code editor right away and we're going to open up
our index G-S file we will require in that module and they'll make use of it.
So just somewhere up here among all of our other require statements will say Konst body parser is require
body Dasch parser like so.
So here's their quires statement.
And then if you read carefully in the documentation you may have noticed that it said that this was
an express middleware remember that express middleware are wired up to express by the app dot use call.
I remember that was something that we covered during an optional section a while ago.
So these are all middleware that somehow operate on the incoming requests before they are sent off to
our requests handlers.
So we'll put this new parser middleware right above all the other ones the very top will say app don't
use and to wire up the body parts or middleware.
We're going to say body parser Daut Jaison and we'll make sure that we put an opening and closing parentheses
right after Jaison like so.
OK so that's pretty much it.
Now any time a post request or a put request or a patch request or anything else that has a request
a body comes into our application this middleware right here will parse the body and then assign it
to the rec dot body property of the incoming request object.
So with that in mind let's take a break.
We're going to come back in the next section.
We're going to out in a little bit of test code to our building redstart J.S. file and just make sure
that the request object or the token the credit card token is being communicated to our request helper
successfully.
So I'll catch you in just a minute.