WEBVTT 00:02.100 --> 00:07.310 So now that we know we have everything more or less wired up properly, let's go back to Kurds' Don't 00:07.320 --> 00:12.600 Go, which is in the internal folder, in the Kurds folder, and add a couple of methods that allow 00:12.600 --> 00:16.660 us to create a customer and then subscribe that customer to a given plan. 00:17.190 --> 00:22.200 So I'm going to scroll to the bottom of this file just before Khurd error message. 00:22.830 --> 00:26.380 And I'll add, first of all, a method that allows us to create a customer. 00:26.430 --> 00:34.310 So it's a function and it has the receiver of a pointer occurred and I'll call this create customer. 00:36.420 --> 00:42.850 And this is going to take two parameters, RPM, which is a payment method and email we should have 00:42.850 --> 00:53.520 then both of these are strings and I will return a pointer to a stripe customer and a string and potentially 00:53.520 --> 00:53.910 an error. 00:57.740 --> 01:03.230 Now, because we're working with Stripe, we need to set our secret keys, so stripe key is equal to 01:03.230 --> 01:04.070 from the receiver. 01:04.100 --> 01:05.690 See our secret? 01:08.980 --> 01:14.080 And now I want to create a customer, so to do that, we need to set some parameters and I'm going to 01:14.080 --> 01:22.510 call my variable customer programs and that will be a sign, the value of a reference to from a straight 01:22.510 --> 01:24.640 package customer Purim's. 01:28.300 --> 01:28.770 Right there. 01:30.640 --> 01:38.170 So we're going to specify the payment method, and that needs to be in a striped string format, so 01:38.170 --> 01:42.850 we'll cast it to the stripe downstream and that's just our payment method. 01:45.920 --> 01:51.590 And the second value will be the email, again, that needs to be in a striped string, and that's just 01:51.590 --> 01:52.610 our email primmer. 01:55.620 --> 01:57.810 The third one is invoice settings. 02:00.460 --> 02:08.580 And this will be set to a reference to striped court customer invoice settings, programs right there. 02:10.060 --> 02:12.700 And what we need to specify, and this is the default payment method. 02:16.270 --> 02:23.650 And in this case, that's going to be equal to stripe dot string, peer payment method, which seems 02:23.650 --> 02:23.880 odd. 02:23.890 --> 02:26.430 It's like we're specifying it twice, but that's how we have to do it. 02:27.670 --> 02:28.790 And we'll put a comma there. 02:29.590 --> 02:33.540 OK, now that we have our parameters all set up, let's create a customer. 02:33.640 --> 02:34.840 So I'll call my customer. 02:34.960 --> 02:39.670 Just let me scroll up and give us some room and we'll check for an error. 02:40.540 --> 02:44.710 And that's a sign the value of customer, the new. 02:48.310 --> 02:54.870 And it takes our customer perhaps, and we'll check for an error, if error is not equal to nil 02:58.480 --> 03:01.430 in this case, I'm going to create an empty string variable called MSG. 03:03.310 --> 03:03.900 Hello again. 03:06.970 --> 03:12.910 And now I'll just check to see if we have an error, if stripe error, which is a variable, I'm just 03:12.910 --> 03:14.490 declaring and OK. 03:16.120 --> 03:21.610 And those are a sign the value of error and we're going to cast it to a striped out error. 03:25.810 --> 03:33.210 And then, OK, so if we have an error, we'll set our message to using our card error message method 03:33.220 --> 03:42.190 that's just below this one and we'll hand the strike error and it's code and that will get the error 03:42.190 --> 03:44.140 message into something that's a little more readable. 03:45.580 --> 03:51.370 And we'll return nil plus our message, plus the error. 03:53.540 --> 04:01.670 Otherwise, everything worked and we created a customer so low return, just an empty string, and so 04:01.670 --> 04:03.940 that allows us to create a straight customer. 04:03.950 --> 04:08.340 But of course, we also want to be able to subscribe this customer to a plan. 04:08.960 --> 04:11.600 So let's create another function and I'll do it just above this one. 04:12.620 --> 04:15.500 Again, it's going to be a function with a receiver of a pointer to card. 04:17.120 --> 04:26.330 And this one, I'll just call it subscribe to plan and it will take a customer cost, which is a pointer 04:26.330 --> 04:28.570 to stripe dot customer. 04:31.400 --> 04:37.400 And it's also going to take the plan, the email, the last four digits of the transaction and the card 04:37.410 --> 04:37.720 type. 04:39.620 --> 04:45.540 And those are all strings and it will return a string and potentially there. 04:48.800 --> 04:51.380 So the first thing we need to do is to get the straight customer right. 04:51.440 --> 04:57.470 So we'll create a variable stripe customer and that will be a sign the value of from the customer, 04:57.470 --> 04:59.420 the customer that's passed to this function. 05:01.550 --> 05:05.120 And the next thing we need to decide is what are we going to subscribe this customer to? 05:05.270 --> 05:10.160 And that needs to be in the form of a slice of pointers to stripe subscription item. 05:10.760 --> 05:11.420 So let's do that. 05:11.800 --> 05:16.600 Call the things we're going to subscribe them to items and there might be more than one in our case 05:16.610 --> 05:16.930 right now. 05:16.940 --> 05:25.430 There's just going to be one, but that's a slice of a pointer to stripe dot subscription items. 05:29.020 --> 05:37.660 Items, programs, and will populate that with what it needs, which is which is a plan and that has 05:37.660 --> 05:39.580 to be in the form of a striped string. 05:39.940 --> 05:43.780 So we'll cast it to a striped string and it's just a plan. 05:44.290 --> 05:45.960 The string that was passed to this function. 05:48.040 --> 05:49.450 So now we know the customer. 05:49.780 --> 05:51.520 We know what we're going to subscribe them to. 05:52.150 --> 05:54.520 So let's set up some parameters that we'll need in a moment. 05:55.310 --> 05:56.590 So I'll call that Paramus. 05:56.590 --> 06:01.240 And that's going to be a reference to striped subscription programs. 06:06.990 --> 06:14.520 And in this, we need a customer and that's going to be cast to a striped string MetroStars striped 06:14.520 --> 06:14.940 customer. 06:18.820 --> 06:22.880 We also need the items which we just created items. 06:26.220 --> 06:30.440 And I'm going to add some metadata to this, things that we might need later on and we might not. 06:30.450 --> 06:32.690 But it's not going to hurt anything to add this metadata. 06:32.880 --> 06:42.690 So programs the add metadata and I will add the last four digits, which I got passed right to this 06:42.690 --> 06:44.500 function last four. 06:45.870 --> 06:48.630 And I'll also add parameter to add metadata. 06:53.040 --> 06:54.490 I'll the current type. 06:58.440 --> 07:03.450 Which is card type, which will receive as a result of this function, and then we're going to add the 07:03.450 --> 07:07.680 payment intent, which is fairly important because we actually have to build these people on a regular 07:07.680 --> 07:08.210 basis. 07:08.460 --> 07:13.610 And one of the Syntex to add that's a little bit different is programs and expand. 07:16.080 --> 07:20.360 And what we want to add is latest underscore invoice. 07:20.700 --> 07:24.270 What was the invoice we last charged them with and payment intent. 07:25.650 --> 07:32.880 That's how we get the payment content for a subscription and no subscription, which is just a variable 07:32.880 --> 07:33.510 I'm creating. 07:33.930 --> 07:35.100 And we'll check for an error. 07:35.280 --> 07:42.420 That's a sign the value of subduct new and it should have done the auto import for us. 07:42.510 --> 07:44.090 We just had no programs to it. 07:45.790 --> 07:55.990 You're going to spell right programs and let's check for an error, if error is not equal to nil, then 07:55.990 --> 08:07.270 we'll return nothing and empty string and the error, otherwise we return the subscription ID 08:10.280 --> 08:11.530 and No. 08:14.360 --> 08:19.670 So there are the two functions, if we got everything right, that allow us to, first of all, create 08:19.670 --> 08:26.110 a customer right down here and then use that customer to subscribe to a given plan. 08:26.360 --> 08:26.780 All right. 08:26.790 --> 08:32.870 In the next lecture, we'll actually go back to the handler that we used to subscribe a user to a plan 08:33.230 --> 08:35.210 and try it out and see how it works. 08:35.910 --> 08:41.600 I see an error there or at the very top could not import go customer. 08:41.610 --> 08:42.950 I missed something here. 08:43.010 --> 08:47.390 We didn't get the seventy to the seventy two there. 08:47.720 --> 08:48.140 All right. 08:48.290 --> 08:49.930 Now we can try it out in the next election.