# Collections with PSE links

> Send a PSE link per invoice, hear about the payment by webhook and mark the invoice paid without reconciling by hand.

[Sandbox · coming soon]

A company with receivables (installments, invoices, memberships) wants every customer to pay from their bank and its system to mark the invoice paid on its own.

## The flow

1. For every invoice you create a fixed-amount link with the invoice as `reference` and an expiry.
2. You send `checkoutUrl` to the customer over the channel you already use.
3. The customer pays with PSE; zulu credits your balance.
4. The `RECHARGE_LINKS` webhook tells you the link is `PAID` and the `DEPOSITS` webhook carries the deposit with your `reference`.
5. You mark the invoice paid.

## 1. One link per invoice

**POST** `/v1/recharge-links` — Create a PSE payment link · [Sandbox · coming soon] · [See the reference](https://developers.zulu.com.co/en/reference/create-recharge-link.md)

```js title="create-link.js"
export async function linkForInvoice(invoice, token) {
  const response = await fetch('https://sandbox.zulu.com.co/v1/recharge-links', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${token}`,
      'X-API-Key': process.env.ZULU_API_KEY,
      'Content-Type': 'application/json',
      'Idempotency-Key': `invoice-${invoice.id}`,
    },
    body: JSON.stringify({
      amount: invoice.amountCop,
      concept: `Invoice ${invoice.number}`.slice(0, 60),
      reference: invoice.number,
      paymentMethod: 'PSE',
      expiresInHours: 72,
    }),
  });
  if (!response.ok) throw new Error(`zulu ${response.status}: ${await response.text()}`);

  return response.json();
}
```

- Use the invoice number as `reference` (up to 25 characters): it arrives intact in the deposit.
- The `Idempotency-Key` derived from the invoice keeps a retry from creating two links (preview, in the sandbox).
- Store `slug` and `checkoutUrl` next to the invoice.

## 2. Webhooks

When `RECHARGE_LINKS` arrives with `PAID`, or `DEPOSITS` with your reference, fetch the resource and mark the invoice:

```js title="webhook.js"
export async function onDeposit(event) {
  const deposit = await zulu.get(`/v1/deposits/${event.depositId}`);
  const invoice = await invoices.findByNumber(deposit.reference);

  if (!invoice || (await payments.exists(deposit.depositId))) return;
  await payments.record({ depositId: deposit.depositId, invoiceId: invoice.id, amount: deposit.amount });
  if (deposit.amount >= invoice.amountCop) await invoices.markPaid(invoice.id);
}
```

Verify the signature before processing: [Webhooks and signature verification](https://developers.zulu.com.co/en/docs/webhooks.md).

## 3. Links that go unpaid

| What happens   | What to do                                                                              |
| -------------- | --------------------------------------------------------------------------------------- |
| `REFUSED`      | The bank refused the payment. The customer can retry with the same link; let them know. |
| `EXPIRED`      | It expired. Create a new link with the same `reference` if the invoice is still open.   |
| Invoice voided | Cancel the link with `DELETE /v1/recharge-links/{slug}`.                                |

Mind the maximum of active links at once: cancel the links of voided invoices and use expiries.

| Limit | Value |
| --- | --- |
| PSE links active at once | 15 |
| Amount of a PSE link | 100,000 – 1,000,000,000 COP |

## 4. Test it end to end

Create a link, pay it with the simulator and check your system marks the invoice:

**POST** `/v1/sandbox/recharge-links/{slug}/pay` — Simulate a PSE link payment · [Sandbox only] · [See the reference](https://developers.zulu.com.co/en/reference/simulate-recharge-link-payment.md)

Also simulate `REFUSED` and let a link expire with `expiresInHours: 1` to test every branch.
