> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lerian.studio/llms.txt
> Use this file to discover all available pages before exploring further.

# Pix BTG antifraud partner check

> How the Indirect Pix Plugin (BTG) asks your institution's antifraud partner to screen sent and received Pix, how you turn each check on, and what the partner must implement.

The Indirect Pix Plugin (BTG) can ask your institution's own antifraud partner to screen a Pix before it sends the Pix to BTG or credits it. The partner answers `ALLOW`, `REVIEW` or `DENY`. Each check is off by default. You turn it on for your institution.

## The four checks

***

Each check is a flow. A flow has its own settings, route and credentials.

| Flow | What the partner screens |
| - | - |
| `out` | A Pix that your institution sends to a payee at another institution. |
| `intra_out` | A Pix between two accounts of your institution, from the payer side. |
| `in` | A Pix that your institution receives from BTG. |
| `intra_in` | A Pix between two accounts of your institution, from the payee side. |

With `intra_out` and `intra_in` both on, the partner screens the same Pix twice, and a payee that `intra_in` denies never reaches `intra_out`. The plugin never screens a received return, MED returns included, an intra-PSP refund, or the payer side (`out`, `intra_out`) of a cash-out with `X-Purpose` `INSTANT_PAYMENT_REFUND`.

## Sent Pix

***

With the `out` flow on, the plugin debits the account in Midaz, asks the partner, and only then sends the Pix to BTG. The partner has at most the `syncBudgetMs` of the route, which is 1500 ms or less. The question carries the `metadata` that you sent to `POST /v1/transfers/cashout/process`.

| Answer | Result |
| - | - |
| `ALLOW` | The plugin sends the Pix to BTG, and answers `201` with `PROCESSING`. |
| `DENY` | The plugin reverts the debit, and answers `201` with `REJECTED`. It publishes `cashout.rejected` and sends no webhook. |
| `REVIEW` | The Pix waits in `FRAUD_ANALYSIS` when it can be held (see below). Otherwise the plugin refuses it like a `DENY`. |

The plugin reverts the debit and answers `201` with `REJECTED` in these cases:

* The partner gives no valid decision, and the `posture` of the route is `CLOSED`.
* The configuration of the flow is broken, under any posture. Examples are a missing route, a missing credential bundle, an empty `ANTIFRAUD_SECRETS_BACKEND`, and an address that the plugin does not call.

When the partner gives no valid decision and the posture is `OPEN`, the plugin sends the Pix without screening. The check shows `origin` `FALLBACK` and the `cause`. A `CLOSED` posture with an `OPEN` band that holds the amount behaves the same way.

If Midaz does not answer the revert, the answer is `201` with `PENDING`. The Pix never reaches BTG, and `POST /v1/transfers/{id}/unblock` with `allowNotFoundUnblock` set to `true`, 150 seconds after its last change, reverses the debit and ends it `FAILED` with `BTG_NOT_FOUND`.

Read the outcome from `status` and `fraudCheck`. The `failureReason` of the event `cashout.rejected` is `FRAUD_DENIED` for a refusal by the partner, and `FRAUD_CHECK_UNAVAILABLE` for the cases above.

A screened Pix carries `fraudCheck`: the `decision`, its `origin` (`PROVIDER` or `FALLBACK`), the `cause`, the `analysisId`, the `score` and the `reasons`. The `score` and the `reasons` are the partner's, as it sent them. The text of a reason can name a third party. `fraudCheck` is in the `/process` response, in `GET /v1/transfers/{id}`, in the transfer webhook and in the events `cashout.processing` and `cashout.rejected`.

A Pix Agendado goes through the same check when its schedule fires. A refusal closes the schedule `FAILED` with `failureReason` `FRAUD_REJECTED`, and the plugin does not retry it.

### Hold a sent Pix for review

A sent Pix that the partner answers `REVIEW` waits in `FRAUD_ANALYSIS` only when all of these hold:

