So I've managed to subscribe to customer and captured the subscription idea and of course the payment
method idea, and now I need to implement some functionality that will allow me to cancel a subscription.
And if you look at the strike documentation and I'll put a link to this in the course resources for
this lecture, it's pretty straightforward and it's about the same amount of code as it is to refund
a charge.
Now, up here, it says by default, subscriptions and billing continue until cancellation as configured
in your subscription life cycle settings.
And you can go read that a subscription may cancel automatically after up to four unsuccessful attempts
to build a customer.
Well, that's perfect.
That's exactly what I want.
And here is the sample code for how to cancel the subscription.
So let's go back to our editor to Visual Studio Code and open up cards.
Don't go.
And I'll create another method or another function right here.
Func C pointer to card and I'll call this one cancel subscription and it will take a sub ID subscription,
which is a string.
And that's the thing we just added to the database when we insert a transaction and it potentially returns
an error.
And of course I need to do the same thing I do here, Strachey, so I'll just copy that to set our secret
key.
Otherwise, nothing will work.
And now I need to create some parameters which I'll just call her arms.
And that's equal to a reference to Stripe Dot subscription Prem's.
And all I need to put in there is cancel at period end and set that to strike rule.
True.
So the only parameter I'm sending is this person wants to cancel his or her subscription, so don't
renew it, cancel it as soon as their current period ends.
So they're paying me twenty dollars a month.
If the period ends in two weeks, in two weeks their subscription will be cancelled.
OK, so that's pretty simple.
And now I just cancel the subscription, so I'll ignore the first return parameter, but I will check
for an error and I'll call sub dot update.
And hopefully that imported the right version.
I'll check in a moment and I hand it the subscription ID, which I received as a parameter to this function
and the presence that we just created.
And I'll check for an error if error is not equal to nil.
Return the girl, otherwise return them.
OK, so there is any function available to us from our current package and we can use that in a handler
to actually cancel a subscription.
And we'll get started on that in the next lecture.