Parsware
All articles

BS Lang

Call an outside service

callService lets a server-side script post to another system, like a payment provider. The script never holds the address, only a service name. Each environment sets where that name points, so Test can use a sandbox and Production the real thing, and an environment that sets nothing reaches nothing. We took payments in a sample order app, and saw an answer, a refusal and a missing setting.

The Order payment script calling callService with par_payment_service and a JSON body, then checking answer.ok, next to the title "Call an outside service"

A plugin step, a Custom API or a scheduled job can call another system with callService. It posts a body to a service and gives you back the answer. We used it to take a payment before an order is saved.

We used the Calling out: a payment service and an email sample. Import it from samples/solutions/business-rules-outbound-calls. We imported it into our Test environment, because it has its own Order table.

A service name, not an address

Here is the call in the sample:

var answer = callService('par_payment_service', body);

The first argument isn't a URL. It's the name of an environment variable, and the variable holds the address. The solution declares the variable; each environment sets its own value:

The Environment variables screen of the Calling out solution in the Test environment: Mail service, par_mail_service, Text, Not set; and Payment service, par_payment_service, Text, Not set

Just after import, both say Not set. A solution carries the declaration, never the value, because the value is different in every environment: a payment sandbox in Test, the real provider in Production.

This is also what keeps outbound calls safe. A script can only reach an address that an administrator put into a variable in this environment. There's no function that takes a URL, so a script can't be pointed at an internal server by whoever writes it.

With nothing set, nothing is called

We added an order, ORD-1001 for 250, set Status to Paying and saved:

The New Order form with ORD-1001, 250, dana@example.com and Paying, and a red message: The environment variable 'par_payment_service' has no value in this environment, so there is nowhere to call. An administrator sets it under Environment Variables.

The save is refused, and the message names the variable and says who can fix it.

Set the address

Select the variable and choose Set value:

The Set value dialog for Payment service: This value belongs to this environment alone. It is never exported with the solution, and importing the solution again will not overwrite it. The Value box holds http://localhost:5990/pay

The value belongs to this environment only. It isn't exported with the solution, and importing the solution again won't overwrite it.

For this article we pointed it at a small stand-in provider running on our machine. It answers 200 with a payment reference, or 402 for a declined card, depending on the address.

Taking the payment

We saved ORD-1001 as Paying again:

The saved order ORD-1001: Status Paid, Payment reference PAY-338481, and Confirmation email: Not sent, because par_mail_service has no value

The step called the service, and the order came back Paid, with the reference the provider sent. The provider received:

{"reference":"ORD-1001","amount":250}

(Confirmation email says Not sent, because we haven't set the mail service yet. That's the next article.)

The script

The code is in a solution script, Order payment:

The Order payment script in the editor: func takePayment() checks the status is Paying and there is no reference yet, builds a JSON body with a template, calls callService('par_payment_service', body), then if answer.ok sets par_payment_reference to answer.body and par_status to Paid, else throwValidationError with the status

callService gives back an object:

Property What it holds
ok true for a 2xx answer
status the HTTP status, like 200 or 402
body what the service sent back, as text
truncated true if the answer was too long and was cut

The body you send is text. Here it's JSON, built with a template so the amount goes in as a number.

The payment function, as text

The Order payment script from the picture above, as the sample ships it:

func takePayment() {
  if (getValue('par_status') == 'Paying' && isEmpty(getValue('par_payment_reference'))) {
    var body = `{"reference":"${getValue('par_name')}","amount":${getValue('par_amount')}}`;
    var answer = callService('par_payment_service', body);

    // A status the provider chose is an answer, not a fault.
    if (answer.ok) {
      setValue('par_payment_reference', answer.body);
      setValue('par_status', 'Paid');
    } else {
      throwValidationError(`The payment service refused this order (${answer.status}).`);
    }
  }
}

export takePayment, confirmByEmail;

Each of the two steps is then two lines: import 'par_order_payment'; and takePayment();. Read the first line of the function carefully, because it is what makes the payment safe to retry.

Never charge twice

takePayment only calls the provider when the order is Paying and has no payment reference yet. Once a payment succeeds, the reference is set and the status becomes Paid, so saving the order again, editing its notes, or an import that touches it, never charges a second time.