* `ANTIFRAUD_OUT_REVIEW_HOLD_ENABLED` is `true`. The default is `false`.
* The `review_window_minutes` of the flow is above `0`.
* The Pix is a `TRANSFER`, not a Saque or a Troco.
* The deadline `decideBy` is still ahead.

Run the schedule worker with `SCHEDULE_WORKER_ENABLED=true` before you turn the hold on. Without it, nothing cuts a Pix in analysis at `decideBy`, and its debit stays.

The call answers `201` with `FRAUD_ANALYSIS` and `fraudCheck.decideBy`. The debit stays, and the plugin sends nothing to BTG. The plugin queues a `FRAUD_ANALYSIS` transfer webhook and publishes `cashout.fraud_analysis`, both in the same transaction as the status. Unblock answers `422` while the Pix is in analysis.

`decideBy` is the creation time plus the shorter of two windows, less 3 minutes. The first is the Pix manual window: 30 minutes from 08:00 to 20:00 on a business day, and 60 minutes at any other time, in UTC-3. The second is the review window of the flow. A review window of 1 to 3 minutes gives a deadline that has already passed, and the plugin refuses that `REVIEW`.

A Pix in `FRAUD_ANALYSIS` ends in one of these ways:

1. **The partner decides.** The partner calls `POST /v1/transfers/{evaluation_key}/resolve` with its application token. The `evaluation_key` is `pix-out:` and the `endToEndId`, the key that the partner evaluated. The body has `analysisId`, `decision` (`ALLOW` or `DENY`) and optional `reasons`. A malformed `reasons` list is dropped, and the decision still applies. The route ignores `X-Idempotency`.
2. **Your back office decides.** A person with the `operator` or `editor` role calls the same route, and adds a `justification` of 10 to 500 characters. The plugin does not publish it.
3. **The payer cancels it.** `POST /v1/transfers/{transfer_id}/cancel` takes `X-Account-Id` and `X-Idempotency`. The plugin reverts the debit and closes the Pix `CANCELLED`, with the `CANCELLED` webhook and the event `cashout.cancelled`. A Pix Agendado that fired it ends `FAILED` with `failureReason` `CASHOUT_CANCELLED`.
4. **`decideBy` passes.** The plugin reverts the debit and closes the Pix `REJECTED` with `fraudCheck.resolution` `EXPIRED`. The `REJECTED` webhook and `cashout.rejected` follow. The `failureReason` of the event is `FRAUD_ANALYSIS_EXPIRED`.

On `ALLOW`, the plugin sends the Pix to BTG as a payment of type `FRAUD`, and the status goes back to `PENDING`, then `PROCESSING`. On `DENY`, it reverts the debit and closes the Pix `REJECTED`, and the `failureReason` of `cashout.rejected` is `FRAUD_DENIED`. `fraudCheck.resolvedBy` is `PARTNER`, `OPERATOR` or `PAYER`.

The first decision that records its resolution wins. The same decision again answers `200` with the current transfer and moves no money.

| Status and code | Meaning |
| - | - |
| `409` `PIX-0472` | Another decision, another closing, another `analysisId`, or a decision at or after `decideBy`. |
| `503` `PIX-0474` | The plugin is still recording the analysis. Retry after the `Retry-After` time. |
| `502` `PIX-0475` | A `DENY` or a cancel whose revert failed. The plugin recorded the intent. Repeat the call to finish it. |
| `422` `PIX-0471` | A cancel for a Pix that is already closed, or that a decision or the deadline closed. |
| `422` `PIX-0470` | A cancel for a Pix in any other status. |
| `422` `PIX-0473` | A body that is not valid: the `decision` is not `ALLOW` or `DENY`, the `analysisId` is not 1 to 64 printable characters, or a person sends no `justification` of 10 to 500 characters. |
| `404` | A cancel for a transfer of another account. |

