WEBVTT 00:01.020 --> 00:06.210 So we have our canceled subscription functionality in the cards package, and, of course, we now need 00:06.210 --> 00:10.260 to create a handler on the API that allows us to cancel a subscription. 00:10.920 --> 00:17.490 So right now, if I go to Handler's APCO, we have this refund charge handler and it will be similar 00:17.490 --> 00:18.060 to this. 00:18.720 --> 00:24.310 But instead of canceling or refunding an individual charge, we're going to actually cancel a subscription. 00:24.330 --> 00:30.240 So I'll just scroll down right below this and create a new function, which I'll call func with a pointer 00:30.240 --> 00:36.350 of a receiver of app pointed to application and I'll call it cancel subscription. 00:36.540 --> 00:36.900 OK. 00:38.180 --> 00:43.860 And it's a handler, so it takes a response writer and a pointer to a request 00:46.500 --> 00:49.460 and it's going to receive a payload adjacent payload. 00:49.470 --> 00:56.160 So I'll create a variable to Unmerciful or JSON into her and I'll call it sub to cancel, subscription 00:56.160 --> 00:56.810 to cancel. 00:56.820 --> 01:02.160 And it's a start and it has a field called IVI, which is an end. 01:02.310 --> 01:07.860 And in JSON we'll call that ID and it has a field called payment intent. 01:07.870 --> 01:09.870 And of course, it's not really a payment intent. 01:10.710 --> 01:14.880 And I may rename that, I think I might add, a cleanup section where we do a little bit of cleanup 01:14.880 --> 01:17.520 because this should be called identifier or something like that. 01:17.790 --> 01:19.020 Right now, let's just get this working. 01:19.440 --> 01:21.480 So payment intent, which is a string. 01:21.960 --> 01:30.030 And in JSON, we'll call that P.I. and currency and it's a string. 01:30.210 --> 01:34.250 And in JSON I'll call that currency. 01:34.890 --> 01:38.100 So that's what we're going to get from the back end or from the front end. 01:39.090 --> 01:42.150 And of course, now I need to read my values into that chasten. 01:42.150 --> 01:49.350 So error is assigned the value of after Chazen and that requires a response. 01:49.350 --> 01:55.770 Writer are pointed to a request and we're on martialing into subcouncil and we check for an error. 01:56.160 --> 02:05.400 If error is not equal to nil, then we'll just say after bad request and give it to her and the error 02:05.880 --> 02:06.650 and will return. 02:07.830 --> 02:10.860 But if we get past that, we then marshall the Jasons successfully. 02:12.570 --> 02:17.430 So at this point we need to again initialize our Kurds' package. 02:17.940 --> 02:27.240 So KO'd is assign the value of Kurds notecard and we give it our secret, which is after config 02:31.410 --> 02:33.750 dot straight dot secret. 02:34.950 --> 02:44.850 We give it our key, which is abda config the key, and we give it a currency which is sub to cancel 02:45.630 --> 02:46.330 the currency. 02:47.450 --> 02:49.110 OK, so we have our card now. 02:49.110 --> 02:49.650 We can use it. 02:52.890 --> 03:01.650 So we'll check for an error and we get the error or the potential error from calling card dot cancel 03:01.650 --> 03:02.220 subscription. 03:02.880 --> 03:08.970 And all that requires is the subscription ID which we've stored in the field payment content sub to 03:08.970 --> 03:11.160 cancel payment content. 03:12.420 --> 03:13.170 Check for an error. 03:13.350 --> 03:14.790 I'll just copy and paste this. 03:20.510 --> 03:25.570 And again, we need to update the status in the database now, going to come back up here and find that. 03:25.670 --> 03:34.430 So I'll just copy this, copy all of this because it's almost identical and come back down here and 03:34.430 --> 03:35.020 paste it in. 03:36.290 --> 03:42.320 So we're not going to be updating the audio status charge to refunder I.D. We're going to be updating 03:42.320 --> 03:53.000 the sub to cancel it and we're not going to set it to status, i.e. to set it to three, because back 03:53.000 --> 03:59.960 in our database, if we look at our order statuses, we do have a three so statuses and we'll clear 03:59.960 --> 04:02.750 this status. 04:02.760 --> 04:05.060 Three is canceled and that's the one I want. 04:05.250 --> 04:06.520 So I'll set it to three. 04:07.640 --> 04:10.520 And the error message will be the subscription 04:13.910 --> 04:14.750 was canceled, 04:18.890 --> 04:20.480 but the database could not be updated. 04:20.480 --> 04:21.230 And that's fine. 04:21.470 --> 04:27.160 And my message is going to be subscription canceled. 04:28.100 --> 04:34.940 OK, so there is a handler that we can actually call, so we're getting closer, but we still have some 04:34.940 --> 04:35.390 work to do. 04:35.390 --> 04:41.590 Of course, we have to go modify our sales page to account for the situation where we're calling one 04:41.600 --> 04:44.570 API route and where we're calling another API route. 04:44.660 --> 04:49.000 And while we're at it, let's go to our Roots API and set up a route to this. 04:49.160 --> 04:57.920 So down here in Route Stasch API, go all that another route which I will call Mux Dot post because 04:57.920 --> 05:07.580 we'll post to this and it will be API slash admin slash cancel subscription and have this call the handler 05:07.670 --> 05:10.280 app dot cancel subscription. 05:11.590 --> 05:19.460 OK, so now we have our logic in the back end to save the subscription ID and we also have the appropriate 05:19.460 --> 05:22.850 function in our Kurd's package that allows us to cancel a subscription. 05:23.180 --> 05:28.340 We have a handler that will take a JSON payload and attempt to cancel a subscription and we have a route 05:28.340 --> 05:29.090 to that handler. 05:29.090 --> 05:31.520 So it's time to turn our attention to the front end.