This is the one habit every outbound call that changes something needs: a condition that is only true before the call has worked. A reference, a sent date, a status that moves on. Without it, any later save of the record repeats the call. It also helps to send the provider something it can recognise as the same request, as the sample does with the order's own reference, so a well-behaved provider can refuse a duplicate on its side too.

Sending an object

The sample builds its body as text with a template. callService also takes an object or a list and sends it as JSON, which avoids getting the quotes and commas right by hand:

var answer = callService('par_payment_service', {
  reference: getValue('par_name'),
  amount: getValue('par_amount')
});

What comes back in answer.body is the provider's answer as text. In the sample the provider answers with just the payment reference, so the script stores the text as it is. A provider that answers with a longer document leaves the script to pick out what it needs; keep what you store short and readable.

Before or after the save?

Where a call goes depends on what should happen if it fails.

The call Put it Because
Must succeed for the save to make sense, such as taking a payment Before the write (PreOperation) A failed call refuses the save, so nothing half-true is stored
Reports something that has already happened, such as a confirmation After the write (PostOperation) The save stands either way; record the outcome on the record
Slow, or not needed by the person waiting After the write, run in the background The save doesn't wait for it

The sample does the first and the second: the payment before the write, the confirmation email after it. A call made before the write holds up the save while it waits, and gets ten seconds at most, so keep those calls to the ones that truly must decide the save. Plugin steps: before and after a save covers the stages, and When background work fails what happens to a step that runs in the background and fails.

Setting the address in each environment

Because the address is an environment variable, each environment points at its own service:

Environment par_payment_service
Development A stand-in on a developer's machine, or a provider's sandbox
Test The provider's sandbox, with test cards
Production The provider's live address

Importing a new version of the solution never overwrites these values, so a release can't point Production at a sandbox by accident. Environment variables shows the screen, and Environment variables across environments how a solution declares them.

Questions that come up

Can a script call any address it likes? No. It names a service, and only an administrator decides, per environment, where that name points. That is what stops a script from being pointed at an internal system by whoever writes it.

Can a form rule call a service? No. Outbound calls run on the server, in plugin steps, Custom APIs and scheduled jobs, where the address and the call can't be seen or changed from a browser.

What does the provider see as the caller? A call from the platform's server, with the body the script sent. Anything the provider needs to authenticate the call belongs in its address or in what the provider issues for it, configured in the environment, not written into the script.

Before the write, on purpose

The payment is taken in a PreOperation step: before the order is written, inside the same save. That matters. Taking the money and recording that it was taken are one act. If the call fails, the save is refused and nothing is stored. A step that saved first and paid after could leave an order marked Paid with no payment behind it.

The cost is that the save waits for the provider. A call gets ten seconds, and no more.

The sample has two such steps, one when an order is created and one when it's changed, because a plugin step watches one kind of save. Both import takePayment from the script:

The plugin steps of the solution's 1.1.0 version: Take payment before saving a new order (Create, Before the write) and Email the confirmation for a new paid order (Create, After the write)

An answer isn't a failure

We pointed the variable at the address that declines, and saved a new order, ORD-1002:

The New Order form for ORD-1002 with Status Paying and the message The payment service refused this order (402).

A 402 is an answer: the provider said no. callService doesn't stop the script; it hands the answer back, and the script decides. This one refuses the save with the status in the message, and ORD-1002 isn't stored.

Only when there's no answer at all does callService stop the script by itself: no variable, nobody listening, or no answer in ten seconds. A script can't do anything useful with those.

Then we set the variable to not-a-url:

The same form with the message The endpoint configured for 'par_payment_service' is not an absolute http or https address.

A different message, because it's a different problem. A declined card is the customer's; a missing or broken address is for whoever runs the environment.

Two bugs we found and fixed

Writing this article, we found two problems in the sample itself:

  • A new order saved as Paying was never charged. The sample only had the step for changed orders, but the form's own hint said to set Paying and save. So the first thing you'd try stored an unpaid order and said nothing. The sample now has a step for new orders too, and both import the same script, which is why the code above is a solution script.
  • A failed email made a saved order look unsaved. That one's in the next article.

Both are fixed in version 1.1.0 of the sample, with tests.

Where it works

callService is available in plugin steps, Custom APIs and scheduled jobs, which run on the server. It isn't in form rules or command buttons, which run in the browser, or in process steps yet.

Try it

Import the sample, set the payment service to any address that answers 200, and save an order as Paying. Then point it somewhere that answers 402 and try again.

Next: send an email from a script, with sendEmail.