So let's write the necessary code to call our MICRA service, and of course, the first thing we have
to do is to figure out where we call our MICRA service.
Do we do it in our front end and our back end?
And it turns out in our case, it's from the front end because here in my rootstock go file in the CMD
Web folder online twenty eight, this is the handler payment succeeded.
That actually creates the transaction, creates the customer, creates the order, it gives us all the
information we need in order to generate an invoice.
So let's go look at that function.
So payments exceed everything I need.
Should be here.
So if I go all the way down to the bottom right before I write this information to the session, the
receipt, actually, at this point I should call the microcircuits.
But of course, that MICRA service expects to receive a Jason payload in a specific format.
And none of the types that I have right now actually include that format.
So let's go over to invoice handlers.
And this is the type I'm talking about type order.
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
this function.
And you can put it wherever you want, but I'm going to put it right here and I'm going to rename it
from Order to Invoice because I already have an order.
So I'll call this invoice.
And that's everything in the correct format, all the fields that we need.
So let's go back down here where we're going to call the micro service.
So the first thing I'll do is create a variable called EMV for invoice, and that will be assigned the
value of the type we just created invoice.
We'll just populate its fields.
Now, the first thing we need, of course, is the idea, which is the order ID, but we don't have
an order ready.
We have order, order, ID.
But it's actually set to zero because the order we create doesn't include that ID.
It is not assigned an ID until Save Order is called.
But as you can see here, that returns and into an error and the agent is in fact the new ID.
So I need to do is change this to a variable order ID and then change that to an assignment operator.
And down here we have order ID populated with the correct value.
The next thing we want is the amount and that we do have its order that amount.
And the next thing we need is the product name.
Now, in our case, we're only selling one product and we don't actually pass the name of the product
to this handler.
But that's OK.
We just have one.
So for our case right now, we'll make a widget.
We could, in fact, look up the name from the database.
But in our case, we're selling one product and it's trivial to look it up from the database.
So I'll leave that as an exercise for you.
So we need the product quantity.
We need and want that we have it in order, not quantity.
Next, we need the first name of the customer and we got that as a post to this handler in tax and data.
So first name and we need the last name, which will be tax and data dot last name.
And we need the email again to send data, dot email and finally create it up.
And that is time dot now.
So the next thing to do is to actually call our micro service.
Now I could if I wanted to put all that logic right in here, but I know I have another place in this
code where I'm going to actually have to generate an invoice as well.
And that's when someone buys our bronze plan, the subscription.
So what I'll do instead is come down here and create a new function with the receiver of app pointer
to application and I'll just call it say call invoice, micro
in color, whatever you want, of course.
And it will take one parameter I envy of of type invoice, the type we just created, and it potentially
returns an error.
Let's give ourselves some room here.
So we need a new URL to call and I'm going to hardcoded.
Of course you can set this up as a command line parameter or an invasion environment variable or whatever
you wish.
But in my case I'm just going to hardcoded for now.
HDB colon slash slash localhost court, five thousand slash invoice, slash, create and send.
OK, and of course you probably don't want a hard code that in production, but I just want to get this
running right now and it's trivial for you to set it up as an environment variable or read it as a command
line parameter.
Now we'll create a variable called out and I'll check for an arrow and I will take the invoice I received
as a parameter and convert it to Jason.
Jason Marshall indent.
And I'm just doing indent in case I want to look at it.
I'm going to Marshall.
I envy with no prefix and I'll put a tab character.
In as the spacer, I'll check for now, if there is not equal to nil,
then return here.
Now, I'll create a request that's a variable and an error on going, I'm going to check for an error.
Those are assigned the value of from the HTP package.
I'll create a new request and that's going to be a post request, which is what our microsurgeons expects
to receive.
Posting to the URL we defined right up there in line one ninety.
And we need to pass out our Jason.
But of course, that needs to be in a specific format.
So we convert it to that format by calling bytes dot new buffer
and we check for an error.
If error is not equal to nil, return the error.
Assuming we get past that, let's set a tender request header dart set and we just need to set content
type and I have too many parentheses.
Let's try that again.
Content dash type two application slash Jason.
Now we create a client, which is a sign, the type of a reference to HTP, new client, her client.
And we look for a response, check for an error, and we call client, not do a request, so that's
actually going to make the call to our myCar service check for an error.
If error is not equal to no, return the error.
Now, assuming that works, we may as well read the body, so we'll say defer RESPA body close so we
don't have a memory leak and we'll just print it out to the console.
So after info, log, dot, print line,
rest up body
and we return now.
So there's a function I can call.
Let's scroll up now to where we actually created our invoice.
Let's try it.
Error equals app, call invoice micro and pass it on.
And if error is not equal to nil.
I'm not going to actually stop it at this point because I want to complete the transaction.
But I'll look at the log file and make sure it's working.
So all I'll do instead is set up, er log the print line, whatever the error might be, if there is
one, if we have a typo or something.
So let's give this a try.
Let's open our terminal window
and stop our application, make stop and I'll make start to start at the back end, the front end and
the micro service.
Everything is running.
Let's go to our Web browser and I'll go to products by one widget and I'll look in mail trap, make
sure that it's empty.
It is OK.
So let's buy a widget.
So this will be Selina, Kyle, Selina at I Love Cats Dotcom with the name Selina Kyle and our standard
for two credit card number
zero one thirty three.
And any CBC charge the card so we should get a receipt page.
So that was good.
Now let's go back and look at our log file.
So it sent the mail.
Let's go back here and let's look and mail trap and there is my invoice, there is the message and let's
look at the attachment 26 PDF.
So let's open it and I'll open it with Firefox.
Attention, Selina, Kyle, Selina and I love cats, dotcom, the date, description, quantity and
total, so everything works as expected.
Perfect.
So we still have to do, of course, the part where somebody comes here and says, I want to buy a subscription.
We want that invoice mailed as well.
But I'll talk about that in the next election.