So now that we know we have everything more or less wired up properly, let's go back to Kurds' Don't
Go, which is in the internal folder, in the Kurds folder, and add a couple of methods that allow
us to create a customer and then subscribe that customer to a given plan.
So I'm going to scroll to the bottom of this file just before Khurd error message.
And I'll add, first of all, a method that allows us to create a customer.
So it's a function and it has the receiver of a pointer occurred and I'll call this create customer.
And this is going to take two parameters, RPM, which is a payment method and email we should have
then both of these are strings and I will return a pointer to a stripe customer and a string and potentially
an error.
Now, because we're working with Stripe, we need to set our secret keys, so stripe key is equal to
from the receiver.
See our secret?
And now I want to create a customer, so to do that, we need to set some parameters and I'm going to
call my variable customer programs and that will be a sign, the value of a reference to from a straight
package customer Purim's.
Right there.
So we're going to specify the payment method, and that needs to be in a striped string format, so
we'll cast it to the stripe downstream and that's just our payment method.
And the second value will be the email, again, that needs to be in a striped string, and that's just
our email primmer.
The third one is invoice settings.
And this will be set to a reference to striped court customer invoice settings, programs right there.
And what we need to specify, and this is the default payment method.
And in this case, that's going to be equal to stripe dot string, peer payment method, which seems
odd.
It's like we're specifying it twice, but that's how we have to do it.
And we'll put a comma there.
OK, now that we have our parameters all set up, let's create a customer.
So I'll call my customer.
Just let me scroll up and give us some room and we'll check for an error.
And that's a sign the value of customer, the new.
And it takes our customer perhaps, and we'll check for an error, if error is not equal to nil
in this case, I'm going to create an empty string variable called MSG.
Hello again.
And now I'll just check to see if we have an error, if stripe error, which is a variable, I'm just
declaring and OK.
And those are a sign the value of error and we're going to cast it to a striped out error.
And then, OK, so if we have an error, we'll set our message to using our card error message method
that's just below this one and we'll hand the strike error and it's code and that will get the error
message into something that's a little more readable.
And we'll return nil plus our message, plus the error.
Otherwise, everything worked and we created a customer so low return, just an empty string, and so
that allows us to create a straight customer.
But of course, we also want to be able to subscribe this customer to a plan.
So let's create another function and I'll do it just above this one.
Again, it's going to be a function with a receiver of a pointer to card.
And this one, I'll just call it subscribe to plan and it will take a customer cost, which is a pointer
to stripe dot customer.
And it's also going to take the plan, the email, the last four digits of the transaction and the card
type.
And those are all strings and it will return a string and potentially there.
So the first thing we need to do is to get the straight customer right.
So we'll create a variable stripe customer and that will be a sign the value of from the customer,
the customer that's passed to this function.
And the next thing we need to decide is what are we going to subscribe this customer to?
And that needs to be in the form of a slice of pointers to stripe subscription item.
So let's do that.
Call the things we're going to subscribe them to items and there might be more than one in our case
right now.
There's just going to be one, but that's a slice of a pointer to stripe dot subscription items.
Items, programs, and will populate that with what it needs, which is which is a plan and that has
to be in the form of a striped string.
So we'll cast it to a striped string and it's just a plan.
The string that was passed to this function.
So now we know the customer.
We know what we're going to subscribe them to.
So let's set up some parameters that we'll need in a moment.
So I'll call that Paramus.
And that's going to be a reference to striped subscription programs.
And in this, we need a customer and that's going to be cast to a striped string MetroStars striped
customer.
We also need the items which we just created items.
And I'm going to add some metadata to this, things that we might need later on and we might not.
But it's not going to hurt anything to add this metadata.
So programs the add metadata and I will add the last four digits, which I got passed right to this
function last four.
And I'll also add parameter to add metadata.
I'll the current type.
Which is card type, which will receive as a result of this function, and then we're going to add the
payment intent, which is fairly important because we actually have to build these people on a regular
basis.
And one of the Syntex to add that's a little bit different is programs and expand.
And what we want to add is latest underscore invoice.
What was the invoice we last charged them with and payment intent.
That's how we get the payment content for a subscription and no subscription, which is just a variable
I'm creating.
And we'll check for an error.
That's a sign the value of subduct new and it should have done the auto import for us.
We just had no programs to it.
You're going to spell right programs and let's check for an error, if error is not equal to nil, then
we'll return nothing and empty string and the error, otherwise we return the subscription ID
and No.
So there are the two functions, if we got everything right, that allow us to, first of all, create
a customer right down here and then use that customer to subscribe to a given plan.
All right.
In the next lecture, we'll actually go back to the handler that we used to subscribe a user to a plan
and try it out and see how it works.
I see an error there or at the very top could not import go customer.
I missed something here.
We didn't get the seventy to the seventy two there.
All right.
Now we can try it out in the next election.