
How to Integrate AI with Brazilian Payment Gateways Safely
Integrate AI with Brazilian payment gateways like Asaas, Mercado Pago and Pagar.me: start read-only, approve payment links by hand, and secure webhooks.
Want to apply this in your company?
Book a call with Juan →How to Integrate AI with Brazilian Payment Gateways Safely
To integrate AI with Brazilian payment gateways such as Asaas, Mercado Pago or Pagar.me safely, start with a read-only integration. Then let the AI prepare payment links that a person approves, and only after that consider any broader action. In parallel, treat webhooks as untrusted input (verify the source, prevent duplicates) and send the model only the minimum financial data. The AI understands, summarizes and proposes. The decision to charge, refund or change an amount stays under fixed rules and human approval.
Key takeaways
- Start with read access: checking charges, statuses and due dates already covers much of the daily billing routine without any risk of moving money.
- If the provider does not offer a read-only credential for what you need, build your own intermediate layer that exposes only lookup functions to the AI.
- A payment link generated by AI must go through human approval, with amount, customer and description checked before sending.
- A webhook is external input: verify authenticity, handle repeated events, and never let the text of an event become an instruction to the model.
- Send the model the least personal and financial data possible, log every lookup and every action, and decide in advance who is accountable for errors.
Why start with read-only access?
Billing is an area where a mistake is costly and hard to undo: charging the wrong person, duplicating a link, sending the wrong amount over WhatsApp. An AI agent, however capable, can misread a request or be steered by malicious text. That is why the first version of the integration should have no write power at all.
With read access, the AI already does useful work for finance and customer support:
- Answering "has this invoice been paid?" by checking the charge status.
- Listing a customer's overdue charges for whoever will call or write to them.
- Summarizing a customer's history before a negotiation.
- Flagging mismatches, such as a payment with no matching charge or a charge with no payment.
- Drafting a reminder message that a person sends or approves.
Asaas, Mercado Pago and Pagar.me all offer APIs and access keys, but how granular the permissions are differs between them and can change. Check each provider's official documentation to see whether a key can be limited to lookups. If it cannot, the safe route is to build a small layer of your own (a service you control) that holds the full key and exposes to the AI only functions like "look up a charge by identifier" and "list a customer's overdue charges". The AI never sees the key and never calls the provider's API directly.
This design follows the principle of least privilege described in OWASP's authorization guidance: each component gets only the permission it needs for its task.
How do you design the integration step by step?
A lean plan that works for any of the three providers:
- Map the current flow: who creates charges, who checks payments, who talks to customers with overdue balances, and where the information lives today.
- Pick a single question or task to start with, for example "check charge status by order".
- Use test environment credentials (a sandbox, when the provider offers one) throughout development.
- Build the lookup functions in the intermediate layer, with validated inputs (an identifier in the expected format, no free text passed through to the API).
- Limit what comes back: return only the necessary fields to the AI, such as status, amount, due date and identifier, without the full tax ID, address or card data.
- Log every call: who asked, which function, when, and with what result.
- Define what the AI answers on its own and what it hands to a person (disputes, refunds, renegotiation, any exception).
- Run the pilot with a small group, compare the AI's answers with manual checks, and only then expand.
If the AI will talk to customers on WhatsApp, read the guide on how to integrate AI into your company's WhatsApp first, because a financial lookup depends on correctly identifying who is on the other side of the conversation.
How do you generate payment links with human approval?
Once reading is stable, the natural second step is letting the AI prepare a payment link. The key word is prepare, not issue. The safe flow has three separate stages:
- Proposal: the AI builds a draft with customer, amount, description, due date and payment method, from a trusted source (an order, quote or contract), never just from what was said in the conversation.
- Approval: a person sees the complete draft, compares it with the source, and approves or rejects it. The approver must be identifiable and authorized to approve.
- Issuance: your system, not the model, calls the provider's API with the data exactly as approved.
A few rules reduce the risk in this stretch:
- Amounts come from an existing price table, quote or order. The AI does not calculate discounts on its own.
- Set a maximum amount per link above which approval requires a second person.
- Use idempotency keys when the provider offers them, so a repeated call does not create duplicate charges. Stripe's public documentation on idempotent requests is a clear explanation of the concept. Each Brazilian provider has its own mechanism, so confirm it in their official documentation.
- Keep the approved draft, the approver and the identifier of the generated link. That is your decision record.
- Block, at the code level, any change or cancellation of a charge made directly by the AI.
To organize these barriers, the article on guardrails for AI agents explains how to validate input, output and action separately. The piece on what not to delegate to AI in business helps you decide where human approval should never be removed.
What should you check in webhooks before the AI reacts to them?
Webhooks are the notifications the provider sends to your system when something happens: payment confirmed, charge overdue, refund issued. They are useful, but they are also an open door on the internet. Anyone who discovers the address can try to send a fake event saying a charge was paid.
What to verify, in general terms (signature or token details vary by provider, so follow each one's official documentation):
- Authenticity: validate the signature or token the provider sends. Reject anything that fails. Stripe's webhook documentation shows the verification reasoning most providers follow.
- Confirmation at the source: for sensitive events such as "paid", query the provider's API to confirm the real state before releasing a product or service.
- Duplicates: providers may resend the same event. Store the event identifier and ignore repeats.
- Order: events can arrive out of sequence. Use the queried state, not just arrival order.
- Fast response: acknowledge receipt and do the heavy work later, in a queue, so events are not lost to timeouts.
- Model isolation: the content of an event never becomes an instruction. Free-text fields, such as a description or the payer's name, can contain phrases that try to manipulate the model. This risk appears in OWASP's list of risks for LLM applications.
A simple split helps: deterministic logic (validate, deduplicate, update status) runs in ordinary code. The AI comes in afterward, for example to draft a notice to the customer, always from structured fields that have already been validated.
What financial data can the AI see, and what does LGPD require?
Billing data identifies people: name, tax ID, email, phone, payment history. Brazil's data protection law (LGPD) requires a clear purpose, data minimization and care with sharing. In practice, that becomes design rules:
- Never send card data, passwords, API keys or tokens to the model. They serve no purpose for the task and increase the damage of a leak.
- Mask or omit anything unnecessary: instead of the full tax ID, use an internal charge identifier.
- Check where the AI provider processes the data and whether there is an international transfer. This point has its own requirements, covered in our article on international data transfer under LGPD when using AI providers.
- Decide how long query logs are kept and who can read them.
- If a decision about a person is automated (for example, denying a payment condition), look at the guidance on human review in LGPD automated decision-making. For classification questions, the website of ANPD, Brazil's data protection authority, is the official reference.
- If the model is called by an agent with tools, keep the payment provider's key out of the model's context, stored on your server.
This content is technical and operational guidance, not legal advice. For the legal basis and contracts with the AI provider, involve your legal team or the company's data protection officer.
Illustrative scenario: monthly fee billing over WhatsApp
Imagine a small recurring-services company that uses one of the three providers to issue monthly charges. Today, someone in finance answers questions on WhatsApp such as "has my payment come through?" and "can you send the link again?".
In the first phase, the AI handles only the first question. It identifies the customer from data already validated in the customer record, calls the lookup function in the intermediate layer, and replies with the status and due date. If it cannot find the charge, or if the customer mentions a dispute, it hands the case to the finance person with a summary.
In the second phase, when the customer asks for the link again, the AI issues nothing. It creates a draft (same charge, same amount, a new date allowed by policy) and sends it to an approval queue. The finance person checks it and approves, the system issues the link through the API, and the AI writes the message with the link already generated. The whole path is logged.
None of this requires the model to have any write permission at the provider. For messages the company starts, such as due date reminders, WhatsApp template and consent rules must be respected, and the guide on WhatsApp outbound messages, templates and opt-in covers that point.
How does the map, limit, pilot and hand over method apply here?
Map: lay out the real billing flow, who decides what, and which questions repeat, before choosing any tool. Limit: define which data and which functions the AI may use, starting with read access, with the provider's key out of the model's reach. Pilot: run a single flow, such as status lookup or a link draft, with logs and a success metric agreed in advance (for example, how many answers the finance person had to correct). The guide on how to measure an AI automation pilot goes into this kind of criterion. Hand over: give the team the documentation, the logs and the review procedure, and decide from the data whether the pilot should expand, be adjusted or stop.
Frequently asked questions
Can I give the full Asaas, Mercado Pago or Pagar.me API key to the AI? It is not recommended. A key with write power allows creating, changing or canceling charges, and a model error or manipulation would have real effects. Keep the key on your server and expose to the AI only well-defined lookup functions.
Can the AI send the payment link directly to the customer? Yes, as long as the link was approved by a person and issued by your system. The model writes the message but does not set the amount, customer or terms. For messages the company initiates, respect WhatsApp template and consent rules.
How do I protect webhooks from fake events? Validate the signature or token sent by the provider, confirm sensitive events by querying the API, and ignore repeated events using the event identifier. The details of each mechanism are in the official documentation of the provider you use. Also, never treat the event text as an instruction to the model.
When does it make sense to let the AI act on its own? Only after a pilot with logs, an agreed metric and a review of errors, and only for low-risk actions that are easy to undo. For refunds, amount changes or cancellations, keep human approval. When in doubt, the default answer is to keep a person in the loop.