So this time around, we want to actually submit our form and I'm looking at the terminal page go HTML
file right now and the action for that form is payment succeeded.
So we actually want to submit this form with the idea of charge, underscore form, handle that on our
front end and generate a receipt page and we'll be done.
We'll be able to charge credit cards using our virtual terminal.
So let's get started with that.
So the first thing I'll do is open my routes file and I will create a new route start post and it will
be what I call it, payment dash succeeded, payment Dash succeeded, and that will go to a handler
that doesn't exist yet.
I'll just call it a quote payment succeeded.
You can call it a receipt or whatever you want, but I want to call mine payment succeeded.
OK, so let's go create that handler.
So I've got a handlers and I'll create a new handler function, have a pointer to application and it's
called payment succeeded.
And it takes the two things that every handler does a response writer
and a pointer to a request.
OK, so now that I've created that back in routes, the error should be gone.
And it is.
So let's write this handler now, because this is a post.
The first thing I have to do is handle the pass form well, check for error and call person form.
And if there's an error, then I'll just log it after error log, print, line the error and I'll return,
which isn't very polite, but it's sufficient for our purposes right now.
And I will read the posted data.
So cardholder will be a variable I declare is assigned the value of our farmgate and we're looking for
cardholder underscored name.
I better make sure that's actually what I called it.
So let's go back to the terminal page and make sure it's cardholder name and it is good.
OK, so that's the first one.
I want to get the payment intent, so I'll just duplicate this and call this payment intent.
And the parameter is named PT. To underscore intent, I'll duplicate that, and now I want to get the
payment method.
Which is payment method,
and I'll duplicate that payment amount, which is payment amount.
And the currency payment currency.
We'll be payment currency.
Now, let's create a data variable data is assigned the value of make a map string and I'll just put
it in your face because this is what I'm going to be passing to my template and I'll take advantage
of the type in our template data called data.
Interface.
OK, and let's add the information data cardholder
is equal to cardholder, and I know I could do this all online, but I'm trying to make it as readable
as possible.
Data email.
She didn't get so I have to grab that, too.
That will be equal to email and up here I have to grab that.
So I'll call the email and email, which is what I called it on the form.
And data payment intent, which I'll just call part, is payment intent.
And duplicate the payment method, which I'll call PM because I'm lazy payment method and duplicate
that pay for payment amount
and PC for payment Cursi currency.
OK, so I have that now.
I'm going to render a template and I'll use the same syntax I did for other handler.
If error is not if error is assigned the value of apt render template and I want to pass it the response
rate or the request.
A template that doesn't exist yet.
But we will in a minute I'll call it succeeded and we'll create a template data and we'll populate that
with the one thing that we need in there, which is data data.
And then we'll put a semicolon and say if error is not equal to nil and we'll do the same thing I did
before after analog print, one error.
And of course, you probably want to do something else.
But for our purposes, we're focusing on stripe.
This is quite sufficient.
So now we actually need to go create that template.
So I'll go to my command web templates folder and I'll create a new file in there called
This You Page Go HTML.
And this will be a very simple receipt, I'm not going to make any effort to make a pretty.
So we'll put up here template, base letter spell template, right.
And hand it data.
And in the first block, which I call title, all define title.
And just override the default value and put in payment succeeded and the.
And then we'll have our content block to find content.
And it.
And in here, I'll just say to say class equals MTW five to push it down a bit, the payments exceed.
And of course, you could put whatever you want in here, this is just a receipt, so I'll put in H.R.
and just to make it a little cleaner picture and I'll simply say I'll put the information that we've
passed here.
So you obviously don't want to put payment intent in a final receipt, but we're just passing data.
So index data and we're looking for.
That's what I called it.
And then I'll duplicate this a few times and I'll put payment or cardholder name cardholders enough
that I call that cardholder.
And then we had their email.
Which I called the email.
And then we have the payment method.
Which I called him, not great naming there, but anyway, it's showing what how we pass data to this
template and what you might do for a receipt that I'm sure you can figure this out amount.
I'm going to print it out exactly in the way that we received it, which is pay for payment amount.
So it'll show what's being passed to our handler.
And of course, that won't be a decimal.
No currency
index, the PC for currency, and that should be enough.
So let's make sure everything compiled it did.
Let's go back to our Web browser.
And reload this just to make sure it's current and I'll put a transaction through this time, I'll make
it four twenty five dollars and thirty four cents.
And the cardholder name is Jack Jones.
And Jack's email is Jack at Jones dot com.
And we'll make this a transaction that passes by using the tax credit card number that always passes
and zero three twenty seven with four or five six.
So what should happen now is we should very briefly see that green success message and then this form
will get submitted.
Actually, no, it won't, because they need to go back to my template and submit form.
I'm glad I thought of that now.
So terminal page means I need to re-enter all that information, but that's OK.
So in the statement would submit the form becomes a document get element, but ID and I'm going to get
the idea of charge, underscore form, not submit.
They're all right there.
Recompile.
There it is, a recompile.
So let's go back to our browser, reload this page one page one more time and try it again.
Twenty four dollars and fifty six cents.
This time Jack is the name Jack Jones dot com and our test credit card number zero one twenty seven
for four for now.
We should see that little green thing flash at the top of the page and we should be taken to our receipt
page.
And we did.
There it is now, you obviously could make that as good as you want.
I see.
I missed email here.
What I miss email back here was that I call it on the form, probably didn't call it email.
So card hold email type email and name is card holder email.
So I can I can just change this to email and the next time I run that it'll work just fine.
OK, apart from that one typographical error, which I seem to be making a lot of these days, that
should all work as expected.
So now we know how to charge a credit card to check for errors to handle the posted data that's sent
to our our our front end handler.
And of course, we want to do more than just generate a receipt.
We actually want to write some information to the database and we're going to do that.
But we're going to do it in the next section of the course where we actually put a product, a virtual
product or some fake product up on a Web page and allow people to buy it.
And we're also going to, in the following section, put up a subscription where people can buy something
and get charged once a month or once a week or whatever interval you decide is the appropriate one.
In other words, we're going to implement recurring payments and during those two sections of the course
will expand both our front and our back end to write information to a database, to read information
from a database and all of the things you would have to do in a typical e-commerce site.
And we'll take care of those in the next few sections.