So this time around, we want to finish up or at least continue working on our get payment intent handler
that will actually contact Stripe, get a payment intent and do all of the things we needed to do.
Before we do that, there's one slight change I want to make.
If you recall, a while ago we created inside the internal folder a package called KO'd.
And we're going to run into a naming conflict between the card that exists within the strike package
and the card type that we've defined ourselves.
So I'm going to fix that.
I'm going to do it.
And this is one of the disadvantages of Visual Studio Code, is it doesn't refactor packages very well,
but we can do it manually.
So in the internal package, create a new folder and just call it cards, plural.
OK, then open up the cargo, select all and copy and inside the cards folder, create a new file called
Kurtzberg.
Go and paste that in there, then go to the very top of that new file and add an S to cards and now
we can delete the entire card directory off the cards plural the card singular and this will get around
that problem we're going to run into with the name.
OK, so I'll close that cards file.
Now back inside my handler.
What I know what's going to happen.
This is going to be called not using GIs as it is right now.
It's going to be called using post and it's going to consist of the body of the request or the body
of the request that we get will actually consist of adjacent file.
And that Jason file will conform to the standard that we have in straight payload.
So let's create a variable, which I'll call our payload of type straight pallet, and then we want
to get the body of that request and decoded into our variable payload.
We can do that really easily and go error is assigned the value of from the JSON package.
We want a new decoder and we're going to be decoding our body, the request body, and we're going to
decode that into a reference to payload.
And if everything went as expected, we now have our payload variable populated with both currency and
amount.
So we'll check for an error if error is not equal to nil.
And right now I'm just going to say APTA error log, print one and I'll print out the error and then
I'll return, which is not very polite.
We should be sending back a properly formatted JSON error message, but we'll take care of that later.
OK, now if you'll recall, actually you don't have to recall.
It's right there in front of us.
The currency is a string and the amount is a string.
And I need that amount to be a numeric value and that's pretty easy.
So what I'll do is simply say amount and again, we'll check for an error is assigned the value of and
we'll use the Sterkel package A2 Alpha to integer.
How we're going to convert payload don't amount.
And again we'll check for an error and I'll just copy and paste this error handling code which will
be replaced with something better later on in any case.
So now we have the amount, so we have the currency and we have the amount.
So now we're going to take advantage of that cards dot card type that we have.
So we'll create a variable, which I'll call card and it's going to be assigned the value of from the
cards package and we want card and I might have to do the manual import number, seem to get it for
me and we'll populate its various fields.
So we need to put in the secret.
And that will come from our app, dot config, dot stripe, dot secret.
We need the key and our key comes from Aptos Config Strachey, and our currency comes from currency.
Payload currency.
There it is.
So now we have our card type and of course, that means that we actually have access to all of the necessary
methods that are on that.
So so at this point, we've created an instance of that card type and we have access to all of the methods
that are built into it.
So first thing I'm going to do is I'm going to declare a variable, which I'll call OK, and I'll assume
that's going to be true to start with.
So everything's going to be all right.
Now, I'll call that function charge in cards and get the three variables that are returns.
The first one, if you recall, is payment intent.
So I call upon the second one is a message which is just a string and the third one is an error.
And those are all going to be populated by calling card dot church.
I'll use the alias because I think it's more readable.
And that requires two variables.
The currency which comes from payload currency and the amount which we just created ourselves.
Now we're going to check for an error.
If error is not equal to true to nil, then I'm not going to just send back a principled error message
and return instead.
In this case, I'm going to say OK is equal to faults.
In other words, something went wrong because in this case it may not be an error, it could be a card
decline message.
So let's just see what we're going to do here.
If OK, if everything is OK, then we should charge the credit card.
We have our payment intent, in other words, otherwise we're going to do something else.
Now, if we've actually succeeded in our message, then we want to send some Jason back.
So let me scroll up here a little bit.
And what I'm going to send back, if the charge succeeded, is the actual payment intent, because that's
what I want on the other end.
On the front end.
OK, so all I have to do is say out an error or assign the value of Jason Mercial and I'll mercial in
debt to make it readable.
OK, pie with no prefix and some spaces to make it all format nicely.
And again, I'll find my error handling code up here.
Copy that.
Come down here and paste it and then I'll set the header.
I can copy that from down here.
Copy these two lines.
Come back up here and put it in there.
So once we call card charge, and that's the one that actually connects to Stripe for us, we're going
to get a payment in 10 back if our payment intent comes back with no error, that OK is true.
So on line 50, we check for that and a line fifty one, we create our Jason.
We want to send back and then we just set the header and send it out.
That's if things work as expected.
Now if things don't work as expected and I'm going to take all of this, cut it out of there,
paste it into here and modify it appropriately.
So first thing is our Jason response is going to be, OK, false because things didn't work as expected.
The credit card didn't get approved or whatever the case may be.
The message will be MSG, which we got back up there on line forty five when we called card charge and
the content, I'll just leave empty lot that I don't have to do that, but I'm going to just to make
it very readable that we do a commercial indent, we send it off and set our header and write it out
to the browser.
So this is at least a good start for what we want to do.
Now, there's one other thing I want to take care of here, and that is right at the very top.
If you recall when we looked at the output from the browser last time we saw, OK, and that was set
to true.
And then we saw empty values for these and empty string and empty string and the value zero.
And then a lot of cases.
In fact, in most cases, if there's no value for a particular field, you don't even want to bother
putting it in JSON.
And you can do that and go really easily.
You put a comma after what you want to call it in Jason.
And then the keyword limit.
Empty, empty what?
I'm going to copy and paste it for the next two as well, and that will make things ever so much nicer.
OK, so let's make sure this compiles, I'll say make a stop to make sure everything is stopped and
it is and make start underscore back.
Let's hope it compiles.
It does perfect.
OK, so in the next lesson, we'll go modify that route to be a post instead of aget and we'll go write
some JavaScript on the front end and see if we can get a single transaction to go through.