WEBVTT 00:03.500 --> 00:10.280 So this time around, we want to set up our back end to give us a payment intent, and I'll be talking 00:10.280 --> 00:11.390 about that in some detail. 00:11.390 --> 00:16.700 But suffice it to say that the payment intent is something you get from Stripe and it changes during 00:16.700 --> 00:18.280 the life cycle of a transaction. 00:18.290 --> 00:20.520 And it's what we're going to use to charge a credit card. 00:21.170 --> 00:26.120 So before we do that, let's just go back and verify that our JavaScript we set up is entirely correct. 00:26.130 --> 00:34.760 So go back to my ID and right here in the Valley function, what I'm going to do is create another function 00:34.760 --> 00:43.280 outside of this and I want to call it func should hide payment buttons or just hide Paiva like that, 00:43.620 --> 00:44.100 OK? 00:44.750 --> 00:50.300 And that's a simple JavaScript function and all it's going to do is go to pay button. 00:51.060 --> 00:55.910 That's a constant we set up a little while ago, as you can see it right there in line sixty two class 00:56.650 --> 01:03.500 list, and then we'll add the nun to that to hide the pay button so they don't click on it twice. 01:03.500 --> 01:06.050 The user doesn't accidentally charge a credit card twice. 01:06.740 --> 01:20.550 And we'll also go processing the class list dot remove DNA and that will show a little processing center. 01:20.580 --> 01:21.680 So let's make sure that works. 01:22.100 --> 01:30.440 So right here in the valve function, I'll just call that paper, paper, paper like that and I'll say 01:30.450 --> 01:30.860 this. 01:30.860 --> 01:32.350 Make sure my application is running. 01:32.360 --> 01:33.490 Yes, it just rebuilt. 01:33.500 --> 01:34.110 That's good. 01:34.220 --> 01:39.980 So let's go back to our Web browser, reload the page and first of all, make sure the validation still 01:39.980 --> 01:40.400 works. 01:40.580 --> 01:41.220 It does. 01:41.220 --> 01:43.760 So I'll put some in some values in here. 01:43.760 --> 01:51.650 One me the add here dot com and charge Kirk and this payment charge card should be hidden. 01:52.040 --> 01:54.830 We should see a little spinner right here and we do. 01:54.860 --> 01:55.730 So that works great. 01:55.730 --> 01:56.240 Perfect. 01:57.020 --> 01:58.190 So let's go back to our code. 01:58.520 --> 02:04.250 And now what I want to do is to set up code that will get a payment intent from strike. 02:04.760 --> 02:08.360 And of course, that means we need to add something to our go file. 02:08.370 --> 02:09.690 So back to our Web browser. 02:11.390 --> 02:14.510 This is the official stripe package and it's on GitHub. 02:14.510 --> 02:17.000 And there's a link to this in the course resources for this lecture. 02:17.480 --> 02:19.190 And there's a lot of coding there, as you can see. 02:19.190 --> 02:20.530 So we're going to scroll down to the read. 02:22.930 --> 02:26.750 And find out how to install this and there it is right there. 02:27.250 --> 02:33.540 So as I record this lecture, this is version 72 of Stripe's Striped DeCicco. 02:34.000 --> 02:35.260 And if you want to follow along. 02:35.260 --> 02:36.040 Exactly. 02:36.040 --> 02:42.280 Make sure that even if it's a later number on the GitHub repository, say it's seventy three or seventy 02:42.280 --> 02:43.830 four, use seventy two. 02:43.840 --> 02:45.280 And that way it'll still work. 02:45.520 --> 02:50.440 And it will absolutely make things simpler for you as well as you follow along just in case there's 02:50.440 --> 02:51.390 a breaking change. 02:51.400 --> 02:53.020 So I'll go back to Visual Studio Code. 02:53.020 --> 02:54.930 I've copied that install link. 02:55.730 --> 02:56.590 Let's get back there. 02:57.250 --> 03:03.640 Opened my terminal, I'll stop my error, cleared the screen and just paste that in and that should 03:03.640 --> 03:04.690 go and get. 03:05.980 --> 03:06.730 And it does. 03:06.770 --> 03:09.190 OK, so now it's installed so we can actually use it. 03:09.850 --> 03:16.630 So I'll close my terminal window and so far all we've been working on is the front end and now I want 03:16.630 --> 03:17.890 to work on the back end. 03:17.920 --> 03:22.870 So we're actually going to have to binaries running at the same time, one that handles displaying Web 03:22.870 --> 03:27.850 pages to the user and one that we're going to call from those Web pages using JavaScript. 03:27.860 --> 03:29.100 So an API, in other words. 03:29.620 --> 03:35.770 So what I'm going to do first is go to my project folder in my explorer and create a new folder in the 03:35.770 --> 03:36.610 internal folder. 03:36.610 --> 03:42.880 So I'll create a new folder in here in Internal and I'm going to call it card and color, whatever I 03:42.880 --> 03:44.260 want, but it actually works. 03:44.260 --> 03:48.840 And inside of Khurd will create a new file called KO'd that would go. 03:49.210 --> 03:52.340 And this is where I'm going to put all of my calls to Stripe. 03:52.750 --> 03:57.460 OK, so package decoration, package card. 03:58.780 --> 04:03.160 And the very first thing I'll do is I'll define a card type, OK? 04:03.550 --> 04:07.990 And this card type is going to hold some information that's necessary for us to talk to. 04:08.770 --> 04:16.910 So type I'll call it card with capital C struct and it's going to have a secret which will be our straight 04:16.930 --> 04:18.100 secret, which is a string. 04:19.270 --> 04:23.200 It will have our key, which we may not use, but it's not going to have it in their string. 04:23.980 --> 04:25.210 That's our publishable key. 04:25.600 --> 04:27.090 And what currency are we charging? 04:27.190 --> 04:28.710 And that also is a string. 04:29.860 --> 04:31.980 So I have that set up format. 04:34.180 --> 04:39.580 The next thing I want to do is to store it, create another type and that's for transaction. 04:39.580 --> 04:44.440 And we're going to start with a very simple one, but it will become more complex as time goes on. 04:44.440 --> 04:47.050 So I'll create another type and that's transaction. 04:49.170 --> 04:59.130 Transactions built right into struct and for fields, it has transaction status, which will be an integer. 05:00.480 --> 05:05.310 And I'm also going to put in there the amount of the transaction, which will also be an it, not a 05:05.310 --> 05:05.810 float. 05:05.820 --> 05:11.960 We're not going to use floats to do our math with transactions, as it suggests in the stripe documentation. 05:11.970 --> 05:17.070 And as you probably know, in any case, it's always easier to do integer math when it comes with transactions. 05:17.070 --> 05:23.310 You don't wind up with floating point errors, but you'll see that's what currency we're using, which 05:23.310 --> 05:23.850 is a string. 05:25.080 --> 05:30.300 I also want to get the last for the last four digits of whatever credit card transaction went through. 05:30.330 --> 05:33.090 That's always useful information to have, not the entire credit card. 05:33.120 --> 05:34.800 I don't ever want that information. 05:35.040 --> 05:39.870 I'm going to send it off to Stripe, never stored anywhere in my application, never have any access 05:39.870 --> 05:40.170 to it. 05:40.200 --> 05:50.910 OK, so that will be a string and I'll have the bank return code, whatever we get back from straight. 05:50.970 --> 05:51.970 Something I can use. 05:51.990 --> 05:53.340 OK, string. 05:54.040 --> 05:54.950 So let's format that. 05:55.410 --> 06:01.800 Now we have two types so let's create a function that will actually charge a credit card. 06:02.200 --> 06:09.630 OK, so I'm going to call this func and it's going to have the receiver of a pointer to the card type 06:12.000 --> 06:16.620 and I'm going to call this create payment intent because that's what I'm actually doing. 06:16.830 --> 06:21.240 OK, and that payment intent is going to have two parameters. 06:21.270 --> 06:24.300 First of all, what currency are we using, which is a string? 06:24.780 --> 06:27.810 And second, what's the amount you want to charge, which is in it? 06:28.830 --> 06:31.590 And it's going to return three possible things. 06:31.590 --> 06:34.530 The first one is the information I want for the transaction. 06:34.530 --> 06:37.770 And it's a pointer or two from the stripe, the package. 06:40.070 --> 06:42.230 And they're important for me, which is good. 06:44.170 --> 06:48.410 It's a payment intent, that's the first return value. 06:48.520 --> 06:52.630 The second potential return value is an error, which will be nil if everything went properly. 06:53.090 --> 06:54.340 And we're also going to return a string. 06:55.360 --> 06:57.880 So that's my function definition. 06:59.410 --> 07:05.830 Now I'm going to have to have access to my secret, OK, my secret, keep my stripes secret. 07:06.130 --> 07:13.960 So I'm going to call from the strike package a key and that's going to be equal to from the receiver 07:13.960 --> 07:17.230 that this function has c dot secret. 07:18.280 --> 07:20.520 OK, so now I have access to my secret key. 07:20.530 --> 07:25.780 And of course, when I call this package, I'm going to have to populate that construct with the secret 07:25.780 --> 07:26.070 key. 07:26.080 --> 07:27.040 But we'll get to that shortly. 07:28.000 --> 07:29.620 Now, I want to create a payment intent. 07:29.950 --> 07:37.090 So we're going to create a payment intent and we're going to do that by creating a variable which we 07:37.090 --> 07:42.460 call parameters, just the parameters we're going to be passing off to Stripe and we're going to use 07:42.460 --> 07:46.270 a reference to a stripe payment intent Prem's. 07:48.150 --> 07:54.570 Intent programs right there and we're going to populate its fields as follows the amount. 07:54.720 --> 07:56.240 Well, I got that as an input. 07:56.590 --> 08:02.910 OK, but I actually need to convert it to a stripe in 60 for which if you read the straight documentation, 08:03.180 --> 08:04.680 it says, that's what I need to have. 08:04.710 --> 08:07.620 Fortunately, we have a utility in the straight package that does it for us. 08:07.980 --> 08:15.400 Stripe dot in sixty four and then I'm going to take the entire gut and cast that to an end to sixty 08:15.430 --> 08:17.820 four and that's just amount. 08:18.680 --> 08:27.360 So that's the first field, the second field as you might have guessed by by now, this currency and 08:27.360 --> 08:27.810 currency. 08:27.810 --> 08:32.880 Again it's just a string, but I need to cast it to a stripe string and again Stripe gives us a package 08:32.880 --> 08:38.370 to do that or utility you string and I just paschen currency. 08:39.960 --> 08:42.480 OK, so I have my parameters at this point. 08:42.600 --> 08:45.840 If I wanted to, I can actually add some metadata. 08:45.840 --> 08:54.750 I could put something like parameters dot add metadata right there and I can just pass in something 08:54.750 --> 09:02.770 as simple as whatever or key, a key and then whatever I want it to be, which I'll make value. 09:03.480 --> 09:04.470 Now we're not going to do that. 09:04.470 --> 09:09.120 But just so you're aware, if you want to add information to this transaction, that's how you do it. 09:09.180 --> 09:10.130 So I'll comment on it. 09:10.140 --> 09:16.160 Just so you have a reference to what we want to do now is actually get our payment intent. 09:16.170 --> 09:19.230 So pie for payment, intent and error. 09:19.230 --> 09:25.320 We're going to check for an error or assign the value of payment intent and that will import imported 09:25.320 --> 09:31.000 from straight payment intent, not new and hopefully up in. 09:31.270 --> 09:32.900 Yes, I have my payment intent. 09:32.910 --> 09:33.480 Important. 09:33.480 --> 09:33.860 Good. 09:34.590 --> 09:37.930 So we want a new payment intent and we're going to pass it the parameters we just gave. 09:38.640 --> 09:41.100 So that gives me a payment intent possibly. 09:41.190 --> 09:42.150 Let's check for an error. 09:42.570 --> 09:44.100 If error is not equal to nil, 09:48.450 --> 09:55.050 then I'll create a variable message which will just be a string and I'll say if stripe error. 09:58.150 --> 10:04.150 OK, stripey of being a variable name, I've just chosen an error, and then we're going to get our 10:04.540 --> 10:08.890 stripe error and that's straight out of the straight package. 10:08.920 --> 10:16.300 OK, and then OK, so if OK is false, then we're going to get our message. 10:16.570 --> 10:19.920 MSJ equals try that again. 10:19.930 --> 10:23.260 MSJ equals whatever the error messages. 10:23.270 --> 10:25.780 So straight error code. 10:27.190 --> 10:28.870 Now let's see if that returns the right type. 10:36.120 --> 10:44.220 OK, now that Erica, that's not very useful, actually, I want to string, so what I'm going to do 10:44.220 --> 10:50.550 is come down here and create another function function and I'll call it third error message. 10:51.000 --> 10:55.470 And it's going to take as a parameter code and the code will be a straight error code. 10:57.180 --> 10:59.550 And that's what we're actually getting up here, OK? 11:01.230 --> 11:03.840 And that's going to return what I want, which is a string. 11:04.680 --> 11:06.310 And I'll just put a couple in for right now. 11:06.510 --> 11:12.030 So again, I'll declare a variable var message and MSG and that's going to be equal to nothing to string. 11:12.960 --> 11:15.570 And then I'll just switch on the code that I can switch. 11:16.620 --> 11:17.370 So code. 11:19.080 --> 11:22.820 And the first thing would be case straight. 11:22.920 --> 11:29.650 The error code card declined error code an hour. 11:29.700 --> 11:30.570 That's why it's not working. 11:31.410 --> 11:32.130 Error code. 11:32.130 --> 11:34.620 KO'd declined. 11:37.750 --> 11:44.260 And get this where it belongs, and I will help, OK, if the area code is cleared, declined, then 11:44.260 --> 11:49.000 I'll say the message is equal to your card was declined. 11:49.540 --> 11:53.070 Now, there are more of these, but there's no point in you watching me type them out. 11:53.320 --> 11:55.510 So what I'll do here is just say default. 12:00.450 --> 12:03.390 And I'll make the message the same thing, so I'll copy this. 12:06.110 --> 12:08.910 And pasted in there and just so you see how this works. 12:08.930 --> 12:20.660 Let me put one more in case straight, not the error code word expired card, but it expired card there. 12:22.520 --> 12:24.470 And the message will be your card. 12:26.310 --> 12:27.150 Is expired. 12:29.820 --> 12:34.380 OK, now there are more of these, but what we're going to do here is just have those just to get this 12:34.380 --> 12:36.630 up and running MSJ return that. 12:38.720 --> 12:41.600 OK, now back up here. 12:41.910 --> 12:43.950 Our message is going to be equal to that. 12:46.500 --> 12:51.540 And we'll change this to cut that out of there and call. 12:52.650 --> 12:53.370 Instead. 12:56.740 --> 13:03.700 Card error message and we'll pass it that parameter, and then down here we'll return the necessary 13:03.700 --> 13:09.520 things for this function return nil because there is no payment intent. 13:10.120 --> 13:12.160 Our error and the message. 13:13.770 --> 13:16.390 Otherwise, we got something useful back. 13:16.410 --> 13:21.320 We'll return the API for payment intent nil, because there is no error. 13:21.480 --> 13:22.830 I don't empty string for the message. 13:24.460 --> 13:28.390 OK, so that is how we're going to get a payment intent. 13:28.410 --> 13:29.340 Now, create payment. 13:29.340 --> 13:32.520 Intent is perhaps what's wrong with this. 13:34.240 --> 13:36.100 Error should be returned as the last argument. 13:36.280 --> 13:36.710 You're right. 13:36.740 --> 13:48.520 I mean, move that will remove that and put this here and then down here will change this to return 13:48.520 --> 13:49.480 the error last. 13:50.800 --> 13:55.350 And we'll get rid of this string here and put it right here, OK? 13:55.610 --> 13:57.220 That's semantically correct. 13:57.220 --> 13:58.460 That's better idiomatic. 13:58.480 --> 13:59.630 Go, go code. 14:00.550 --> 14:02.350 So we have this payment intent function. 14:02.440 --> 14:04.150 That's not really a meaningful name. 14:04.150 --> 14:07.450 I mean, it is if I'm working with Stripe, but let's make an alias to this. 14:07.480 --> 14:09.220 Let's create right above it. 14:09.250 --> 14:11.680 Another function and it will be func. 14:12.430 --> 14:21.070 See KO'd again and I'll call this one church, which is a much more meaningful church. 14:21.640 --> 14:32.260 So currency will be, string amount will be, and it will return a pointer to a stripe payment intent 14:34.510 --> 14:37.420 and add a string and potentially there. 14:37.720 --> 14:45.280 And all we'll do is just call return, don't create payment intent and we'll pass that our currency 14:46.030 --> 14:48.340 and our amount there. 14:48.940 --> 14:52.380 Now that's a little nicer and that's absolutely not necessary. 14:52.390 --> 14:57.910 You can call create payment intent directly, but charge seems to me to be a more meaningful function 14:57.910 --> 14:58.180 name. 14:58.390 --> 15:01.420 I use create criminal intent here because I'm using straight. 15:01.810 --> 15:08.110 But at some point down the road I might want to modify my code base to work with, say, Stripe and 15:08.110 --> 15:11.320 PayPal and maybe a Canadian bank like Moneris. 15:11.920 --> 15:17.530 And at that point I could use the repository pattern and have charge refund, partial refund, whatever 15:17.530 --> 15:18.160 those may be. 15:18.490 --> 15:22.000 And those would be meaningful names regardless as to the payment date we were using. 15:22.120 --> 15:25.450 Of course, I'm not going to do that in this course, but it's the sort of thing you may want to keep 15:25.450 --> 15:29.090 in mind as you build any payments solution and go. 15:29.980 --> 15:31.610 So now I have this built up. 15:31.630 --> 15:35.320 OK, so in the next lecture we'll actually hook it up to our front end. 15:35.320 --> 15:37.720 We'll build the will, build the back end, will build the front end. 15:37.720 --> 15:41.140 We'll get both running and we'll try running a test transaction. 15:41.140 --> 15:44.340 And that might take a couple of lectures, but we'll get started in the next one.