Dispatch a POS webhook to sub-workflows
This workflow receives the events sent by a point-of-sale software through an API route, identifies the resource targeted by the request, then delegates the processing to a dedicated sub-workflow (order, payment or refund).
Loading the diagram…
The need
A point-of-sale software sends its events (orders, payments, refunds) through an API route. Each event must be dispatched to the dedicated sub-workflow based on the request method and route, so that each type of event is handled by a dedicated process.
What the workflow does
- POS webhook — the API route trigger receives the notifications from the POS software. The path
/v1/pos/*pathcaptures the resource (orders,payments,refunds) inincomingRequest.params.path; the HTTP method arrives inincomingRequest.methodand the request body indata. - Identify the resource — the Code node combines the method and the path into
context.variables.route(e.g.POST orders). - Request resource — the Switch node routes
{{ context.variables.route }}to the matching branch. - Sub-workflow: register the order — for
POST orders, the Call a workflow node runs the POS — Register an order sub-workflow, inheriting the context and trigger data. - Sub-workflow: process the payment — for
POST payments, the POS — Process a payment sub-workflow takes over. - Sub-workflow: handle the refund — for
POST refunds, the POS — Handle a refund sub-workflow is executed. - Reply 204 (not handled) — any other method or resource receives an HTTP response
204 No Contentthrough the default branch of the switch.
What you need
- An API route trigger that receives the notifications from the POS software (path
/v1/pos/*path). - The Code node, the Switch node, the Call a workflow node and the HTTP Response node.
- Dedicated sub-workflows for orders, payments and refunds.
Construction
Copy the workflow definition and paste it into the editor (the Trigger node is recreated automatically, it is not in the snippet):
[
{
"id": "code-109",
"name": "Identify the resource",
"config": {
"type": "CODE",
"code": "const method = context.incomingRequest.method;\nconst resource = context.incomingRequest.params.path;\ncontext.variables.route = `${method} ${resource}`;\nreturn context.variables.route;"
},
"parents": [
"trigger"
],
"position": {
"x": 0,
"y": 200
},
"next": "switch-109"
},
{
"id": "switch-109",
"name": "Request resource",
"config": {
"type": "SWITCH",
"variable": "{{ context.variables.route }}",
"cases": [
{
"value": "POST orders",
"next": "wf-order-109"
},
{
"value": "POST payments",
"next": "wf-payment-109"
},
{
"value": "POST refunds",
"next": "wf-refund-109"
}
],
"noMatchBehaviour": "reply-109"
},
"parents": [
"code-109"
],
"position": {
"x": 0,
"y": 400
}
},
{
"id": "wf-order-109",
"name": "Sub-workflow: register the order",
"config": {
"type": "CALL_WORKFLOW",
"workflow": "uuid-v4-caisse-commande",
"inherits": {
"context": true,
"data": true
}
},
"parents": [
"switch-109"
],
"position": {
"x": 800,
"y": 600
}
},
{
"id": "wf-payment-109",
"name": "Sub-workflow: process the payment",
"config": {
"type": "CALL_WORKFLOW",
"workflow": "uuid-v4-caisse-paiement",
"inherits": {
"context": true,
"data": true
}
},
"parents": [
"switch-109"
],
"position": {
"x": 800,
"y": 800
}
},
{
"id": "wf-refund-109",
"name": "Sub-workflow: handle the refund",
"config": {
"type": "CALL_WORKFLOW",
"workflow": "uuid-v4-caisse-remboursement",
"inherits": {
"context": true,
"data": true
}
},
"parents": [
"switch-109"
],
"position": {
"x": 800,
"y": 1000
}
},
{
"id": "reply-109",
"name": "Reply 204 (not handled)",
"config": {
"type": "HTTP_RESPONSE",
"statusCode": "204"
},
"parents": [
"switch-109"
],
"position": {
"x": 800,
"y": 1200
}
}
]
Check that it works
- Route convention: the switch compares the
method space pathstring produced by the Code node — the case and spelling of the values (POST orders, …) must match exactly the events sent by the POS software. - Deploy the workflow (Deploy a workflow), then trigger the real scenario: every execution generates a trace you can view (View traces).
Variations
- Sub-workflow version: the Call a workflow node targets a specific version; after each change of a sub-workflow, update the selected version then re-deploy the calling workflow (see version management).
- Unknown requests: the default branch replies
204without processing; log unknown routes to spot API changes on the POS side. - Webhook response: each sub-workflow returns its result to the calling workflow; add an HTTP Response node at the end of the branch if the POS software expects an acknowledgement.
Going further
See also: Examples and integrations