So I'm sure you've noticed this already, but this payment succeeded function, this method is not going
to be able to be used for both the virtual terminal and for buying products simply because we have this
line right here on line.
Forty three in payment succeeded.
And that's widget ID because of course we don't have a widget ID on the virtual terminal that's just
used for making a credit card transaction of pretty much any sort.
So that means we're probably going to have to have two handlers, one for virtual terminal and one for
buying a widget.
Now it seems to me that all of this everything from pretty much well past form all the way down to about
here where we get the expiry year.
We're going to have to do that twice.
And I'm not a big fan of code duplication.
So let's simplify this.
By making a new function that does all this stuff for us and then we can reuse it as many times as we
need to.
So I'm going to come up here just before the payment succeeded function and I'm going to create a new
type because I think it's simpler and it will certainly make calling things a lot simpler if we take
all of this kind of information, first name, last name, email and so on and so forth, and put that
at its own type and then we can pass a single variable.
So I'm going to call that type transaction data and it will be a struggle.
And we'll give it some members first name, which will be a string last name to also be a string email
string and then payment intent
I.D. And I'm going to call it payment intented because that's what it is.
It's not the true payment content itself.
That's also a street payment method ID which will which will also be a string then the amount payment
amount and that will be in it.
We're also going to be the payment currency,
which will be a string the last four digits, which will also be a string the expiry month, which is
the expiry year which will be attached and the bank return to.
Which will be a strength.
OK, so that'll be our type no, we'll create a function that I can call from both of the two handlers
that will handle actually charging a credit card or handling the posted data after we charge a credit
card.
So it'll be a function with the receiver of application pointed to application.
And I'll call this get transaction data.
And it's going to take only one parameter.
And that will be our request because that's all I really need, which is a pointer to a request.
And it will return transaction data, the type we just created and potentially an error.
OK, so scroll this up and give ourselves some room.
First thing I'll do is create an empty variable var and I'll call it taxand data for transaction data
of type transaction data.
Then I'll pass the form and I can just actually want to split the screen here and scroll down a bit
and get this information so I can copy and paste just to save some typing.
So we'll pass the form the request and we're not going to return.
Nothing will return to some data and the error which is error.
And after that, I'll actually grab the information now I can grab the posted data right here, all
of this, but not including would you do?
I'm going to cut it out of that
and we'll fix this function up after the fact.
So I get my data.
And, of course, you might want to put some validation in here, maybe have another function that validate
your data, returns a boolean and don't go any further and get transaction data if you don't have this
necessary information.
But I'll leave that as an exercise for you because it's not very difficult.
OK, now I'm also going to at this point, since I have the amount, we're in this function right there,
this is where I will actually turn the amount from a or the payment amount from a string to an end,
because that's what I need it to be.
So I might as well do that right now.
Then I can grab all of this information card right down to here and cut it out.
So this is information that I'm going to be doing every time I process a transaction or create my card
variable, get my payment in, get my payment method and then get the information from the payment method.
So I'll cut that out of there and I'll paste that right in here and I'll have to fix my returns.
So I'll grab the return from this error.
Check up here and put it down here and down here.
And now that I have all this information.
I can create or populate my tax and data variable and declare it at the top of this function, and data
will now be equal to transaction data and I'll populate all of its fields.
First name will be first name.
Last name will be last name from the forum post.
Scroll up a bit and give us some room.
Email will be email.
Payment intented will be our payment content right there.
Payment methods will be our payment method.
The payment amount will be amount, which is a it.
Our payment currency will be our payment currency.
The last four digits will be last for.
Expiry month will be cast to an end expiry month because remember, that comes as you sixty four expiry
year will be expiry year.
And the bank return code, I'll have to find that.
Right there, PPI charges, data index, zero I.D. So now we have that and all we have to do at this
point is safe return to sanity or data and know, OK, so now we have this function that we can use
wherever we need to.
So let's give it a comment.
Gets transaction data from post and strength.
OK, so we have lots of errors to fix now, but none of that is terribly difficult.
So we'll just scroll down.
The payment succeeded.
And all we really need to do is we've got to get our widget.
We'll use this one for payments.
Payment succeeded will be the handler for buying a widget.
So we'll leave the widget ID bit here on line one of five.
But all we need to do now is create a variable, which I'll call text and data and potentially an error.
And those are assigned the value of from app get transaction data.
And we handed our request, which is ah, and we'll check for an error.
So I'll just copy and paste this error code right here.
And at this point, it's just a matter of fixing up the errors and deleting things we don't need.
So I'll close this so we have more room and this becomes text and data, first name and text and data,
last name and text and data, email.
Now, obviously, you could just pass transaction data, right, to save customer and simplify this
and pass one argument.
But this is simple enough and we still need our customer I.D., but our transaction becomes we don't
need this convert to an amount anymore.
This becomes text and data data about.
Payment amount, and this becomes tax and data payment currency.
And this is tax and data the last for.
And this becomes tax and data expiry month
and expiry year.
And the bank return code.
Payment intented, I don't know what they're.
There we go
to some data, the data that
came at intented.
Antioxidant data, the payment methodology that transactionally stays, are stateside, stays as to
so now we have our transaction and down here when we create an order, this becomes tax and data payment
amount.
And what changes at this point is we're not going to be passing a map string interface to the session
anymore.
Instead, we'll just past hour or text and data so all of this goes away.
And a receipt becomes tax and data, which means we need to go register that type in may not go, so
I'll just change this from a map string interface to transaction data.
And of course, since we're registering it, we have to give it the curly brackets that's in Mingo.
So back to Handler's.
Is there anything else we need to do?
There it is.
So we'll get rid of the word should here because we're now writing it to the session.
So that's pretty good.
Now, the last thing will have to change, of course, is back in our template where we have a receipt
page.
We can't read things from index data because that doesn't exist anymore.
So instead, all we do to make it simple by declaring a variable.
Which I'll call Dollar Sign Texan, and that's a sign the value of index is not data I'll look for.
I will have to put that in the data variable shortly.
But let's just change this first so this becomes a dollar sign.
And I called it payment intent.
I guess that's that's what our transaction data type.
And this is dollar sign.
First name.
Dollar sign and last name.
Dollar sign to send email with capitally.
Payment method becomes dollar sign, scientism payment method ID.
And this is dollar sign, tax and payment amount unless format that has currency since we wrote that
function a while ago, format, currency.
That's the function that's available to our templates that we defined quite some time ago.
Now, the currency dollar sign Texan.
Payment currency.
Dollar sign and last for.
All our sovereignty and not thank return.
Code.
Dollar sign and dot expiry month and expiring expiry month.
And dollar signs and expiry year.
OK, back to our handlers to get the correct type into what we're passing to this template, and that,
of course, is the receipt page.
So this gets changed.
We're still looking for a receipt, but we're not converting it or casting it to a map string interface
or casting it to a transaction data.
And we'll just call this Texan now, we'll create our data type, which is a map data will be assigned
a value of make a map string interface.
And we simply put that in the map, put it on the map.
So data and we're looking for direction equals accept.
And that should do it unless we have some typos.
So let's start or stop our applications or I'll say it's not running now, so make start.
There goes a comp. That's a good sign.
Let's go back to our Web browser, reload this page.
Make sure it's current and we'll make this one.
Dick Grayson, Dick at Grayson, dot com, Dick Grayson and our standard credit card number
zero seven, say twenty eight and some CDC.
And if we get everything right, this should still work.
And it does, and the payment amount is formatted properly and everything looks great, so the next
step, of course, is to go back to our handlers and write one that's specific for virtual terminal,
because the one we have here, which is called Peyman, succeeded.
That won't work.
We don't have a widget ID and actually don't have first name and last name.
So we'll have to make that change as well.
And those are pretty straightforward changes and we'll take care of them in the next lecture.