WEBVTT 00:01.550 --> 00:06.050 So let's write the necessary code to call our MICRA service, and of course, the first thing we have 00:06.050 --> 00:08.420 to do is to figure out where we call our MICRA service. 00:08.660 --> 00:10.640 Do we do it in our front end and our back end? 00:11.270 --> 00:17.420 And it turns out in our case, it's from the front end because here in my rootstock go file in the CMD 00:17.420 --> 00:22.850 Web folder online twenty eight, this is the handler payment succeeded. 00:23.330 --> 00:28.070 That actually creates the transaction, creates the customer, creates the order, it gives us all the 00:28.070 --> 00:31.600 information we need in order to generate an invoice. 00:31.640 --> 00:33.590 So let's go look at that function. 00:36.290 --> 00:38.840 So payments exceed everything I need. 00:39.020 --> 00:40.020 Should be here. 00:40.130 --> 00:44.930 So if I go all the way down to the bottom right before I write this information to the session, the 00:44.930 --> 00:49.970 receipt, actually, at this point I should call the microcircuits. 00:52.370 --> 00:57.380 But of course, that MICRA service expects to receive a Jason payload in a specific format. 00:57.380 --> 01:01.150 And none of the types that I have right now actually include that format. 01:01.640 --> 01:03.710 So let's go over to invoice handlers. 01:04.190 --> 01:07.130 And this is the type I'm talking about type order. 01:07.310 --> 01:13.880 So I'll copy that and I'll go back to handlers that go in the CMD Web folder and go back up just before 01:13.880 --> 01:14.630 this function. 01:14.630 --> 01:18.500 And you can put it wherever you want, but I'm going to put it right here and I'm going to rename it 01:18.500 --> 01:21.350 from Order to Invoice because I already have an order. 01:22.310 --> 01:23.510 So I'll call this invoice. 01:24.710 --> 01:27.590 And that's everything in the correct format, all the fields that we need. 01:28.430 --> 01:32.000 So let's go back down here where we're going to call the micro service. 01:32.000 --> 01:36.650 So the first thing I'll do is create a variable called EMV for invoice, and that will be assigned the 01:36.650 --> 01:39.500 value of the type we just created invoice. 01:39.950 --> 01:41.190 We'll just populate its fields. 01:42.050 --> 01:48.110 Now, the first thing we need, of course, is the idea, which is the order ID, but we don't have 01:48.110 --> 01:48.770 an order ready. 01:49.040 --> 01:50.870 We have order, order, ID. 01:50.870 --> 01:55.700 But it's actually set to zero because the order we create doesn't include that ID. 01:55.820 --> 01:58.760 It is not assigned an ID until Save Order is called. 01:59.210 --> 02:04.580 But as you can see here, that returns and into an error and the agent is in fact the new ID. 02:05.300 --> 02:11.750 So I need to do is change this to a variable order ID and then change that to an assignment operator. 02:13.100 --> 02:17.840 And down here we have order ID populated with the correct value. 02:18.650 --> 02:24.560 The next thing we want is the amount and that we do have its order that amount. 02:26.660 --> 02:29.450 And the next thing we need is the product name. 02:29.480 --> 02:33.830 Now, in our case, we're only selling one product and we don't actually pass the name of the product 02:33.950 --> 02:34.970 to this handler. 02:35.120 --> 02:35.780 But that's OK. 02:35.780 --> 02:36.510 We just have one. 02:36.510 --> 02:39.110 So for our case right now, we'll make a widget. 02:39.860 --> 02:42.440 We could, in fact, look up the name from the database. 02:42.440 --> 02:46.460 But in our case, we're selling one product and it's trivial to look it up from the database. 02:46.460 --> 02:48.220 So I'll leave that as an exercise for you. 02:48.740 --> 02:50.300 So we need the product quantity. 02:50.300 --> 02:53.120 We need and want that we have it in order, not quantity. 02:56.180 --> 03:02.300 Next, we need the first name of the customer and we got that as a post to this handler in tax and data. 03:03.140 --> 03:12.470 So first name and we need the last name, which will be tax and data dot last name. 03:13.760 --> 03:20.870 And we need the email again to send data, dot email and finally create it up. 03:21.950 --> 03:23.630 And that is time dot now. 03:24.440 --> 03:27.320 So the next thing to do is to actually call our micro service. 03:27.320 --> 03:32.870 Now I could if I wanted to put all that logic right in here, but I know I have another place in this 03:32.870 --> 03:36.410 code where I'm going to actually have to generate an invoice as well. 03:36.410 --> 03:39.230 And that's when someone buys our bronze plan, the subscription. 03:39.590 --> 03:44.900 So what I'll do instead is come down here and create a new function with the receiver of app pointer 03:44.930 --> 03:50.420 to application and I'll just call it say call invoice, micro 03:54.020 --> 03:55.400 in color, whatever you want, of course. 03:55.400 --> 04:01.340 And it will take one parameter I envy of of type invoice, the type we just created, and it potentially 04:01.340 --> 04:02.180 returns an error. 04:03.710 --> 04:05.240 Let's give ourselves some room here. 04:06.740 --> 04:09.380 So we need a new URL to call and I'm going to hardcoded. 04:09.560 --> 04:14.660 Of course you can set this up as a command line parameter or an invasion environment variable or whatever 04:14.660 --> 04:15.140 you wish. 04:15.140 --> 04:17.270 But in my case I'm just going to hardcoded for now. 04:17.900 --> 04:28.340 HDB colon slash slash localhost court, five thousand slash invoice, slash, create and send. 04:28.700 --> 04:33.830 OK, and of course you probably don't want a hard code that in production, but I just want to get this 04:33.830 --> 04:38.810 running right now and it's trivial for you to set it up as an environment variable or read it as a command 04:38.810 --> 04:39.620 line parameter. 04:41.210 --> 04:47.690 Now we'll create a variable called out and I'll check for an arrow and I will take the invoice I received 04:47.690 --> 04:49.550 as a parameter and convert it to Jason. 04:50.540 --> 04:52.520 Jason Marshall indent. 04:53.060 --> 04:55.100 And I'm just doing indent in case I want to look at it. 04:55.670 --> 04:56.360 I'm going to Marshall. 04:56.360 --> 05:00.980 I envy with no prefix and I'll put a tab character. 05:01.590 --> 05:08.520 In as the spacer, I'll check for now, if there is not equal to nil, 05:11.550 --> 05:12.330 then return here. 05:15.800 --> 05:21.310 Now, I'll create a request that's a variable and an error on going, I'm going to check for an error. 05:21.530 --> 05:24.800 Those are assigned the value of from the HTP package. 05:25.050 --> 05:30.680 I'll create a new request and that's going to be a post request, which is what our microsurgeons expects 05:30.680 --> 05:31.340 to receive. 05:31.730 --> 05:35.030 Posting to the URL we defined right up there in line one ninety. 05:35.600 --> 05:38.440 And we need to pass out our Jason. 05:38.450 --> 05:40.640 But of course, that needs to be in a specific format. 05:40.640 --> 05:44.660 So we convert it to that format by calling bytes dot new buffer 05:47.470 --> 05:49.130 and we check for an error. 05:49.430 --> 05:53.270 If error is not equal to nil, return the error. 05:57.210 --> 06:05.220 Assuming we get past that, let's set a tender request header dart set and we just need to set content 06:05.220 --> 06:06.980 type and I have too many parentheses. 06:06.990 --> 06:07.830 Let's try that again. 06:08.670 --> 06:13.970 Content dash type two application slash Jason. 06:17.640 --> 06:25.950 Now we create a client, which is a sign, the type of a reference to HTP, new client, her client. 06:30.560 --> 06:38.720 And we look for a response, check for an error, and we call client, not do a request, so that's 06:38.720 --> 06:43.250 actually going to make the call to our myCar service check for an error. 06:43.250 --> 06:46.400 If error is not equal to no, return the error. 06:50.300 --> 07:00.050 Now, assuming that works, we may as well read the body, so we'll say defer RESPA body close so we 07:00.050 --> 07:03.160 don't have a memory leak and we'll just print it out to the console. 07:03.170 --> 07:06.680 So after info, log, dot, print line, 07:09.470 --> 07:10.460 rest up body 07:13.460 --> 07:14.240 and we return now. 07:18.500 --> 07:20.030 So there's a function I can call. 07:23.680 --> 07:27.420 Let's scroll up now to where we actually created our invoice. 07:27.590 --> 07:28.060 Let's try it. 07:29.110 --> 07:34.750 Error equals app, call invoice micro and pass it on. 07:34.840 --> 07:37.720 And if error is not equal to nil. 07:40.180 --> 07:44.380 I'm not going to actually stop it at this point because I want to complete the transaction. 07:44.650 --> 07:46.760 But I'll look at the log file and make sure it's working. 07:46.780 --> 07:54.310 So all I'll do instead is set up, er log the print line, whatever the error might be, if there is 07:54.310 --> 07:55.820 one, if we have a typo or something. 07:56.530 --> 07:57.750 So let's give this a try. 07:58.030 --> 07:59.770 Let's open our terminal window 08:02.800 --> 08:10.060 and stop our application, make stop and I'll make start to start at the back end, the front end and 08:10.060 --> 08:10.990 the micro service. 08:12.720 --> 08:13.450 Everything is running. 08:13.530 --> 08:21.390 Let's go to our Web browser and I'll go to products by one widget and I'll look in mail trap, make 08:21.390 --> 08:22.170 sure that it's empty. 08:22.180 --> 08:23.200 It is OK. 08:23.760 --> 08:24.740 So let's buy a widget. 08:24.750 --> 08:38.910 So this will be Selina, Kyle, Selina at I Love Cats Dotcom with the name Selina Kyle and our standard 08:38.910 --> 08:40.230 for two credit card number 08:43.380 --> 08:45.300 zero one thirty three. 08:45.300 --> 08:52.850 And any CBC charge the card so we should get a receipt page. 08:52.860 --> 08:53.550 So that was good. 08:54.390 --> 08:56.430 Now let's go back and look at our log file. 08:58.880 --> 08:59.950 So it sent the mail. 09:00.170 --> 09:09.680 Let's go back here and let's look and mail trap and there is my invoice, there is the message and let's 09:09.680 --> 09:12.880 look at the attachment 26 PDF. 09:12.890 --> 09:16.160 So let's open it and I'll open it with Firefox. 09:18.140 --> 09:24.260 Attention, Selina, Kyle, Selina and I love cats, dotcom, the date, description, quantity and 09:24.260 --> 09:26.960 total, so everything works as expected. 09:27.260 --> 09:27.860 Perfect. 09:28.280 --> 09:34.250 So we still have to do, of course, the part where somebody comes here and says, I want to buy a subscription. 09:34.250 --> 09:36.140 We want that invoice mailed as well. 09:36.650 --> 09:38.590 But I'll talk about that in the next election.