Back to work

Invoice to Payment · Billing automation

Invoices sent, payments matched, late payers chased

A small studio’s billing, run automatically, without a spreadsheet.

What can’t go wrong

  • 1 emailEach invoice emailed once
    How we know
    If the same request arrives twice, or the email service is slow to reply, the customer still gets exactly one email. Both cases were checked.
  • 1 refundNo refund paid twice
    How we know
    Two refund requests for the same invoice, sent at the same moment, gave one refund and one refusal. A refund can never be more than what was paid.
  • 1 per stepOne reminder per late step
    How we know
    Late invoices are set to get a reminder at 3, 10 and 21 days. In the end-to-end runs the 3 and 10 day reminders went out once each, and paid, refunded and cancelled invoices got none.
  • 4 casesUnclear payments go to a person
    How we know
    Paying too much, paying in the wrong currency, an unknown reference or paying a cancelled invoice: each one is set aside for review and the team gets an alert. Nothing is guessed.

How a bill moves

  1. A request comes inChecked before anything happens
  2. The invoice goes outNumbered, made into a PDF, emailed
  3. The payment is matchedReceipt once it is paid in full
  4. Late ones are chasedReminders at 3, 10 and 21 days

How it works, in detail

For technical reviewers: the workflows, architecture and automated checks.

TriggerStepAnswer to senderRedrawn from the build’s layout file

Verified on real n8n

31 of 31 scenarios passed on n8n 2.42.6.

  • 31/31End-to-end scenarios passed
    Details
    Run on n8n 2.42.6 on 15 Nov 2026. Each scenario posts signed events to the production webhooks and checks the answer, every call, row, stored PDF, email and alert. 109.8 s of scenarios.
  • 72Unit checks passed
    Details
    42 workflow unit checks and 30 unit checks of the shared kit, run without n8n. Totals, tax, matching, transitions and reminder steps are plain functions inlined into the Code nodes.
  • 155Nodes in three workflows
    Details
    Issue invoices 65, Match payments 42, Chase overdue invoices 48. The workflow files are generated from source and checked for staleness by a unit check.
  • 0Validator errors
    Details
    n8n-mcp validate_workflow in the runtime and strict profiles on all 4 workflows, the shared error workflow included. Strict-profile warnings were reviewed and accepted.
  • 3 × 5 sRetries on every call
    Details
    Every HTTP call retries 3 times, 5 seconds apart. Every write carries an Idempotency-Key, so a retry after a timeout gets the first answer instead of a second write.

Inside the automation · 155 nodes

Three workflows, one invoice

Scroll the steps. Tap any node for its job, retries and what it sends.

  1. Issue invoices1 / 8

    401

    Signature before anything else

    How it works
    An HMAC over the timestamp and the raw body, compared in constant time, with a 300-second window. A wrong, missing, tampered or 10-minute-old signature got 401 and no call of any kind.
  2. Issue invoices2 / 8

    400

    Broken events become dead letters

    How it works
    A broken payload gets 400 with its reasons and is stored once as a dead letter. When the dead letter could not be stored on 4 tries, the sender still got 400 and #crit-alerts got the payload hash.
  3. Issue invoices3 / 8

    409 · 200

    One claim per event

    How it works
    A redelivery while the first run is still going gets 409 with Retry-After: 30. A redelivery after it gets 200 duplicate after exactly one call.
  4. Issue invoices4 / 8

    422

    Checks before a number

    How it works
    Client, currency, dates, tax codes and totals are checked before the sequence is touched, so a refused request leaves no gap. Good, refused, good gave CRIT-2026-0001 and CRIT-2026-0002.
  5. Issue invoices5 / 8

    0001

    Same event, same number

    How it works
    The sequence returns the same value for the same event id. Storage failing on all 4 tries and the event redelivered still gave one number, CRIT-2026-0001, with the sequence called once.
  6. Issue invoices6 / 8

    SHA-256

    One PDF, checked twice

    How it works
    One storage key per invoice. The hash is checked when the PDF is stored and again when it is read back, so the invoice, the stored object and the email attachment carry the same SHA-256.
  7. Issue invoices7 / 8

    23 h

    One email per event

    How it works
    The send is stamped first and carries an Idempotency-Key. A send whose answer was lost is resent with the same key only within 23 hours; after that the run stops with the ids and a person checks.
  8. Issue invoices8 / 8

    money_seq

    One money write at a time

    How it works
    Void only while nothing is paid; refund only up to what was paid. A write lands only if the invoice's money_seq is unchanged; the run that loses reads again and decides again, at most 5 passes.
  9. Match payments1 / 3

    9

    Nine outcomes, one decision

    How it works
    Matched by reference against the payments and refunds before it: exact, partial, completes, overpaid, already paid, wrong currency, unknown reference, void invoice or after a refund. The decision is stored with the payment.
  10. Match payments2 / 3

    1

    One receipt, on full payment

    How it works
    A receipt goes out only when this payment brings the invoice to paid in full. Two full payments posted at the same moment gave one receipt and one review item.
  11. Match payments3 / 3

    4

    Ambiguity goes to a person

    How it works
    Over-payment, wrong currency, unknown reference and a payment on a void invoice each became a review item and a #crit-billing message with ids and amounts only. None is guessed.
  12. Chase overdue1 / 3

    09:00

    Two schedules, one clock

    How it works
    Every day at 09:00 Manila time and every hour at minute 20, or via a signed sweep request. The clock is read once per run, and sweeps can be triggered directly by signed requests.
  13. Chase overdue2 / 3

    3 · 10 · 21

    Reminders by days overdue

    How it works
    One claim, one email and one row per invoice and step, so a step is never sent twice. In the end-to-end runs, 3 and 10 days went out once each; paid, refunded and void invoices got none.
  14. Chase overdue3 / 3

    30 min

    Stuck work reported once

    How it works
    An invoice still not delivered after 30 minutes, or a number taken with no invoice row, goes to #crit-billing once. A 5-minute-old invoice was not reported; the next run reported nothing new.