The route needs the `transfers` permission with the action `decide`. The `operator` and `editor` roles hold it. The cancel needs the `transfers` permission with the action `post`.

### Internal Pix between accounts of your institution

With the `intra_out` flow on, the plugin asks the partner after it validates the payee and prepares the credit, and before it posts. A payee that `intra_in` denied never reaches the partner. When `intra_in` put the payee under a block, any answer other than `ALLOW` refuses the transfer.

| Answer | Result |
| - | - |
| `ALLOW` | The transfer posts in one Midaz transaction, as it does without the flow. |
| `DENY`, or no answer under a `CLOSED` posture | `201` with `REJECTED`, and `cashout.rejected`. Nothing was debited. |
| `REVIEW` with the hold applying | The plugin holds the payer balance in Midaz, with the legs of the posting as a pending transaction. The answer is `201` with `FRAUD_ANALYSIS`, with the same webhook and `cashout.fraud_analysis` as an external Pix. |
| `REVIEW` without the hold | The plugin refuses it like a `DENY`. |

If Midaz refuses the hold, the answer is `422` with `PIX-0600` when the balance is short, and the cash-out is `FAILED` with `MIDAZ_HOLD_FAILED`.

The same `/resolve`, cancel and cut decide it. On `ALLOW`, the plugin commits the hold and completes the transfer. The cash-out is `COMPLETED`, or `PENDING` until `POST /v1/transfers/{transfer_id}/unblock` finishes it, or `FAILED` with `MIDAZ_COMMIT_FAILED` when Midaz refuses the commit. On `DENY`, a cancel or the cut, the plugin cancels the hold. Nothing was debited, so there is nothing to reverse.

### The outcome of each sent Pix

When the route of a sent flow has a `feedbackEndpoint`, the plugin reports how each screened Pix ended. A route with no `feedbackEndpoint` sends nothing. The `Idempotency-Key` is `pix-out:`, the `endToEndId`, a colon and the `outcome`.

| How the Pix ended | `outcome` |
| - | - |
| Completed by the settlement | `SETTLED` |
| Denied, refused by the closed posture, refused by BTG or Midaz, or any `FAILED` | `REJECTED` |
| Cut at the review deadline | `EXPIRED` |
| Cancelled by the payer during the analysis | `CANCELLED` |

A Pix that the sent flow never screened sends no report, and neither does an intra-PSP Pix that `intra_in` refused. A partner that is down is retried every 5 minutes for 24 hours. A permanent refusal, such as a 4xx, is not retried. The partner must apply each outcome once.

## Received Pix

***

With the `in` flow on, the plugin asks the partner when BTG asks it to approve a received Pix. With `intra_in` on, it asks before the single Midaz transaction of an internal Pix. The plugin checks the destination account first, and a Pix that the account check refuses does not reach the partner. The partner has at most the `syncBudgetMs` of the route, which is 1000 ms or less. The plugin makes one attempt and does not retry.

| Answer | Reply to BTG |
| - | - |
| `ALLOW` | `ACCEPTED`. |
| `DENY`, or `REVIEW` with the cautelar block off | `DENIED` with the reason code `FRAD`. |
| `REVIEW` with the cautelar block on, without `AG01`, on the external flow | `ACCEPTED`. The plugin credits the Pix under a block. |
| `DENY` or `REVIEW` with the partner reason `AG01` | `DENIED` with the reason code `DS04` and the reason "Operation not allowed". The plugin does not mark it as fraud. |
| No valid answer, posture `OPEN` | `ACCEPTED`. The check shows `origin` `FALLBACK`. |
| No valid answer, posture `CLOSED` | `DENIED` with `DS04` and the reason "Fraud check unavailable". Never `FRAD`. |
| Broken configuration, under any posture | `DENIED` with `DS04`. |

