So this time around, we want to set up our back end to give us a payment intent, and I'll be talking
about that in some detail.
But suffice it to say that the payment intent is something you get from Stripe and it changes during
the life cycle of a transaction.
And it's what we're going to use to charge a credit card.
So before we do that, let's just go back and verify that our JavaScript we set up is entirely correct.
So go back to my ID and right here in the Valley function, what I'm going to do is create another function
outside of this and I want to call it func should hide payment buttons or just hide Paiva like that,
OK?
And that's a simple JavaScript function and all it's going to do is go to pay button.
That's a constant we set up a little while ago, as you can see it right there in line sixty two class
list, and then we'll add the nun to that to hide the pay button so they don't click on it twice.
The user doesn't accidentally charge a credit card twice.
And we'll also go processing the class list dot remove DNA and that will show a little processing center.
So let's make sure that works.
So right here in the valve function, I'll just call that paper, paper, paper like that and I'll say
this.
Make sure my application is running.
Yes, it just rebuilt.
That's good.
So let's go back to our Web browser, reload the page and first of all, make sure the validation still
works.
It does.
So I'll put some in some values in here.
One me the add here dot com and charge Kirk and this payment charge card should be hidden.
We should see a little spinner right here and we do.
So that works great.
Perfect.
So let's go back to our code.
And now what I want to do is to set up code that will get a payment intent from strike.
And of course, that means we need to add something to our go file.
So back to our Web browser.
This is the official stripe package and it's on GitHub.
And there's a link to this in the course resources for this lecture.
And there's a lot of coding there, as you can see.
So we're going to scroll down to the read.
And find out how to install this and there it is right there.
So as I record this lecture, this is version 72 of Stripe's Striped DeCicco.
And if you want to follow along.
Exactly.
Make sure that even if it's a later number on the GitHub repository, say it's seventy three or seventy
four, use seventy two.
And that way it'll still work.
And it will absolutely make things simpler for you as well as you follow along just in case there's
a breaking change.
So I'll go back to Visual Studio Code.
I've copied that install link.
Let's get back there.
Opened my terminal, I'll stop my error, cleared the screen and just paste that in and that should
go and get.
And it does.
OK, so now it's installed so we can actually use it.
So I'll close my terminal window and so far all we've been working on is the front end and now I want
to work on the back end.
So we're actually going to have to binaries running at the same time, one that handles displaying Web
pages to the user and one that we're going to call from those Web pages using JavaScript.
So an API, in other words.
So what I'm going to do first is go to my project folder in my explorer and create a new folder in the
internal folder.
So I'll create a new folder in here in Internal and I'm going to call it card and color, whatever I
want, but it actually works.
And inside of Khurd will create a new file called KO'd that would go.
And this is where I'm going to put all of my calls to Stripe.
OK, so package decoration, package card.
And the very first thing I'll do is I'll define a card type, OK?
And this card type is going to hold some information that's necessary for us to talk to.
So type I'll call it card with capital C struct and it's going to have a secret which will be our straight
secret, which is a string.
It will have our key, which we may not use, but it's not going to have it in their string.
That's our publishable key.
And what currency are we charging?
And that also is a string.
So I have that set up format.
The next thing I want to do is to store it, create another type and that's for transaction.
And we're going to start with a very simple one, but it will become more complex as time goes on.
So I'll create another type and that's transaction.
Transactions built right into struct and for fields, it has transaction status, which will be an integer.
And I'm also going to put in there the amount of the transaction, which will also be an it, not a
float.
We're not going to use floats to do our math with transactions, as it suggests in the stripe documentation.
And as you probably know, in any case, it's always easier to do integer math when it comes with transactions.
You don't wind up with floating point errors, but you'll see that's what currency we're using, which
is a string.
I also want to get the last for the last four digits of whatever credit card transaction went through.
That's always useful information to have, not the entire credit card.
I don't ever want that information.
I'm going to send it off to Stripe, never stored anywhere in my application, never have any access
to it.
OK, so that will be a string and I'll have the bank return code, whatever we get back from straight.
Something I can use.
OK, string.
So let's format that.
Now we have two types so let's create a function that will actually charge a credit card.
OK, so I'm going to call this func and it's going to have the receiver of a pointer to the card type
and I'm going to call this create payment intent because that's what I'm actually doing.
OK, and that payment intent is going to have two parameters.
First of all, what currency are we using, which is a string?
And second, what's the amount you want to charge, which is in it?
And it's going to return three possible things.
The first one is the information I want for the transaction.
And it's a pointer or two from the stripe, the package.
And they're important for me, which is good.
It's a payment intent, that's the first return value.
The second potential return value is an error, which will be nil if everything went properly.
And we're also going to return a string.
So that's my function definition.
Now I'm going to have to have access to my secret, OK, my secret, keep my stripes secret.
So I'm going to call from the strike package a key and that's going to be equal to from the receiver
that this function has c dot secret.
OK, so now I have access to my secret key.
And of course, when I call this package, I'm going to have to populate that construct with the secret
key.
But we'll get to that shortly.
Now, I want to create a payment intent.
So we're going to create a payment intent and we're going to do that by creating a variable which we
call parameters, just the parameters we're going to be passing off to Stripe and we're going to use
a reference to a stripe payment intent Prem's.
Intent programs right there and we're going to populate its fields as follows the amount.
Well, I got that as an input.
OK, but I actually need to convert it to a stripe in 60 for which if you read the straight documentation,
it says, that's what I need to have.
Fortunately, we have a utility in the straight package that does it for us.
Stripe dot in sixty four and then I'm going to take the entire gut and cast that to an end to sixty
four and that's just amount.
So that's the first field, the second field as you might have guessed by by now, this currency and
currency.
Again it's just a string, but I need to cast it to a stripe string and again Stripe gives us a package
to do that or utility you string and I just paschen currency.
OK, so I have my parameters at this point.
If I wanted to, I can actually add some metadata.
I could put something like parameters dot add metadata right there and I can just pass in something
as simple as whatever or key, a key and then whatever I want it to be, which I'll make value.
Now we're not going to do that.
But just so you're aware, if you want to add information to this transaction, that's how you do it.
So I'll comment on it.
Just so you have a reference to what we want to do now is actually get our payment intent.
So pie for payment, intent and error.
We're going to check for an error or assign the value of payment intent and that will import imported
from straight payment intent, not new and hopefully up in.
Yes, I have my payment intent.
Important.
Good.
So we want a new payment intent and we're going to pass it the parameters we just gave.
So that gives me a payment intent possibly.
Let's check for an error.
If error is not equal to nil,
then I'll create a variable message which will just be a string and I'll say if stripe error.
OK, stripey of being a variable name, I've just chosen an error, and then we're going to get our
stripe error and that's straight out of the straight package.
OK, and then OK, so if OK is false, then we're going to get our message.
MSJ equals try that again.
MSJ equals whatever the error messages.
So straight error code.
Now let's see if that returns the right type.
OK, now that Erica, that's not very useful, actually, I want to string, so what I'm going to do
is come down here and create another function function and I'll call it third error message.
And it's going to take as a parameter code and the code will be a straight error code.
And that's what we're actually getting up here, OK?
And that's going to return what I want, which is a string.
And I'll just put a couple in for right now.
So again, I'll declare a variable var message and MSG and that's going to be equal to nothing to string.
And then I'll just switch on the code that I can switch.
So code.
And the first thing would be case straight.
The error code card declined error code an hour.
That's why it's not working.
Error code.
KO'd declined.
And get this where it belongs, and I will help, OK, if the area code is cleared, declined, then
I'll say the message is equal to your card was declined.
Now, there are more of these, but there's no point in you watching me type them out.
So what I'll do here is just say default.
And I'll make the message the same thing, so I'll copy this.
And pasted in there and just so you see how this works.
Let me put one more in case straight, not the error code word expired card, but it expired card there.
And the message will be your card.
Is expired.
OK, now there are more of these, but what we're going to do here is just have those just to get this
up and running MSJ return that.
OK, now back up here.
Our message is going to be equal to that.
And we'll change this to cut that out of there and call.
Instead.
Card error message and we'll pass it that parameter, and then down here we'll return the necessary
things for this function return nil because there is no payment intent.
Our error and the message.
Otherwise, we got something useful back.
We'll return the API for payment intent nil, because there is no error.
I don't empty string for the message.
OK, so that is how we're going to get a payment intent.
Now, create payment.
Intent is perhaps what's wrong with this.
Error should be returned as the last argument.
You're right.
I mean, move that will remove that and put this here and then down here will change this to return
the error last.
And we'll get rid of this string here and put it right here, OK?
That's semantically correct.
That's better idiomatic.
Go, go code.
So we have this payment intent function.
That's not really a meaningful name.
I mean, it is if I'm working with Stripe, but let's make an alias to this.
Let's create right above it.
Another function and it will be func.
See KO'd again and I'll call this one church, which is a much more meaningful church.
So currency will be, string amount will be, and it will return a pointer to a stripe payment intent
and add a string and potentially there.
And all we'll do is just call return, don't create payment intent and we'll pass that our currency
and our amount there.
Now that's a little nicer and that's absolutely not necessary.
You can call create payment intent directly, but charge seems to me to be a more meaningful function
name.
I use create criminal intent here because I'm using straight.
But at some point down the road I might want to modify my code base to work with, say, Stripe and
PayPal and maybe a Canadian bank like Moneris.
And at that point I could use the repository pattern and have charge refund, partial refund, whatever
those may be.
And those would be meaningful names regardless as to the payment date we were using.
Of course, I'm not going to do that in this course, but it's the sort of thing you may want to keep
in mind as you build any payments solution and go.
So now I have this built up.
OK, so in the next lecture we'll actually hook it up to our front end.
We'll build the will, build the back end, will build the front end.
We'll get both running and we'll try running a test transaction.
And that might take a couple of lectures, but we'll get started in the next one.