> ## 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/KypD0TIKUCqZ014J/images/en/d2/id-refinance-a-payroll-loan.svg?fit=max\&auto=format\&n=KypD0TIKUCqZ014J\&q=85\&s=e79c434d6c5d27e21d77b8ff53b0c3ac)

## 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 | 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. |
| 6 | Lerian | Dataprev | Dataprev API | | Lerian sends the reversal to Dataprev (Manual 008). |
| 7 | Dataprev | Lerian | Dataprev API | | Dataprev excludes the new contract, and the settled contracts become active again. |
| 8 | 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. |

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. With FGTS collateral, a refinancing settles one contract.

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.

If the answer does not arrive, do not send the command again under a new key. The code `RAIL_COMMAND_OUTCOME_UNRESOLVED` 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). The code `RAIL_COMMAND_ALREADY_REFUSED` means that Dataprev refused the command under this key. Send a corrected request under a new key.

## How you know it finished

***

Row 4 returns the new contract number and the settled contracts.

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