So the next step we need to follow is to actually create the handler for this fetch statement online
157 and of course, this is calling the back end.
So let's open our API folder and open our Roots API and let's create a route that points to a non-existent
handler.
And I'll just put it right here.
So Muxtape post, because this is a post request and we're looking for API and it's a long one.
Create customer and subscribe to plan and that's going to call after.
And we'll create a handler called Create Customer and subscribe to the plan and that doesn't exist.
So let's go over to our API handlers, which is the file handlers Dash API Dargo in the API folder and
at very bottom I'll create a new handler and it will take the receiver app application.
And it's called Create Customer and Subscribe to Plan.
And we give it the W, which is an EDP response writer and ah, which is a pointer to an IEP request.
And if I do that, just that much should make the arrow go away and route's dash API and it does where
everything is named properly.
So what do we want to do in this handler?
Well, to start with, we're going to have a payload in the form of JSON passed to us.
So we'll create a variable called data and it will be in the form of straight payload.
Now, if we scroll up to where the payload is defined, we have here the only things I'm expecting to
find in stright payload are currency and amount.
And I'm actually not passing either one of those.
If we go back to the bronze plan page, I'm actually passing it in my payload.
Where is my payload?
There it is right there.
A plan, a payment method, an email and allows for.
So let's go back to Handler's API and we'll add to this a payment method.
Which is a string and that will be in Jason called payment underscore method of duplicate that we're
also handling it.
An email which is in the string of Jason email is a string.
And then Jason, I'm calling it email.
Then I'm passing last for the last four.
And then Jason, I'm going to call that last underscore four.
And finally, we're ending the plan, which is a strike plan, the ID for it.
And in this off, just call a plan.
OK, so let's format this and scroll back down to our new handler and start doing things with it.
So the first thing I'll do is get my Jason or get the values out of Jason into something I can use.
So error is assigned the value of Jason's new decoder and I'm decoding the request body and I'm going
to decode it into ampersand data variable and declare it on line.
One of four will check for an error if error is not equal to nil.
And all I'll do right now is after error log print one error and return.
As I've said before, not very friendly, but sufficient for my purposes at the moment.
Now let's just see if we get those values after info log gridline and I'll just print a few out.
So I'll printed, say, dated or email and dated last four and dated dot o payment method and data plan
to print that information out because that's where we're be past and I'll just give them a hard coded
response.
So I'll set a variable.
OK, equal to true and I'll set a variable message which will default to an empty string and then I'll
use my Jason response type.
So response is assign the value of Jason resp.
It's been a while since we've used that.
So it's Jason response and we'll just set OK to OK and message to MSG.
And let's write this information out as Jason gives some kind of response back.
So I wrote an error or assign the value of Jason Marshall indent and a commercial rest.
I'll give it no prefix and a few spaces for indentation and I'll copy my error checking code.
Paste it in here and then write things up, so w dot header, dot set and we'll set content type two
application slash Jason and W dot right at.
And that should give us at least enough to make sure that everything is wired up correctly, so let's
see if things are wrong.
I'll say make stop.
Good make start.
Everything combined, let's go back to our Web browser.
So let's go to our subscription page.
Open the terminal window, the JavaScript terminal, just to make sure things are working as expected.
Let's just see what happens here.
OK, so I'll clear the console and I'll put in some test data.
So Sam Smith, Sam Smith, the CEO, Sam Smith, and some credit card number four to four to four to
four two and make it expire in the future with some CVC.
And let's see what we get back, if anything.
So I got my OK, true back, and if I go back and look at my I.D., I actually have Sam Smith the last
four digits, the payment method and the plan I want to subscribe to.
Perfect.
Now, obviously, we need to do more in this create customer and subscribe to plan handler than just
pass them, Jason, and print some information out.
We actually need to create that straight customer and we need to subscribe that customer to a plan.
And we're going to do that not here in Handler's API.
Of course, we'll do it over in our internal package named Cards.
Cards Don't Go.
So we'll get started on that in the next lecture.