A refused Pix is never stored or credited. An intra-PSP Pix that `intra_in` refuses rejects the cash-out with no debit. The answer to a received Pix stays on the cash-in as `fraudCheck`. For the external flow, [Get an inbound webhook](https://docs.lerian.studio/en/reference/interfaces/pix-btg/get-an-inbound-webhook.md) shows the audit keys in `payload.metadata`: `antifraudDecision`, `antifraudOrigin`, `antifraudCause`, `antifraudAnalysisId`, `antifraudScore`, `antifraudReasons` and `antifraudLatencyMs`. They are absent when the flow is off or the credit is a MED return. The event `cashin.received` carries `fraudCheck`. The event `cashin.denied` announces a refusal of a received Pix, external or intra-PSP.

### Credit a received Pix under a block

A received Pix that the partner answers `REVIEW` is credited and held, instead of refused, when both of these are on:

* `ANTIFRAUD_IN_CAUTELAR_ENABLED` is `true`. The default is `false`. It needs Midaz 4.1.0 or later with `overrides.allowFeeSkip` and `overrides.allowTracerSkip` on the ledger, and `SCHEDULE_WORKER_ENABLED=true` on the schedule worker, because its tick releases the block at the deadline.
* `antifraud.in.cautelar_enabled` is `true` for an external Pix, or `antifraud.intra_in.cautelar_enabled` is `true` for an internal Pix. Both default to `false`.

The block holds through the refund operation routes: `MIDAZ_CASHOUT_REFUND_*`, which fall back to `MIDAZ_CASHOUT_*`. With `accounting.validateRoutes` on in Midaz, give those routes the entries for the hold, its commit and its cancel.

The plugin credits the Pix and places a pending hold of the holder's net amount in one Midaz batch. For an internal Pix, one batch posts the payer debit, the payee credit and the hold. The block lasts until the approval time plus 72 hours, less 15 minutes (`block.expiresAt`). The block webhooks and events carry its status in `block.status`:

| Status | Meaning |
| - | - |
| `BLOCKED` | The plugin credited the Pix and holds up to the net amount. The money is booked and cannot be spent. |
| `UNPLACED` | The plugin credited the Pix and held nothing, because the available balance of the holder covered nothing. |
| `RETURNING` | The return of the holder is in flight. The hold stays until the return completes. |
| `RELEASED` | The plugin cancelled the hold. The money is spendable. Final. |
| `RETURNED` | The return completed. Final. |

The events are `cashin.blocked`, `cashin.block_released`, with `releaseReason` `ALLOW`, `EXPIRED` or `RETURNED`, and `cashin.block_fraud_confirmed`. Each event has a webhook to your endpoint. The three arrive as `TRANSFER` / `CASHIN`, with a `block` object (`blockId`, `amount`, `status`, `blockedAt`, `expiresAt`). Tell them apart by `block.status`, by `fraudCheck.resolution` (`DENY` after a refusal) and by `releaseReason`, which only the release carries.

The partner and a person with the `transfers` permission and the action `decide` decide the block on `POST /v1/transfers/pix-cashin:{endToEndId}/resolve`. It takes the same body and authorization as the sent Pix route, and answers with the cash-in.

| Case | Answer |
| - | - |
| No such cash-in, or no block on it | `404` `PIX-0425`. |
| Another `analysisId`, a block that ended, a decision at or after `block.expiresAt`, another decision, or `ALLOW` on `UNPLACED` | `409` `PIX-0472`. |
| The plugin has not recorded the block yet | `503` `PIX-0474` with `Retry-After`. |
| `ALLOW` on `BLOCKED` | `200`. The plugin cancels the hold and ends the block `RELEASED`. |
| `DENY` on `BLOCKED` | `200`. The plugin records and announces it, and the hold stays. |
| `DENY` on `UNPLACED` | `200`. The plugin records it. There is no hold to keep. |
| Any decision on `RETURNING` | `200`. The plugin applies it when the return ends. |
| Same decision again | `200` with the cash-in. |

A person's `ALLOW` after the partner's `DENY` releases the block, with a justification, and keeps the `DENY` on the record. The plugin does nothing else on a `DENY`. When the deadline passes, the schedule worker releases the block. The partner gets one report per block, on the `feedbackEndpoint` of the `in` route, or of the `intra_in` route for an internal Pix: `SETTLED` when someone decided a release, and when the plugin could hold nothing (`UNPLACED`), `EXPIRED` when the deadline ended it, and `RETURNED` for a completed return. A `DENY` reports nothing.

A return of a `BLOCKED` cash-in through `POST /v1/transfers/{id}/refunds` takes one amount: the held amount when the hold is below the net amount, otherwise the whole received amount. It takes no operations split, no description and no reason `SL02`, and one return at a time. It is refused while a release is due. Any other request answers `409` `PIX-0440`. `POST /v1/transfers/{id}/unblock` on a cash-in whose block is due but not recorded answers `503` `PIX-0016` for up to 130 minutes past the deadline.

## Turn a check on

***

In BYOC, write the settings in the systemplane namespace `plugin-br-pix-indirect-btg.policy`, with `PUT /v1/system/plugin-br-pix-indirect-btg.policy/{key}`. The `editor` role writes them, and every role reads them. The settings API needs `PLUGIN_AUTH_ENABLED=true`: with authentication off, every call to `/v1/system` answers `403`, and no check can be turned on. The body is `{"value": <the setting>}`. See [systemplane](https://docs.lerian.studio/en/reference/platform/systemplane/overview.md). The plugin reads them on every call, so a change applies without a restart. For each flow:

| Key | Meaning |
| - | - |
| `antifraud.<flow>.enabled` | Turns the check on. The default is off. |
| `antifraud.<flow>.route` | The partner route, as one JSON object. The plugin refuses a route that would deny every Pix when you write it. |
| `antifraud.<flow>.review_window_minutes` | Only for `out` and `intra_out`. How long the partner has to decide a `REVIEW`, in minutes, from 0 to 60. The default is 0, which holds nothing. |
| `antifraud.in.cautelar_enabled` · `antifraud.intra_in.cautelar_enabled` | Only for `in` and `intra_in`. Turns on the credit under a block. |

The route holds these fields:

| Field | Meaning |
| - | - |
| `endpoint` | The address that receives the evaluation. |
| `feedbackEndpoint` | Optional. For `out` and `intra_out`, the address that receives the outcome of each screened sent Pix. For `in` and `intra_in`, the address that receives the outcome of each cautelar block. |
| `auth` | `mode` (`hmac-v1`, `lerian-m2m` or `oauth2-client-credentials`). The two token modes also need `tokenUrl` and `clientId`. `oauth2-client-credentials` accepts `scopes`. |
| `syncBudgetMs` | The longest wait for one evaluation, retries included. From 100 to 1500 for `out` and `intra_out`. From 100 to 1000 for `in` and `intra_in`. |
| `attemptTimeoutMs` | The time limit of one attempt. It cannot be more than `syncBudgetMs`. |
| `maxAttempts` | The number of attempts, 1 or more. For `in` and `intra_in`, it must be exactly 1. |
| `posture` | Required. `default` is `OPEN` or `CLOSED`. `bands` are optional, each with `upTo` and `posture`. |

```json theme={null}
{
  "endpoint": "https://antifraud.example.com/evaluate",
  "auth": { "mode": "hmac-v1" },
  "syncBudgetMs": 800,
  "attemptTimeoutMs": 800,
  "maxAttempts": 1,
  "posture": { "default": "CLOSED" }
}
```

In BYOC, the plugin calls an `endpoint`, `feedbackEndpoint` or `tokenUrl` only when it is `https` on a public address, unless its host is in `ANTIFRAUD_ALLOWED_PRIVATE_HOSTS`. When you write a route that does not meet this rule, the plugin refuses it. See the [environment variables](https://docs.lerian.studio/en/interfaces/pix-btg/indirect-pix-environment-variables.md#antifraud-partner).

In BYOC, the plugin reads a credential bundle from the secret manager that `ANTIFRAUD_SECRETS_BACKEND` names. The path is `tenants/{env}/single-tenant/plugin-br-pix-indirect-btg/external/antifraud-<flow>/credentials`. `{env}` is the value of `ENV_NAME`, in lower case and with no spaces. With `ENV_NAME` empty, the path has no `{env}/` segment. `<flow>` is `out`, `intra-out`, `in` or `intra-in`. The bundle is a JSON object of strings:

* `secret`: the client secret, or the HMAC secret for `hmac-v1`. An `hmac-v1` secret has at least 32 characters.
* `certPem` and `keyPem`: an optional client certificate pair. Send both or neither.
* `header.<Name>`: an optional header that the plugin sends on each call.

Any other key makes the bundle invalid, and the flow then denies every Pix.

In BYOC, the plugin caches a bundle for 30 seconds. Rotate a secret by overwriting the bundle, and keep the partner accepting the old secret for at least 30 seconds after. Deleting and recreating a bundle denies every Pix of the flow while it is missing.

## What the partner implements

***

For the full contract the partner implements, see [Antifraud partner contract](https://docs.lerian.studio/en/reference/antifraud-partner-contract.md).

The plugin sends each evaluation as a `POST` of a JSON body to the `endpoint` of the route. The headers are:

| Header | Value |
| - | - |
| `Idempotency-Key` | `pix-out:` and the `endToEndId` for a sent Pix, `pix-cashin:` and the `endToEndId` for a received Pix. The same key returns the same analysis. |
| `X-Lerian-Decide-By` | The deadline of the decision, in RFC 3339 UTC. |
| `X-Lerian-Payload-Schema` | `pix-out.v1` for a sent Pix. `pix-cashin.v1` for a received Pix. |

Each body has `flow` (`EXTERNAL` or `INTRA`), `amount`, `currency` (`BRL`), `payer` and `payee`. It also has `endToEndId`, `initiationType`, `transactionType`, `description` and `txId` when they exist. `pix-out.v1` adds `transferId`, `createdAt` and the `metadata` of the Pix. `pix-cashin.v1` always has `endToEndId`. Each party has `name`, `taxId` (the CPF or CNPJ), `ispb`, `branch`, `account` and `accountType`, and `accountId` when the party has an account in your ledger. The payee also has `key` when the Pix used one.

The plugin signs each call with the `auth.mode` of the route:

* `hmac-v1`: `X-Webhook-Timestamp` has the Unix time in seconds. `X-Webhook-Signature` is `v1,sha256=` followed by the hex HMAC-SHA256 of `v1:<timestamp>.<raw body>` with the `secret`.
* `lerian-m2m` and `oauth2-client-credentials`: the plugin gets a token from `auth.tokenUrl` with the `clientId` and the `secret`. It sends the token as a bearer token.

The partner answers `200` with a JSON body:

| Field | Meaning |
| - | - |
| `decision` | `ALLOW`, `REVIEW` or `DENY`. |
| `analysisId` | 1 to 64 printable ASCII characters, with no space. Required with `REVIEW`. |
| `score`, `reasons` | Optional. The plugin does not decide by them. |

A `202` counts as `REVIEW`, and it needs an `analysisId`, unless the body says `DENY`. Any other status, or a `REVIEW` with no valid `analysisId`, is not a decision: the `posture` of the route decides. The partner decides a held Pix or block later with the resolve call described above.

The outcome report is a `POST` to `feedbackEndpoint`, signed the same way, with `evaluationKey`, `analysisId`, `decisionOrigin`, `outcome` and `occurredAt`. Answer `2xx`. A `408`, `429` or `5xx` is retried, and any other status is final. The plugin also sends an unsigned `HEAD` to the endpoint of each enabled flow every 25 seconds, and ignores the answer.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.