So we've managed to subscribe a user to a plan, and that works really well, but of course, we need
to save some information locally.
And right now I'm looking at Brons Dash planned on page HTML.
And of course, what we want to do is we want to save the transaction locally.
We want to save the customer locally, and we want to save the order locally.
So in order to do that, we're going to do a lot of work here in the strip payment method handler.
And this form is actually only going to communicate with the back end.
We're not actually going to post to the front end and do a redirect and all that will handle everything
from the back end.
As I suggested, we'd be doing a few lectures ago.
So right here in function strip payment method handler, we check for an error.
If we get an error, we display it and that's fine.
And we also create a customer and subscribed to plan and that's fine.
And then we post to API, create customer and subscribe to plan.
Now, since we want to save a transaction and we want to save an order and create a customer and all
that sort of thing, we actually need more information in the payload that we're sending to the back
end.
So to do that, of course, we're going to have to go back to our handlers API and add some information
up here in straight payload.
So let's add it in here first and then we'll keep going on the front end.
So we have currency and amount payment method email.
We don't have card brand.
We can actually put that in here if we want to card brand string and in JSON we'll call that card grant,
which will be Visa or American Express or whatever.
And we have lost four, but we also want expiry month and expiry year.
So let's put that in there.
Expiry month, which will be a string for right now.
And in JSON, we'll call that expiry month.
And the reason I'm putting in a string is because that's how we're going to receive it from the Jerusalem
Post.
And we want experience all duplicate that and change this to expiry year.
And change this to Xperia.
So last foreign plan, we also want the product.
What are we selling?
And of course, that's the idea of the entry in the widgets table.
So we'll call that product and we'll make that a string for now as well, because that's how we're going
to receive it.
And we want the customers first name, which will be a string.
And we want their last name.
So let's format this to come back and make some more changes here shortly, but let's go back to our
bronze dash plan page.
So let's see what we can put in here.
It's all going to go in our payload.
So we want the product ID.
And what do we call that the top here?
I believe it is.
So we have an input type equals hidden name equals product I.D. We better give that an I.D., which
I'll call ID equals product I.D..
OK, so we have an I.V. now so we can grab that value.
So let's go back down to our payload right here.
So I will put in product ID and that will be equal to document target element by ID product, underscore
ID value.
And we have the plan and the payment method in the email in the last four.
So let's get the card brand.
I think that's the next one.
We added her brand.
Now where do we get that?
Where do we actually get the card brand and the expiry date?
Well, it's actually all in the result that's passed to the function stripe payment method handler.
After we make that first call up here in Stripe create payment method.
That's what gives us our necessary information for the card brand, for example, it's a card brand
that will be equal to result DOS payment method DOT card, the card that was used to make this subscription
to the plan.
And we're looking for brand
and in the same way next month.
Is that what I called it in handler's API expiry, I better call it.
Let's be consistent.
And next year, next year, it's hard to say back to bronze plan that will be equal to result payment
method, dot card, dot exp underscore month and then I'll just duplicate that and change it a year
because we get that the same way from our result.
And we want the first name and last name.
First, underscore name is what I called it, and that will be document get element by ID, dot, first
name, I believe, dot value and then we'll duplicate that in the last name.
And last name.
And the last thing I want to include in there, let's make sure we have it in our handlers, is the
amount of the transaction.
Yes, we have an amount and we called it amount, so amount.
And that will be able to document that element by ID.
And we're looking for amount will verify that in a moment value.
Now, I believe it's called amount, so let's scroll up and make sure amount it equals amount and that
gives us the price in the correct format with no decimals.
All right.
So we now have a new payload that we're sending off to our back end.
And of course, that happens when we call API, create customer unsubscribe to plant.
So let's go over to our handlers API and find that method.
Here it is right here.
So what I'm going to do, first of all, is we need to handle our errors a little bit better than we're
doing right now, because all we're doing is saying app error on print, print line error and return
it.
And I want to make this a little more useful.
So there's a couple of changes I'm going to make here.
The first one is to get rid of this, OK, is assigned a true down here.
And instead, I'll create a variable called OK, right here, it doesn't matter where I put it, ver,
I'll just say OK is a sign the value of truth.
So I'm just moving it.
So we have it a little bit earlier in the function.
And I'm also going to create a variable called subscription
and I'll make that a pointer to straight dot subscription.
And the reason I'm doing that is down here, when I'm calling card subscribed to plan, I'm getting
back the subscription ID and I want to I want to modify this function and instead make this as a full
subscription.
What I'll do is make this subscription subscribed to plan.
So right here we'll make this return instead of a string will return a pointer to stripe subscription
and down here we'll return the full subscription instead because we might need that a little bit later
on.
And that means this becomes anneal because you can't return an empty string for a pointer to stripe
subscription.
OK, back to our handlers, and this becomes an assignment to an equal sign that takes care of that.
And this becomes subscription ID if we want to keep that for long.
OK, so that takes care of that.
So now I created the OK variable.
I've created the subscription variable.
The first thing I try to do is to get the customer and instead of saying return here, I'll just set,
OK, false.
And down here I will say if ok then I want to do all this stuff,
get rid of this format.
Everything OK.
So now if we have an error, when we try to get a straight customer, we'll print the error to the log,
which is fine.
We'll set OK default and we won't execute this if this didn't work the way that it was supposed to.
Now, in the same way, once I try to subscribe the customer to a particular plan, if there's an error,
I'm just going to say OK equals false.
OK, so this is a new way of making sure that we're going to have valid information before we go any
further.
First of all, we check to see if we can create a customer, if not said OK, to if we manage to do
that, we try to subscribe that customer to a particular plan.
And if that doesn't work, then we said, OK, default again.
And if all of those worked, OK, we'll still be true.
So we can say if OK.
And here is where we're going to take care of creating a transaction, creating a customer locally,
creating an order and saving all of that information.
And if not, then, of course, we're going to send back OK, which would be false.
And the message to whatever we want it to be some kind of error message.
So we'll take care of the creating a customer and saving it, creating a transaction and saving it and
creating an order and saving it in the next lecture.