By the end, you'll have a system where every website request:
- is saved to your database immediately after form submission, even if the CRM is unavailable;
- is automatically parsed: AI extracts fields from free text and suggests a price based on your price list;
- is reviewed by a manager who confirms the amount and payment details;
- turns into an invoice generated from the same record as the request;
- is closed automatically when a bank webhook about payment arrives;
- on any failure, goes into a manual review queue instead of disappearing.
Signs the pipeline is working:
- No duplicate requests appear in the CRM.
- The invoice amount matches the request amount.
- After payment, the CRM status changes to "Paid" without human involvement.
- If the webhook doesn't arrive, the request stays in the queue and the manager sees it in the "Needs attention" list.
Step 1: Form and request saving
The first loss happens right here: if after form submission you immediately send a request to the CRM and the CRM returns an error, the request is lost. The client sees "Thank you", and you see nothing.
What to do: the form saves the request to your own database (a DB table or file) before sending to the CRM.
Cost of the solution. Saving to your own DB before the CRM is a conscious trade-off: you gain reliability (the request exists even if the CRM is down), but you pay for it with the delay of an extra write and data duplication — the request lives both in your system and in the CRM, and they need to be synchronized. For a start, this is justified; if the request flow is small, the duplication is barely noticeable.
How to check: disable the CRM (or simulate a network error) and submit a request. It should appear in your database, and after the CRM is restored, it should go to it.
Where it breaks: if the database itself is unavailable, the request will be lost. Solution: a temporary file on the server or a queue (e.g., Redis). For a start, a simple table is enough.
Step 2: Parsing the request with AI
Now you have raw data: name, phone, free text "I want a website with a catalog and payment". You need to turn this into structured fields: service, amount, deadline.
What AI does: analyzes the text and suggests a service category, an amount based on your price list (if the text contains keywords), and contact details if they are not in separate fields.
What a human does: checks the suggested fields. AI can make mistakes, especially if the text is ambiguous — the manager confirms or corrects the amount and payment details.
Cost of the solution. AI parsing speeds up processing but requires manual review and costs money per call. Calculate the exact cost of one call based on your LLM's pricing: take the price per 1,000 tokens from the provider's documentation and multiply by the average token usage per request (you'll see it in the logs after the first runs). There is no universal "cost per request" figure — it depends on the model and text length.
How to check: run 10 real requests through the AI and compare with what a manager would suggest. If accuracy is below 80%, refine the prompt or add rules.
Where it breaks: AI can "hallucinate" and suggest a non-existent service. Solution: limit the list of services and verify that the AI's response falls within allowed values. LLMs have rate limits per minute — if exceeded, the request won't be processed, so you need a queue with retries.
Step 3: Invoice from the same record
When the amount is confirmed, the invoice is generated automatically from the request data — from the same record in the database, not from a separate file or Excel.
What to do: an invoice template (HTML or PDF) with request fields substituted: number, date, amount, payment details. The invoice number must be unique and serve as the key for linking to payment.
How to check: generate an invoice for a test request and make sure all fields match the request and the number is unique.
Where it breaks: if the invoice number is not unique, the bank webhook cannot unambiguously identify the payment. Solution: UUID or a counter with uniqueness validation.
Step 4: Bank webhook and closing the request
Why webhook, not API polling
API polling is regular requests "has anything new arrived?" on a schedule. A webhook is a push: the bank itself reports the payment as soon as it happens. The difference shows up in three things:
- Delay. With polling, the delay between payment and status update equals the polling interval: the less often you poll, the slower the reaction; the more often, the more load. A webhook arrives at the moment of the event, and the delay is determined only by your processing speed.
- Load. Polling wastes requests: most responses are "nothing new". A webhook makes one request per real event. With a large number of invoices, this is a noticeable difference both in load on the bank and in your API limits.
- Delivery guarantees. To be honest here: a webhook is more reliable in speed but not absolute — the bank may fail to deliver a notification or deliver it late. Therefore, a webhook does not replace reconciliation but complements it: periodic status reconciliation (via the bank's API, if available, or manually via statement) remains a safety net. Polling as the only mechanism is worse because it's slower and more expensive; a webhook as the only one is risky because delivery is not 100% guaranteed.
Endpoint and signature verification
Set up an endpoint that accepts POST requests from the bank. Processing order:
- Read the raw request body (before parsing JSON) — the signature is calculated over it.
- Verify the signature with the secret from the bank's documentation. Invalid signature — immediately respond 4xx, do not process the event. Which exact code (401 Unauthorized or 400 Bad Request) — check the bank's documentation: the code may determine whether the bank considers the delivery unsuccessful and will retry. For us, what matters is only that it's 4xx and the event is not processed.
- Extract the invoice number and amount, find the request.
- Update the status to "Paid" idempotently (see below).
- Respond 2xx quickly, and move heavy processing (notifications, CRM synchronization) to a queue.
Key point: the idempotency key is inserted in the same transaction as mark_paid. Then either both actions applied or neither — and a repeated delivery after a failure will not be discarded as a duplicate of an unprocessed event.
Separately about the cases "invoice not found" and "amount mismatch". There is a fork here, and it must be chosen consciously, because it determines who initiates the retry:
- Response 200 — the bank considers the delivery successful and will not retry. The event lives only in your manual review queue; if the queue write failed (see "Two holes" below), the event is lost irrevocably. Reprocessing is possible only if a human manually initiates it from the queue — "re-delivery from the bank" will not come by itself.
- Response 5xx — the bank will retry delivery according to its retry rules. This gives an automatic second chance, but you pay for it by the bank retrying even obviously "bad" events (e.g., with a typo in the amount) until you handle them manually — and after handling, the retry will still come and must be processed correctly.
The example below chooses the 200 option with reliable queue writing: the event should not depend on how many times the bank is willing to send it. If you choose 5xx — make sure that manual review and reprocessing correctly survive repeated deliveries.
Pseudocode diagram:
POST /webhooks/bank
raw = read_body()
if not verify_signature(raw, header["X-Signature"], BANK_SECRET):
return 401
event = parse_json(raw)
key = event.invoice_id + ":" + event.payment_id # idempotency key
invoice = find_invoice(event.invoice_id)
if invoice is None:
# invoice not found: do NOT fix the key, but the event must reliably go to the queue
persist_for_manual_review(event) # write to your DB — atomically, before responding
return 200
if event.amount != invoice.amount:
persist_for_manual_review(event) # amount mismatch — same thing
return 200
begin_transaction()
if not try_insert_event(key): # key already exists
rollback()
# key exists, but status may not have been set — see "Key exists but no status"
reconcile_if_needed(event)
return 200
mark_paid(invoice)
insert_outbox(event) # post-processing task — in the same transaction
commit()
return 200
# separate worker: reads outbox, sends notifications and syncs CRM,
# marks the record as done; if not done — will retry
Processing errors (database unavailable, CRM not responding) return as 5xx — then the bank will retry delivery.
Key exists but no status
The statement "key and mark_paid are atomic" is true only for a single transaction. But between "key already inserted by a previous request" and "status set" a failure may occur: the transaction was interrupted, the process crashed, the database rolled back part of the work. Then on retry try_insert_event returns false, and naive code discards the event as a duplicate — and the payment is never marked.
Therefore, the "key already exists" branch must not be empty. The minimally reliable option is to check the actual state and bring it to the required one:
if not try_insert_event(key):
rollback()
if not is_paid(invoice):
# key exists, but payment not recorded — finish the job
begin_transaction()
mark_paid(invoice)
insert_outbox(event)
commit()
return 200
Such a retry is safe: mark_paid is idempotent by status (repeated "Paid" changes nothing), and insert_outbox is protected by the unique event key so as not to create a second post-processing task. Test this: manually insert the key without setting the status, send the webhook — the status should become "Paid".
Two holes to close manually
The pseudocode above is reliable in one thing: the key, mark_paid, and the outbox record are committed together. But it still has two windows where the event can "hang", and both must be closed explicitly.
Window 1: between commit() and enqueuing post-processing. If you write to the queue after commit, the key is already fixed, the "Paid" status is already set, but the task for notifications and CRM synchronization is not created. A process crash right here means that a repeated delivery from the bank will be discarded as a duplicate (try_insert_event returns false), and post-processing will never execute. The solution is a transactional outbox: the record about the need for post-processing is written in the same transaction as the key and mark_paid, and a separate worker reads the outbox and performs the actions. Then "fix the event" and "enqueue post-processing" are one atomic action, no window. That's exactly how it's written in the pseudocode above: insert_outbox(event) is placed before commit().
Window 2: between the decision "to manual review" and the actual write. We return an event with an invisible invoice or a mismatched amount to the bank as 200 — meaning the bank will not send it again. If the write to the manual review queue fails (queue unavailable, process crashed before writing), the event disappears: the manager won't see it, the payment won't be closed. Therefore persist_for_manual_review must write to your DB, not to an external queue, and complete before the 200 response. Then a separate worker moves records from this table to the external queue or to the manager's interface and marks them as processed; if it fails — it will retry. Simply "call the enqueue function and respond 200" is not enough.
About partial payment. The key invoice_id:payment_id distinguishes payments for the same invoice: two partial payments will come with different payment_id and will be processed as two different events. But if the bank sends the same payment_id for a partial payment as for the first one (depends on the bank — check the documentation), the key will match, and the second payment will be discarded as a duplicate. In this case, either include the amount in the key, or don't rely on the key for partial payments, but reconcile the accumulated amount for the invoice with the expected one.
Idempotency: how to distinguish a retry from a new payment
The idempotency key is an event identifier, not a request identifier: invoice number + payment identifier from the webhook. It must be stored in a separate table with a unique index on the key, in the same transaction as the request status update.
- Request race. Two identical webhooks arrive simultaneously: both try to insert the key, the unique index lets only one through, the second gets an insertion error and quietly responds 200. The status update happens exactly once — and is guaranteed to happen because the key insertion and
mark_paidare committed together: if the first request fails before commit, the key is not saved and the repeated delivery will be processed anew. - Retry vs new payment. If the key already exists in the table and the status is "Paid" — this is a repeat of an already processed event, do nothing. If the key exists but there is no status — bring the status to "Paid" (see "Key exists but no status"). If the key is new but the invoice number is the same — this is a new payment for the same invoice (e.g., additional payment): it is processed separately, not as a duplicate.
- Order. Change the status only to "Paid" and do not roll it back on repeated delivery — otherwise a late duplicate will "reset" the payment.
How to check: send a test webhook, then the same webhook again — the status should not change twice. Send two identical requests in parallel — the result is the same. Manually insert the key without setting the status and send the webhook — the status should become "Paid". Send a webhook with a non-existent invoice, then the same webhook again after the invoice is created manually — the second time it should be processed, not discarded as a duplicate.
Access: a public URL for the webhook (during development — ngrok). Whether your bank supports webhooks and what fields and signature method it provides — check its documentation; if there are no webhooks, API polling plus statement reconciliation remains.
Step 5: Manual review queue
Everything that could not be processed automatically goes to the queue, not into silence: AI didn't determine the amount, webhook didn't arrive, bank returned an error, amount mismatch, invoice not found.
What to do: a list (in the CRM or a separate table) of requests requiring attention, and alerts (email, Telegram) when new records appear — otherwise the queue will be missed. It's important that the queue write is reliable (see "Two holes" above): an event returned to the bank as 200 will not come again, and if it didn't land in the queue — it's lost.
How to check: simulate a failure (disable the webhook) and make sure the request appears in the queue. Separately test a process crash immediately after the 200 response to an event sent to manual review: after restart, the event should be in the queue.
How to link payment to invoice if the bank doesn't send the invoice number
A common situation: the webhook contains only the amount and payment purpose. Then the link is built on the payment purpose:
- Encode the invoice number in the purpose. When generating the invoice, substitute a short unique code (invoice number or a separate payment code) into the payment purpose — so that the client sees it when paying and it ends up in the purpose.
- Parse the purpose in the webhook. Look for the code with a regular expression; if found — link the payment to the invoice.
- If not found — reconcile by amount and date. If there is only one unpaid invoice matching the amount, you can match automatically; if several — only manually.
- Everything ambiguous — to the manual review queue with the amount and purpose, so the manager can link the payment to the invoice.
Check the bank's documentation whether the payment purpose is preserved in the webhook and whether it is truncated — the code length depends on this.
Where it breaks: three real risks
-
API limits. External services (LLM, bank, CRM) have limits on the number of requests. Solution: a queue with retries and monitoring. Set the specific retry frequency and queue size based on your limits: for example, exponential backoff with a base pause of a few seconds and a cap of a few minutes, and a request timeout shorter than the interval between attempts. There are no universal numbers here: take the limits from the providers' documentation and calculate for your request flow.
-
Lost duplicates. If the webhook arrives twice, you can update the status twice or create a duplicate request. Solution: idempotency key and checking whether this request has already been processed (see step 4).
-
Personal data. You store and transmit name and phone. Make sure you comply with data protection regulations: consent to processing, storage on servers in the appropriate jurisdiction (if required), encryption.
How to check that no request is lost
- Submit a request with the CRM turned off — it should be saved in the database and go to the CRM after it's turned on.
- Submit a request with text that the AI cannot parse — it should go to the manual review queue.
- Generate an invoice and send a test webhook — the status should update.
- Send the same webhook again and in parallel — the status should not change twice.
- Manually insert the idempotency key without setting the status and send the webhook — the status should become "Paid".
- Send a webhook with an amount that doesn't match the invoice — the request should go to manual review.
- Send a webhook with a non-existent invoice, then the same webhook after manually creating the invoice — the second time the event should be processed, not discarded as a duplicate.
- Crash the process immediately after the 200 response to an event sent to manual review — after restart, the event should be in the queue.
- Crash the process immediately after the payment commit, before post-processing — after restart, notifications and CRM synchronization should be performed from the outbox.
- Disable the webhook and submit a request — it should stay in the queue and appear in the "Needs attention" list.
If all points are met — the pipeline works.
How much it costs and how long it takes
Building the pipeline is not a single evening: from several days to several weeks depending on the number of integrations and the complexity of the price list. Main costs:
- Developer time: 20–60 hours (form, database, integrations, testing).
- Service subscriptions: LLM API (calculate based on the provider's pricing and token usage per request), hosting, possibly a paid CRM plan.
- Bank fees: check with your bank.
If you don't want to figure it out yourself, asibiont.com takes such projects turnkey. Cost is discussed individually, payment — by topping up the balance. More details on the pricing page.