So we have our canceled subscription functionality in the cards package, and, of course, we now need
to create a handler on the API that allows us to cancel a subscription.
So right now, if I go to Handler's APCO, we have this refund charge handler and it will be similar
to this.
But instead of canceling or refunding an individual charge, we're going to actually cancel a subscription.
So I'll just scroll down right below this and create a new function, which I'll call func with a pointer
of a receiver of app pointed to application and I'll call it cancel subscription.
OK.
And it's a handler, so it takes a response writer and a pointer to a request
and it's going to receive a payload adjacent payload.
So I'll create a variable to Unmerciful or JSON into her and I'll call it sub to cancel, subscription
to cancel.
And it's a start and it has a field called IVI, which is an end.
And in JSON we'll call that ID and it has a field called payment intent.
And of course, it's not really a payment intent.
And I may rename that, I think I might add, a cleanup section where we do a little bit of cleanup
because this should be called identifier or something like that.
Right now, let's just get this working.
So payment intent, which is a string.
And in JSON, we'll call that P.I. and currency and it's a string.
And in JSON I'll call that currency.
So that's what we're going to get from the back end or from the front end.
And of course, now I need to read my values into that chasten.
So error is assigned the value of after Chazen and that requires a response.
Writer are pointed to a request and we're on martialing into subcouncil and we check for an error.
If error is not equal to nil, then we'll just say after bad request and give it to her and the error
and will return.
But if we get past that, we then marshall the Jasons successfully.
So at this point we need to again initialize our Kurds' package.
So KO'd is assign the value of Kurds notecard and we give it our secret, which is after config
dot straight dot secret.
We give it our key, which is abda config the key, and we give it a currency which is sub to cancel
the currency.
OK, so we have our card now.
We can use it.
So we'll check for an error and we get the error or the potential error from calling card dot cancel
subscription.
And all that requires is the subscription ID which we've stored in the field payment content sub to
cancel payment content.
Check for an error.
I'll just copy and paste this.
And again, we need to update the status in the database now, going to come back up here and find that.
So I'll just copy this, copy all of this because it's almost identical and come back down here and
paste it in.
So we're not going to be updating the audio status charge to refunder I.D. We're going to be updating
the sub to cancel it and we're not going to set it to status, i.e. to set it to three, because back
in our database, if we look at our order statuses, we do have a three so statuses and we'll clear
this status.
Three is canceled and that's the one I want.
So I'll set it to three.
And the error message will be the subscription
was canceled,
but the database could not be updated.
And that's fine.
And my message is going to be subscription canceled.
OK, so there is a handler that we can actually call, so we're getting closer, but we still have some
work to do.
Of course, we have to go modify our sales page to account for the situation where we're calling one
API route and where we're calling another API route.
And while we're at it, let's go to our Roots API and set up a route to this.
So down here in Route Stasch API, go all that another route which I will call Mux Dot post because
we'll post to this and it will be API slash admin slash cancel subscription and have this call the handler
app dot cancel subscription.
OK, so now we have our logic in the back end to save the subscription ID and we also have the appropriate
function in our Kurd's package that allows us to cancel a subscription.
We have a handler that will take a JSON payload and attempt to cancel a subscription and we have a route
to that handler.
So it's time to turn our attention to the front end.