Integrating TEF (Brazil’s integrated card payment standard) into a point-of-sale system looks simple in the first demo: the system sends the amount, the customer taps the card, the transaction is approved. The real work lies in what happens when something fails halfway. The power goes out, the system freezes, the printer does not respond, the tax invoice is rejected. If the integration was not designed for that, the result is a customer charged with no recorded sale, or a recorded sale with no payment.
Why the payment has two steps
In TEF, card approval does not end the transaction. First comes authorization: the acquirer approves the amount and returns the transaction data, such as the NSU and the authorization code. Then the sales system must confirm the transaction, signaling that the sale was completed on its side. If the sale cannot be completed, the system must undo the transaction instead of confirming it.
That second step exists precisely for failures. While the transaction is neither confirmed nor undone, it stays pending. In SiTef, for example, the CliSiTef library drives authorization through an interactive flow, and at the end the application calls the finalization function stating whether it confirms or undoes. The exact behavior with unresolved pending transactions, such as timeouts and automatic reversal, depends on the version and configuration, so always check the vendor documentation.
Where things break
The most common failure points are:
- The system crashes after authorization and before confirmation. The customer saw “approved” on the pinpad, but the sale was not saved.
- The confirmation is sent, but the response never arrives. The system does not know whether the confirmation was accepted.
- The sale depends on another step that fails. The tax authority (SEFAZ) rejects the invoice, or stock does not reconcile after the payment was authorized.
- The operator tries again. Not knowing whether the first attempt went through, they start another transaction and the customer is charged twice.
Modeling the flow with states
The safest way I know to handle this is to treat the payment as a state machine stored in the database, written before each step rather than after:
STARTED -> AUTHORIZED -> CONFIRMED
-> UNDONE
STARTED -> DECLINED Order matters. The STARTED record is written before calling TEF, with the sale identifier, the amount and the fiscal date and time. As soon as authorization returns, the transaction data is saved as AUTHORIZED. Only after the sale is closed on the system side is the transaction confirmed and the state set to CONFIRMED.
With that, if the system crashes at any point, on restart it knows exactly where it stopped:
- STARTED with no response: the transaction may or may not have been authorized. The pending state must be checked or resolved before the checkout can operate again.
- AUTHORIZED: the payment was approved but not confirmed. The system decides whether the sale can be completed and then confirms, or undoes.
Resolving pending transactions at startup
A good habit is for the system to check for pending transactions at startup and before every new sale. If there are transactions in AUTHORIZED, they are resolved first. That prevents the most dangerous situation: the operator starting a new payment while the previous one is still open.
It also helps to show the operator what is going on. A clear message such as “previous payment pending, resolving” keeps them from restarting the system or repeating the charge on their own.
What about API payments?
With API payments, such as Pix or card gateways, the problem is the same under another name. The request may have been processed even if the response never arrived. The defense is idempotency: each charge is sent with a unique key generated by the system and saved before sending. If the request has to be repeated, the same key is used, and the provider returns the result of the first one instead of creating a new charge. Many payment APIs accept this key in a dedicated header; when they do not, the way forward is to query the status before trying again.
Testing the unhappy path
The approved flow is the easiest to test and the least important. Before going to production, deliberately simulate:
- Shutting the system down right after approval.
- Dropping the network during confirmation.
- Making the tax invoice fail after the payment.
- Restarting the system with pending transactions.
If the system recovers in all these cases without charging twice and without losing a sale, the integration is ready.
If you need TEF, Mercado Pago, PayPal or other payments wired into your system, see how I work on payment gateway integration.