Watch a recorded run travel

Six scenarios from the end-to-end run. Every path is built from the logged calls.

One number, one PDF, one email

What was checked
CRIT-2026-0001: subtotal PHP 116,250.50, VAT 12% PHP 13,800.00, total PHP 130,050.50, answered in 127 ms. The invoice's hash, the stored PDF and the email attachment match.
Requestanswered202
37/37+228 ms
  1. Receive billing event
  2. Verify signature
  3. Route on signature
  4. Validate event shape
  5. Route by event shape
  6. Prepare event claim
  7. Claim eventclaimed200+0 ms
  8. Read claim result
  9. Route on claim
  10. Route by event type
  11. Fetch billing config200+29 ms
  12. Find client200+37 ms
  13. Find invoice for event200+47 ms
  14. Decide invoice plan
  15. Route invoice plan
  16. Respond 202 acceptedanswer202
  17. Route on existing invoice
  18. Claim invoice numbernumber allocated200+70 ms
  19. Prepare invoice row
  20. Store invoice row201+87 ms
  21. Read stored invoice
  22. Check delivery progress
  23. Route delivery step
  24. Create invoice PDF200+116 ms
  25. Hash invoice PDF
  26. Store invoice PDFstored200+143 ms
  27. Prepare PDF record
  28. Record PDF on invoice200+159 ms
  29. Download stored PDF200+166 ms
  30. Prepare invoice email
  31. Record email start200+184 ms
  32. Send invoice email200+200 ms
  33. Prepare delivery record
  34. Record delivery200+214 ms
  35. Prepare delivered result
  36. Mark event done200+228 ms
  37. Summarize run

Build notes

What came up during the build, and what changed because of it.

A race the review found

What happened
Two refunds for one invoice, posted together, both read an empty refund list and both reached the processor. Two payments posted together were both recorded as exact. New checks reproduced each case against the previous build first. The fix is a counter on the invoice, money_seq: a payment row, refund row or void is written only while it still has the value read before deciding, and the run that loses reads again and decides again.

A lost guard answers 200, not 409

Why
n8n retries any HTTP error 3 times with the same body and key, which would only repeat the stale request. A lost guard is answered 200 with outcome conflict, and the guarded write's Idempotency-Key names the counter it was decided against.

No running totals for money

Why
An invoice row with a paid total updated by each payment would let two payments arriving together both read the old total. Paid and refunded amounts are always summed from the payment and refund rows, in integer minor units, and each payment's decision is stored on its own row.

A reminder whose run died is reviewed

Why
The email API keeps an Idempotency-Key for 24 hours and the next sweep is a day later, so a resend could reach the client twice. A claim taken over from a failed or stuck run becomes a review item and an alert instead.

n8n refuses a body that is not JSON

What happened
The first end-to-end run passed 24 of 25. n8n's webhook parses JSON bodies itself and answered 422 before any node ran, so the check now asserts that. The workflow's own path for unparsable bytes is covered by unit checks only.

Late resends are refused

What happens
Invoice emails are stamped as started right before the send; receipts and refunds use their row's creation time. Within 23 hours a replay reuses the same key. After that the run stops into the error workflow with the ids, until a person records the answer or voids the invoice.

Code, build log and full results

Next

UGC Ad Creator

UGC Ad Creator