# Accounts receivable

> Know which invoices are already paid, which are in installments and how much each customer owes you, and hear about each payment by webhook.

Each income invoice says whether it's been collected. Fiscalisto keeps track with what it already stamps: the [payment complements](https://fiscalisto.com/en/docs/api/pagos-y-notas-de-credito), the credit notes and, in the app, the reconciled bank deposits. You don't store balances.

## On each invoice

| Field | What it is |
| --- | --- |
| `payment_status` | `paid`, `partial` (payments or notes for part of it) or `unpaid`. `null` if it isn't an income invoice or isn't current. |
| `amount_due` | What's left to collect, in the invoice's currency. |
| `amount_paid` | What's been collected: current payment complements and credit notes. |
| `due_date` | While something is owed, the day it's due (YYYY-MM-DD): its date plus the credit days in the customer's record (or their customer group) or, without them, the company's (30 unless changed); a PUE with `paid_later`, the last day of its month. `null` once paid. |
| `payment_terms` | The payment terms you wrote when creating it (`CondicionesDePago` in the XML, also on the PDF), such as `"Crédito a 30 días"`; `null` without them. Send them in `payment_terms` on `POST /v1/invoices`, up to 1,000 characters without `|`. They don't change `due_date`. |

Each customer carries `credit_days` (their credit days; `null`: their group's or the company's) and `payment_terms` (the terms their new invoices start with in the app; `null`: the company's). Change them with `POST /v1/customers/{id}`. `POST /v1/invoices` doesn't add the terms by itself: send them in `payment_terms`.

A **PPD** invoice starts `unpaid` and goes down with each payment complement; if a complement is cancelled, its balance comes back. A **PUE** one is paid from the moment it's stamped, unless you stamp it with `"paid_later": true` (the customer pays you this month): it stays `unpaid` until you mark it paid. Send `paid_on` (YYYY-MM-DD) with the day you were paid, or today's is used; the invoice shows it in `paid_on`, and it counts in the customer's payment history (`GET /v1/customers/{id}/payment_behavior`).

```bash tab="cURL"
curl -X POST https://api.fiscalisto.com/v1/invoices/inv_OufpKqnrvx9p6hW1yQbT3mZr8sKdLx0VeN/mark_paid \
  -H "Authorization: Bearer $FISCALISTO_API_KEY"
```

```ts tab="TypeScript"
import { Fiscalisto } from "fiscalisto";

// The key comes from FISCALISTO_API_KEY.
const fiscalisto = new Fiscalisto();

const invoice = await fiscalisto.invoices.markPaid("inv_OufpKqnrvx9p6hW1yQbT3mZr8sKdLx0VeN");
```

```python tab="Python"
from fiscalisto import Fiscalisto

# The key comes from FISCALISTO_API_KEY.
fiscalisto = Fiscalisto()

invoice = fiscalisto.invoices.mark_paid("inv_OufpKqnrvx9p6hW1yQbT3mZr8sKdLx0VeN")
```

```php tab="PHP"
<?php

use Fiscalisto\Fiscalisto;

// The key comes from FISCALISTO_API_KEY.
$fiscalisto = new Fiscalisto();

$invoice = $fiscalisto->invoices->markPaid('inv_OufpKqnrvx9p6hW1yQbT3mZr8sKdLx0VeN');
```

```go tab="Go"
package main

import (
	"context"
	"fmt"
	"log"

	fiscalisto "fiscalisto.com/go"
)

func main() {
	// The key comes from FISCALISTO_API_KEY.
	client, err := fiscalisto.NewClient("")
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()

	invoice, err := client.Invoices.MarkPaid(ctx, "inv_OufpKqnrvx9p6hW1yQbT3mZr8sKdLx0VeN", nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(invoice.ID)
}
```

```java tab="Java"
import com.fiscalisto.FiscalistoClient;

// The key comes from FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var invoice = fiscalisto.invoices().markPaid("inv_OufpKqnrvx9p6hW1yQbT3mZr8sKdLx0VeN");
System.out.println(invoice.getId());
```

```csharp tab="C#"
using Fiscalisto;

// The key comes from FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var invoice = await fiscalisto.Invoices.MarkPaidAsync("inv_OufpKqnrvx9p6hW1yQbT3mZr8sKdLx0VeN");
```

`POST /v1/invoices/{id}/mark_unpaid` does the opposite with a PUE already stamped. If the month ends and a PUE is still unpaid, the law requires replacing it with a PPD (RMF rule 2.7.1.39). A PPD isn't marked: it's paid with its complement (`400 invoice_not_markable`).

In the app, a years-old PPD without a complement can be taken out of **Por cobrar** (collected without a complement, or uncollectible) so it doesn't add up as debt. The API doesn't change it: for the SAT it's still unpaid, so it keeps its `payment_status` (`unpaid` or `partial`) and its `amount_due`, and stays in `GET /v1/receivables`. If its complement arrives later, it becomes `paid` and `invoice.paid` is sent.

## What you're owed

```bash tab="cURL"
curl https://api.fiscalisto.com/v1/receivables \
  -H "Authorization: Bearer $FISCALISTO_API_KEY"
```

```ts tab="TypeScript"
import { Fiscalisto } from "fiscalisto";

// The key comes from FISCALISTO_API_KEY.
const fiscalisto = new Fiscalisto();

for await (const item of fiscalisto.receivables.list()) {
  console.log(item.id);
}
```

```python tab="Python"
from fiscalisto import Fiscalisto

# The key comes from FISCALISTO_API_KEY.
fiscalisto = Fiscalisto()

for item in fiscalisto.receivables.list():
    print(item["id"])
```

```php tab="PHP"
<?php

use Fiscalisto\Fiscalisto;

// The key comes from FISCALISTO_API_KEY.
$fiscalisto = new Fiscalisto();

foreach ($fiscalisto->receivables->list() as $item) {
    echo $item['id'], "\n";
}
```

```go tab="Go"
package main

import (
	"context"
	"fmt"
	"log"

	fiscalisto "fiscalisto.com/go"
)

func main() {
	// The key comes from FISCALISTO_API_KEY.
	client, err := fiscalisto.NewClient("")
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()

	for item, err := range client.Receivables.ListAutoPaging(ctx, nil) {
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(item.ID)
	}
}
```

```java tab="Java"
import com.fiscalisto.FiscalistoClient;

// The key comes from FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

for (var item : fiscalisto.receivables().listAutoPaging()) {
    System.out.println(item.getId());
}
```

```csharp tab="C#"
using Fiscalisto;

// The key comes from FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

await foreach (var item in fiscalisto.Receivables.ListAutoPagingAsync())
    Console.WriteLine(item.Id);
```

Returns what each customer owes, per currency: `amount_due`, how much of it is past due (`amount_overdue`, by each invoice's `due_date`), how many open invoices they have (`open_invoices`) and the oldest one's date (`oldest_invoice_date`), sorted by RFC. With `?customer=RFC` (or their `cus_…` id), only that customer. Their invoices: `GET /v1/invoices?customer=RFC&payment_status=open` (`unpaid`, `partial`, `open` or `paid`).

## How each customer pays

```bash tab="cURL"
curl https://api.fiscalisto.com/v1/customers/cus_OufpKqnrvx9p1/payment_behavior \
  -H "Authorization: Bearer $FISCALISTO_API_KEY"
```

```ts tab="TypeScript"
import { Fiscalisto } from "fiscalisto";

// The key comes from FISCALISTO_API_KEY.
const fiscalisto = new Fiscalisto();

const customerPaymentBehavior = await fiscalisto.customers.paymentBehavior("cus_OufpKqnrvx9p1");
```

```python tab="Python"
from fiscalisto import Fiscalisto

# The key comes from FISCALISTO_API_KEY.
fiscalisto = Fiscalisto()

customer_payment_behavior = fiscalisto.customers.payment_behavior("cus_OufpKqnrvx9p1")
```

```php tab="PHP"
<?php

use Fiscalisto\Fiscalisto;

// The key comes from FISCALISTO_API_KEY.
$fiscalisto = new Fiscalisto();

$customerPaymentBehavior = $fiscalisto->customers->paymentBehavior('cus_OufpKqnrvx9p1');
```

```go tab="Go"
package main

import (
	"context"
	"fmt"
	"log"

	fiscalisto "fiscalisto.com/go"
)

func main() {
	// The key comes from FISCALISTO_API_KEY.
	client, err := fiscalisto.NewClient("")
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()

	customerPaymentBehavior, err := client.Customers.PaymentBehavior(ctx, "cus_OufpKqnrvx9p1")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", *customerPaymentBehavior)
}
```

```java tab="Java"
import com.fiscalisto.FiscalistoClient;

// The key comes from FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var customerPaymentBehavior = fiscalisto.customers().paymentBehavior("cus_OufpKqnrvx9p1");
System.out.println(customerPaymentBehavior);
```

```csharp tab="C#"
using Fiscalisto;

// The key comes from FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var customerPaymentBehavior = await fiscalisto.Customers.PaymentBehaviorAsync("cus_OufpKqnrvx9p1");
```

Returns how that customer pays the issuer, from their credit (PPD) invoices of the last 12 months and their payment complements, in pesos, like **Cómo paga** in the app:

| Field | What it is |
| --- | --- |
| `rating` | `on_time`, `sometimes_late`, `late`, `very_late` or `no_history` (fewer than 2 paid invoices and nothing overdue). |
| `average_days_to_pay` | Days from invoice to payment, amount-weighted average. `median_days_to_pay` is the typical invoice. |
| `on_time_share` | The share (0 to 1) of what was paid that arrived before falling due, with their `credit_days`. |
| `average_days_late` | Days past the due date, on average. What's overdue and unpaid counts with its days up to today, so a customer who stopped paying doesn't come out punctual. |
| `trend` | The last 3 months against the previous 9: `slower`, `faster` or `steady`. |
| `open_invoices` | What they owe, with `due_date` and `expected_payment_date`: its date plus how long the customer usually takes. |

`credit_days` are the customer's (from their record or their group) or, if they have none, the company's (**Ajustes › Cobranza automática**). Use it to decide whether to give them credit in your own system or to project when the money will arrive. It doesn't apply to `XAXX010101000` or `XEXX010101000` (`customer_generic_tax_id`): many customers share those RFCs.

## Each payment, by webhook

| Event | When |
| --- | --- |
| `invoice.partially_paid` | A payment arrived for part of a PPD invoice (a payment complement or a credit note). |
| `invoice.paid` | Nothing is owed anymore: the last complement, a credit note, a reconciled deposit or `mark_paid`. |

In both, `data.object` is the invoice with its `payment_status` and `amount_due`. That way your system records the payment or releases the order without asking.

In test mode it works the same, with the complements and notes you stamp with your `fk_test_` key.
