All right, now that we're no longer accidentally charging a thousand dollars for a ten dollar widget,
let's start implementing some changes on the front end to take advantage of the kinds of things we want
to accomplish.
And right now, I'm looking at Stripe Dash, just partial HTML, and I'm looking at the fetch statement
that calls upon payment intent and down here where the charge charges actually succeeded.
I'm going to make a slight change right here on what's line eighty five in my code.
I'm calling, I'm getting payment, underscore method, underscore types and the first index of an array
and I'm going to change that.
That's useful information.
But there's actually one piece of information that's a little more useful.
Let's change that to payment method.
And the reason I'm doing that is that if you look at our models, dot go file and you look at transaction,
I want to store the last four digits of the credit card and I can't get that from the payment content.
I actually have to get that a different way, which will be taken care of shortly.
So that's our first change in Stripe Dash.
Just partial Dudko html make that on line eighty five and my code.
Make the document get element id payment underscore method equal to result not payment intent payment
underscore method.
So that's the first change I want to make.
OK, now let's go over to our handlers.
So handlers dutko.
And what I'm going to look at is this method payment succeeded.
That's the one that we call when we submit the form after the credit card has been charged, when we
submit the form either on virtual terminal or when buying a single widget.
And right now, we're just parsing the form.
We're getting the past values and we're doing something with them.
We're just printing them on the screen.
And let's make this a little bit better.
As I said a moment ago, one of the things that I want to get from Stripe is I want the last four digits
of that credit card.
And in order to do that, I have to make some changes to our code, both the front end and the back
end.
OK, because we don't have those last four digits.
They're not available to us by default.
The other thing that might be useful to have is the expiry date of the credit card.
Under no circumstances will I ever store a credit card number, but it's useful to be able to verify
that you're looking at the right transaction by saying do the last four digits match the ones I'm looking
for?
And is the expiry date the same?
And that's enough information to verify that you have the right transaction, but not enough for someone
to actually charge that credit card should our database ever get compromised.
So let's get started doing that.
What I'm what I'm going to do, first of all, is go to cards, go.
And that's in the internal folder, the cards folder.
Then cards don't go.
I'm going to open that up and I'm going to create a couple of new methods here.
OK, so right here, just before my card error messages, I'm going to write two new functions.
The first one is a function and again, it has the receiver of a pointer to card.
So we have access to the things we need and I'll call this one get payment method.
And this is where I'm going to be able to get the last four digits of the credit card and it's expiry
date.
And that takes one argument, which I'll just call ESS because it's the string and it returns a pointer
to stripe dot payment method right there and potentially an error.
OK, so first thing we need to do because we're dealing with a credit card and dealing with Stripe is
we have to set our stripe, the key equal to see top secret, which is our stripe, non publishable
key, our secret key.
And then we just call a built in functionality available to us from the stripe package.
And I want to stored in PM, which is payment method.
I'm going to check for an error and that's a sign the value of payment method from Stripe and sometimes
for some reason Visual Studio Code can't find this.
So I might have to add it manually.
And the method is called get and it takes one argument.
I'll take two arguments.
Yes.
Which is the thing we're going to look up and not OK.
And no, it didn't import it for me.
So let's go right up at the top and let's just duplicate this line and change the last bit to payment.
Spell it right.
The payment method.
I should fix that error, so come down here, we'll check for an error if error is not equal to nil
or return nil and the error otherwise we'll return the payment method and no.
OK, let's give this a comment.
It gets the payment method by payment content ID.
And that's enough for that one.
OK, so that's my first one that will allow me to get the information I need in order to retrieve the
last four digits and the expiry date of a credit card and more things.
But that's all I'm interested in right now.
The second thing I want to do is I've been saying off and on that payment, intent changes during its
life cycle.
So when you get it initially, you have one bit of information.
When you get it later on, you might have a different bit of information.
So we occasionally we're going to need to be able to get an existing payment and not create a new one.
So I'll call this one get payment intent, which seems logical.
And it takes one argument, which I'll call it in this case, and a string, and it returns a pointer
to strike payment intent and potentially in error.
Same thing we did last time.
Was that stripped out key equal to
see the secret.
And now it's very straightforward to do this.
We're going to get a p a payment intent and potentially an error.
And those are assigned the value of payment intent
not.
And we pursued the idea that, no, we're not going to do anything with the second parameters, we'll
check for an error.
If error is not equal to nil, return nil nil and the error otherwise returned and nil, no air.
And let's give this a comment.
Actually, I want to change the name to retrieve payment intent because I think that's clearer.
Retrieve payment at 10 because we're getting an existing one.
Retrieve payment intent gets an existing.
Payment intent by ID.
OK, so those are available to us now.
So let's go back to our handlers and let's use them.
So what I'm going to do now, remember that handlers ago is on the front end and Kurds don't go is actually
available to us on the front end as well.
But we still need to instantiate the card.
We need to give the secret and the key to a card variable before we can use any methods in there.
And we're not going to do that very often in handlers for the front end.
But in this instance, I'm going to.
So let's come down here after we get all these values from our form and let's create a card, variable
card as a sign the value of from the cards package card.
And we're going to give it a secret, which it needs.
And that comes from Aptos configured stripe dot secret.
And we're also going to give it its key, which is from after config, straight key.
OK, so now we have this card variable and we have access to all of the methods associated with it.
The first thing I'm going to do is get my payment intent.
All I have right now from the form is the payment intent ID and I actually want to retrieve the existing
payment intent.
So keep an error or a sign.
The value of from my card variable will go to retrieve payment intent and will hand it payment intent,
which we got from the form.
The hidden field will check for an error and I'll just do some simple error.
Checking here.
If errors are not equal to nil, then I'll just say dot error log print line the error and I'll return
again.
Not very friendly, but sufficient for our purposes right now.
So now I have the payment intent.
I also want to get the payment in payment method, which is pretty much the same syntax, just a different
method.
PM for payment method and again error or assign the value of the card, not get payment method right
there.
And we hand it payment method, which is the new ID we put into our form in that statement.
OK, the last one was the first index of an array and we changed that to payment underscore method and
that's what we need to retrieve a payment method and again, error checking.
So I'll just copy and paste.
Now I have this pie and this PMB, the payment intent and the payment method.
What am I going to do with them?
Let's start with the payment method.
I'll create a new variable called Last Four, which will be the last four digits of the credit card
that's assigned the value of PMW card.
What card was used for this particular transaction?
And last for and I also want the expiry month I'm going to grab that expiry month is a sign the value
of PMed card, dot expiry month, month and expiry year, not surprisingly, is a sign the value of
PMI card dot expiry year expire.
Now I have those.
That means I can actually put them in the data I'm passing to my receipt or duplicate this three times
one to three and I'll call the first one last underscore for the next one expiry month and last four
is going to be able to last for and this will be equal to expiry month and then expire,
which is expiry year.
And also add one more.
And this is why I retrieve the payment intent up here on line thirty nine.
And what I want to put in here is the bank return her bank, underscore, return underscore code, not
Kord code.
And that comes from the payment intent.
We just retrieved the charges, the data, the first index of the array in that data, the ID, and
that gives me something I can use as a bank return code.
Now that's not necessarily information I'll ever show to the end user, but it's information that might
be useful for me at some point, perhaps in a dispute or whatever the case may be.
But in any case, I'm getting it back and I'm going to save it.
So now I have all this information.
Let's display it and that'll be enough for this lecture.
So let's go over to the succeeded page go HTML.
And here I'll duplicate this one, two, three, four times and we're going to go with, say, last
for just the last four digits.
I called last underscore four and say bank return code.
Which is like underscore, return, underscore code, and I'll actually do the data on one line, just
a little cleaner expiry date, which would be expiry underscore month slash
index data.
And we're looking for expiry for a year and close by double curly brackets.
OK, so let's stop our application of it's running mixed up and I'll start it like start.
Everything is running.
Let's see if we can get this to work.
So I'll go back here and I'll go to products and I'll try buying one widget.
So this is from, let's say, Sam Smith, Sam at Sam Dotcom, and his credit card is the standard for
to the expiry of 04, say, twenty nine and some some Coke.
OK, let's see what we get for a receipt page and see if we got everything right.
And there it is, so the expiry date, we have that information right here, this is the bank return
code generated by Stripe, which might be useful for us in the future.
We have the last four digits and everything else remains the same except for the payment method.
That's a new kind of payment method, and that's one that we use to retrieve the last four digits and
the expiry date.
OK, that's enough for this time around.
We'll pick it up in the next lecture.