We've now got our API key all set up inside of our cluster and we have installed the stripe library
into our payments project.
And before we open up our root handler and make use of the stripe library itself we're going to first
create a separate file that is going to be responsible for importing and instantiating a copy of the
stripe library.
Let me show you how to do that inside the SRT directory.
I'll make a new file called stripe T.S. than inside of here.
We're going to import stripe from stripe and we forward make use of this.
This is really a class right here.
And to make use of the stripe library we have to create an instance out of that class.
The entire purpose of this file is to really just create an instance of this stripe library and then
export it.
So can be used in other locations inside of our project.
So I'm going to do an export const stripe is new stripe and then to this object right here or the constructor
we're going to provide two different arguments.
The first argument is going to be our API key and you might recall we already wired that up.
It should be accessible inside our project as process EMV dot stripe underscore key as usual typescript
doesn't really believe that we have properly defined this environment variable.
We're going to tell typescript don't sweat it and put the exclamation on their then as a second argument
we're going to provide an options object inside this object.
We're going to enter an API version when you start to enter a string for the API version.
You might get some autocomplete pop up if you get autocomplete and you see a date different than mine
then go ahead and use it.
Otherwise if you do not see any autocomplete at all then use exactly what I'm using right here which
is 20 20 0 3 0 2 OK.
So we've now got a single file that's going to import stripe create an instance of this library and
make it available to the rest of our project so we can take this back over to our root handler import
the stripe instance and use it to charge a user's credit card back inside my roots directory I'll find
the new test file and at the very top I will add in m import or stripe from up one directory
All right.
Now the question is how do we actually charge users credit card using the stripe library.
Well I pulled up some documentation so we can take a look at it and get a better idea of how we actually
take some token that is going to be incoming to our right handler in charge some money with it if you
want to follow log you can navigate to the official stripe documentation at Starbucks com slash Doc
slash API once here on the left hand side we're going to scroll down a little bit and find a section
called charges the charge once again this is how we actually charge the money to a user's credit card
we want to find the section inside of here called Create a charge that's a we're trying to do right
now you can see a code example over here on the right.
So we've already made our instance of the stripe library and if we want to create a charge and Bill
a user for some amount of money we're going to call stripe charges dot create and then we'll provide
some options inside of an object there are technically only two options that are required or go into
entry and possibly a third or fourth.
The first two things that are required are in a Mount property and it's really important to note that
the amount property is not going to be in units of dollars.
Instead stripe works by the smallest currency unit as they refer to right here which in the U.S. currency
system is sense not dollars does.
We have been handling ticket prices throughout our app.
We've always been treating those ticket prices as dollars and we just need to make sure that before
we create a charge we convert that dollar value into cents which essentially means we just need to multiply
it by 100.
The second required option is a currency code.
So me I'm working with U.S. dollars.
I'm going to put it in a currency code of USD.
Now there is technically another required property even though they really market as optional is the
source property.
So this is where we are going to be charging some money from.
There are many different viable sources we can provide but one viable source to credit charge from is
a token.
And as I've said many times this token is going to be coming into our request and we can essentially
view this token as kind of a pre authorization thing.
Something that says Here is something that represents a credit card and we can bill it for some amount
of money now optionally we can also provide a description property the description property is not really
used for anything related to the charge itself.
Instead it's just a little description to go along with the charge.
It will show up on our street dashboard and possibly show it to users as well when they actually look
at their credit card building statements.
So if we wanted to we could definitely fill in a description as well but I'm not going to include it
okay.
So I thought that all in mind let's go back over to our Ed. We're going to make use of the striping
rated charge and add in a couple of different options.
I'm going to scroll down a little bit and right after we do our three checks right here I'll then do
it in a weight stripe charges dot create will then provide those three different options.
The first is currency which will be at USD.
Next up is the amount of money that we want to charge which will be the amount as order dot price.
Again that is a dollar value right there.
So we need to convert it into sense by multiplying by one hundred.
And then finally we also have to provide a source for this charge where the money is going to come from.
And that's going to be the token that is incoming into our request handler.
We've already D structured off that token property off the request body so we can just enter in token
like so and I should be it.
That should create a charge and build a user for some amount of money it's going to save this as is.
But as you can guess well we probably want to somehow test this out and make sure it works as expected.
Let's take a pause right here.
Welcome back.
Next video we're gonna figure out some way to manually test this and eventually write an automated test
around it.