So we've created the necessary functions in our cards package to create a customer and subscribe the
customer to a plan.
So now let's updater create customer and subscribe to Plan Handler, which is in Handler's Dash APG.
So let's update this so we can actually try to subscribe someone to a plan.
So the first thing we're going to have to do obviously is the same sort of thing we did up here and
that is create a card.
So let me copy this code and this is from get payment intent.
We need to create and set up our card package with the necessary values.
So back down here, I'll do it right after this info log.
So I create the card variable.
Now, obviously, it's not payload currency because up here we call the data, so I'll just change that
data.
So we have a card.
Now let's get our customer first, our customer.
So Strib customer, a message and potentially an error all come from card dot, create customer.
And that requires our payment method and that data payment method and it requires the email which is
dated email, which we pulled rid to the adjacent file.
So let's check for an error and we'll just copy this error checking code and paste it right here.
So we have our customer, if everything went well.
Now we need our subscription ID
and potentially an hour and those come from KO'd Dot subscribed to plan and that takes a few parameters.
It takes our straight customer, which we just got straight customer.
It requires the plan data dump plan.
It requires the email dated email, and it requires the last four digits data don't last for.
And then we actually put card time, but we never actually bothered to get that.
So I'll just pass an empty string.
OK, and again, check for an error so I'll just paste.
But I'd copied a moment ago and if everything went well, we should be able to go after info log print
line and print out our subscription.
So subscription ID is and then just subscription.
And of course the last thing I need to do is just comment this line out because we actually declare
a message variable up here on line 119.
So I'll comment this and let's try running this.
So make sure everything is stopped.
Mike, stop.
It is Mike start.
And there we go.
It's running now, we'll try running a transaction and it's not going to work.
And I'm doing this quite deliberately because I want to show you the kinds of error messages you get
back from Stripe's.
So I'll clear the console and we'll go to the products menu and subscription and I'll clear the JavaScript
console and I'll try doing something.
So Sam Smith, Sam at Smith dot com, Sam Smith is the name on the card and our standard for two.
And make this an expiry and give it some value here for the CVC and it's not going to work, but let's
see what we get for a message back from strike.
So we get an error over here unexpected, and that's to be expected because we're not doing really good
air handling at this point.
But let's go back and look at the console and you see you get a message here.
So first of all, we have our printing out the values that we got from the JSON.
So that works.
And then we get an error here.
Error request error from stripe status.
Four hundred code resource missing.
And on it goes.
No such plan.
Now, if we go back to our Web browser and we go to our products and we get them looking at test data
and I bring it by Brons widget plan, what we actually copied and you may have noticed this, I did
it quite deliberately.
There's no copy button here next to this.
And this is what we have in the database when in fact, what we want is this the price.
So let me copy that and switch over to my database and look at widgets and let's change its plan from
this product.
So I'll select all delete and paste in the correct one, OK, and save that and go back to our Web browser
and we'll try again.
So products, subscriptions.
And I'm going to go back to Stripe for a minute just to look at subscriptions.
And if we go to, say, customers and look at subscriptions.
So the most recent one is Jack Nimble.
That's the most recent subscription that I have.
Let's go subscribe at this point and we'll probably get a JavaScript error after the fact, but that
doesn't matter.
Fixed that up shortly.
So this time we'll do Sam Smith again and Sam at Smith dot com, Sam Smith with a credit card.
And now let's pay twenty dollars a month.
So we got our response back.
OK?
And it kind of stopped at this point, nothing else happens because we're not telling it to do anything
else.
But if we go back to subscriptions and refresh this page.
We should have a new subscription, Sam Smith there.
We have actually subscribed a user to a plan.
So as you can see the messages you get back from Stripe when something goes wrong are very good.
For example, if I go back to my cards package, if, for example, I forgot to in this one, forgot
to set my secret, you'll actually get a message back from Stripe, which is extremely readable and
tells you exactly what went wrong.
So if you're having trouble running transactions or whatever through strike, pay close attention to
the error messages you get back from strike because they're extremely helpful.
All right.
We're a lot closer.
We'll move on in the next lecture.