> ## 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.

# Refinance a payroll loan

> How a new payroll loan settles active payroll loans at Dataprev through Lerian, and how to reverse the refinancing within its window.

Settle one or more active payroll loans with a new loan, and reverse the refinancing within its window if necessary.

> [!NOTE]
>
> This flow is a suggested interpretation of the Dataprev manuals for Crédito do Trabalhador. The manuals prevail over this page. Your institution validates the flow and implements it on the Lerian APIs. See [how to read the diagrams](https://docs.lerian.studio/en/diagrams/introduction.md).

## Participants

***

* **Your institution.** The financial institution that holds the loans and makes the new one.
* **Lerian.** Lerian Consignado. It exchanges the requests with Dataprev, and exposes the API your institution uses.
* **Dataprev.** Runs the Crédito do Trabalhador system: the loan auctions and the registry of payroll loans.

## Diagram

***

![Refinance a payroll loan](https://mintcdn.com/lerian-49cb71fc/-JdEHn8RRUgJwJCQ/images/en/d2/id-refinance-a-payroll-loan.svg?fit=max\&auto=format\&n=-JdEHn8RRUgJwJCQ\&q=85\&s=a147f1d3720dbaf9260a04e4a2b13e13)

## Steps

***

| # | From | To | Message | Lerian API | What happens |
| - | - | - | - | - | - |
| 1 | Your institution | Lerian | Lerian API | [`POST /v1/consignado/contracts/{numero_contrato}/refinanciamento`](https://docs.lerian.studio/en/reference/rails/consignado/realizar-consignado-refinanciamento) | Send the refinancing. `numero_contrato` is the new contract, which you choose. The body lists the contracts to settle. |
| 2 | Lerian | Dataprev | Dataprev API | | Lerian sends the refinancing to Dataprev (Manual 008). |
| 3 | Dataprev | Lerian | Dataprev API | | Dataprev settles the listed contracts and registers the new contract. |
| 4 | Lerian | Your institution | Lerian API | [`POST /v1/consignado/contracts/{numero_contrato}/refinanciamento`](https://docs.lerian.studio/en/reference/rails/consignado/realizar-consignado-refinanciamento) | Lerian returns the new contract number, the settled contracts and the Dataprev operation hash. |
| 5 | Lerian | Your institution | Lerian API | Event `consignado.refinanciamento.efetivado` | Lerian publishes the settled contracts and the terms of the new contract. |
| 6 | Your institution | Lerian | Lerian API | [`POST /v1/consignado/contracts/{numero_contrato}/refinanciamento/reversao`](https://docs.lerian.studio/en/reference/rails/consignado/reverter-consignado-refinanciamento) | Optional: reverse the refinancing within 7 business days. |
| 7 | Lerian | Dataprev | Dataprev API | | Lerian sends the reversal to Dataprev (Manual 008). |
| 8 | Dataprev | Lerian | Dataprev API | | Dataprev excludes the new contract, and the settled contracts become active again. |
| 9 | Lerian | Your institution | Lerian API | [`POST /v1/consignado/contracts/{numero_contrato}/refinanciamento/reversao`](https://docs.lerian.studio/en/reference/rails/consignado/reverter-consignado-refinanciamento) | Lerian returns the Dataprev answer. |
| 10 | Lerian | Your institution | Lerian API | Event `consignado.refinanciamento.revertido` | Lerian publishes that the new contract was excluded. |

Each contract to settle must be a contract that Lerian holds for your institution: registered through Lerian, or imported with [`POST /v1/consignado/contracts/{numero_contrato}/import`](https://docs.lerian.studio/en/reference/rails/consignado/import-consignado-contract). Any other contract answers `404`, and Lerian does not call Dataprev. The same holds for a contract that the follow-up read of Lerian shows at Dataprev as excluded, ended or closed by renegotiation. With FGTS collateral, a refinancing settles one contract. When `temGarantias` is `false`, leave out the three FGTS collateral values, and never send `0`. Lerian refuses them with `422` before it calls Dataprev.

Add the optional `booking` block to book the new contract at Lender. Lerian never sends it to Dataprev. The block has `loan_product_version`, `expected_disbursement_date`, `formalization_deadline` and `margin_snapshot`, and an optional `origination_fee_amount`. Send all the members, and leave out `origination_fee_amount` when there is no fee. With the block, `competenciaInicioDesconto` is required. `valorEmprestimo` less `valorIOF` and less the fee must equal `valorPago` plus `valorTroco`, or the request answers `422` `DECLARED_TERMS_INCONSISTENT`. Lerian answers `422` with the booking codes of the averbação to any other booking that Lender would refuse. Without the block, the new contract exists only at Dataprev, and Lender books nothing.

Lerian reports the outstanding balance of the new contract to Dataprev. For each settled contract, it sends a final balance of 0.00.

You can reverse only a refinancing that Lerian registered.

Lerian publishes three facts for a refinancing that goes through it. `consignado.refinanciamento.efetivado` carries the new contract, the settled contracts and the accepted terms. With `booking`, it also carries `novo_contrato`, in the shape of `consignado.contract.registered`. `consignado.refinanciamento.revertido` says that a reversal excluded the new contract, also one made on the Dataprev portal that the follow-up read of Lerian finds. `consignado.refinanciamento.recusado` says that the refinancing was refused or executed nothing, and names the contracts that stay active. A refinancing made outside Lerian publishes nothing.

With `booking`, Lerian publishes `novo_contrato` only on terms that Dataprev confirmed. If Dataprev confirms another `competenciaInicioDesconto`, the request answers `503` `OUTCOME_UNKNOWN` and Lerian parks the command. Recovery publishes `efetivado` only when its read of the new contract shows the booked terms.

If the answer does not settle the command, Lerian parks it in `outcome_unknown` and answers `503` `OUTCOME_UNKNOWN`. Do not send the command again under a new key. The same key meets `RAIL_COMMAND_OUTCOME_UNRESOLVED`, which means that Lerian does not know the outcome yet. Read the command with [`GET /v1/consignado/rail-commands`](https://docs.lerian.studio/en/reference/rails/consignado/list-consignado-rail-commands).

If recovery cannot decide, your back office closes the parked command with `POST /v1/consignado/rail-commands/{id}/close`. Send `outcome` (`accepted` or `refused`) and `portal_evidence`, which states what the Dataprev portal shows. The call answers `204`. A close as `accepted` makes Lerian read the new contract at Dataprev once. A close as `refused` reads nothing, frees the new contract number, and publishes `consignado.refinanciamento.recusado` with `reason_code` `GW-BANCO-RECUSADO`. A command parked for less than 5 minutes answers `409` `RAIL_COMMAND_NOT_CLOSEABLE`.

When nothing ran at Dataprev, Lerian closes the command refused. A `503` `DATAPREV_UNAVAILABLE` whose detail says so means that the call never left Lerian, or that Dataprev answered only `OZ`, its competência-turnover code. A `422` with the Dataprev codes in `upstream` means that the codes prove nothing ran. Send a corrected request under a new key. The code `RAIL_COMMAND_ALREADY_REFUSED` means that the command under the old key closed refused, by Dataprev or before it reached Dataprev.

## How you know it finished

***

Row 4 returns the new contract number and the settled contracts, and Lerian publishes `consignado.refinanciamento.efetivado`.

## Related

***

* [Register a payroll loan](https://docs.lerian.studio/en/diagrams/native-rails/register-a-payroll-loan.md)
* [Bring in a payroll loan by portability](https://docs.lerian.studio/en/diagrams/native-rails/bring-in-a-payroll-loan-by-portability.md)
* [How Lerian Consignado works](https://docs.lerian.studio/en/rails/consignado/how-consignado-works.md)


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