Awesome.
We have a law that says your order is complete and technically it is halfway complete.
I guess because we didn't set up the order in the API but of course we didn't complete the transaction.
This is what we're going to do in this video where instead of working in the front end we're gonna head
over to our back end and this would be the case where for sure you need to have your back in the strappy
application open in your text better.
So we're gonna head over and I'm gonna grab my strappy project and then I'm gonna stop the server.
Remember that was control C and we're going to start by installing the stripe package.
So first let's type NPM dash dash save or I guess first then there's dash save and we're looking for
the stripe package.
Let's around the command and then once we do that we're gonna head over to API then.
Product or I'm sorry order my apologies or not the product then we'll look for the controllers and then
we have order.
J Yes again we look for the API folder then within the API folder we have the order folder then we look
for controllers and then or J us that's where we are going to write our code.
Please make sure that you are actually located within that file because if you're not.
Then again it's not gonna make sense.
We have installed our package.
And let me clear everything.
And again I'm gonna start up the dev server so overrun NPM run develop.
And first we're going to import strike.
So since we have installed the package I'm going to close the sidebar I guess in this case I maybe can
zoom in a bit more so we're going to create a new variable.
And remember since it is a node project of course our imports are going to be bit different.
We're gonna go with a require then we're looking for the stripe and then we would want to right away
invoke it with our secret key.
So I'm gonna head over to my dashboard and I'm gonna look for again my keys.
In this case I'm looking for the secret.
Now it might ask me to authenticate which we can do.
And then once we do that then of course we're gonna have access to Aki please use your key.
By the time you're going to be watching this I'm gonna use already a different key.
So these keys are gonna be removed and I'm going to copy the key.
We're gonna head over to our back end and then as a string we're gonna pass in Aki.
Awesome.
We have our setup then we have our module exports where we would like to export a function and the function
is going to be create and it is going to be a synchronous function.
So we're gonna write a sync and then as a parameter we're getting a context.
So why do we need a context.
Well let's think about it.
If we had all back to our front end would passing in our values correct.
We passing the name total items as well as stripe token I.D..
So wouldn't it make sense that we would be able to access them somehow in our backend and also the same
goes for user talking.
So this is exactly what we're gonna do with the context we're gonna have our context parameter and then
within the function body we're just going to access the items.
So we're gonna go for the same ones.
So name total then we would want items then stripe token AI D.
And that is going to be coming from context then a request request and then body.
Now I'm not gonna console log because my way each and every time I need to stop and start a server.
So just understand.
Again we have context parameter and then whatever items we pass then.
So in our case name total items and stripe token I.D. we can access it by looking at context request
body and we also would want the user correct.
So we're going to go with context or I'm sorry.
I'm gonna go with variable I.D. so I'm going to the structure I'll D I.D. from context state and user
not where I have my user I.D. and first before we set up our order because eventually we're gonna set
up our order we're gonna communicate with stripe and we're gonna make sure that we set up our charge
so I'm gonna set it up as a variable by the name of charge it is going to be a synchronous so I can
just add a weight so I'm gonna wait once the promise settles and we're gonna go with stripe charges
of course that is coming from my variable because I instantiated the stripe so I have stripe charges
and then we have a method by the name of create within the method we passing the object and the object
is looking for following properties we're looking for the amount however for the amount we need to multiply
this by hundred so we're gonna go with total whatever our total is and multiplied by hundred knots just
what stripe requires then we also have a currency Currency property and in here we would want to pass
the USD so you say dollars and we also have a source of Stripe token I.D. So we have a source property
where we have stripe token I.D. Why are we using that value here.
Because of course that is our token.
So like I said from the front end we pass our strike Tarquin I.D. to our back end and now within the
back end using the secretly now essentially we are making our transaction and we also have a description.
So I'm gonna add a comma and then within the description property I'm just gonna passing some kind of
string value I'm going to say order and then let's get let's say today's date.
So new date new date.
Let's invoke it.
And then also we're going to I don't know show the user name.
So by.
And then again we can access the variable since I'm using temporary literal so context state user and
then we have a user name.
So that of course is gonna be my user name.
And if there's gonna be an error we're not going to be able to in fact set up our order on our back
end.
However if everything goes correct then we're gonna create a new order.
So I'm going to say here const order and then since this returns a promise I'm just gonna set up a weight
that I need.
Type strappy and services then order and then we have a create method and then within the create method.
Now we're passing the rest of the fields where we have a name Doro items and then we also had a user.
So we need to set up our user.
And now we're going to set our user equal to I.D..
Remember we do structured it from context.
Site user.
We got our I.D..
So now we're passing it when we're creating the order.
And lastly once we have set up our awesome which is going to return our order.
So we're just gonna type return water.
We're going to serve it and we're gonna head over to our front end to make a proper charge.
So now this is gonna be complete order.
All right then let's say we're gonna add some items here.
We're gonna go again with this one maybe this one added to the cart some more amount.
Then again we're gonna roll back the products and let's select this guy and let's set on all three of
them.
And now we're gonna head over to checkout I'm still the same user so I'm just gonna type in some random
a bigger lure and then let's add the proper values.
So we have card values then we have some kind of month of course then card security code as well as
the zip code.
So let's go with the zip code and let's submit the order.
And just to show this is how our error is going to look like.
And like I mentioned previously if there is going to go something wrong with our stripe charge we're
not gonna create a new order.
So there was an error with our stripe charge and we did not create the error and just show you if we're
gonna head over to our orders you're going to see that we still have this one order where we didn't
have the proper user.
So if something is gonna go with your stripe orders if something is gonna go bananas then you're not
going to create any more.
And now let's fix it.
So we're gonna head over back to our backend and we just need to add math around.
So we always always just need to make sure that we have the integer.
So we're gonna go with the math around and then let's pass in our total multiplied by hundred.
Like so let's save it.
And now let's test it again.
So now we saw how our error is gonna look like.
And again since we're not wiping out the values or anything like that I can just close the alert and
let's try it again.
So let's say we're gonna try to submit the order and in this case I have my order is complete.
So first of all alert is working then we're gonna head over not to our backend but in fact our dashboard
lets refresh.
Now we're gonna have some random big or so I can see already that again my order has been created and
that's a good sign.
And then in this case not only I have the card items I also have the user.
So this is the correct user that created the charge and of course lastly we can just head over to our
dashboard to our stripe dashboard and just see whether we created the charge.
And I can see that I have created some charges already today.
Then if I would want to have a specific charge I can just head over to the balance I have let's say
three payments and then I look for my last one.
So of course this would be the amount and you can see that it was created by John so successfully we're
done setting up our payments using stripe.