Saltar a contenido

Pagos

Recurso client.payments (endpoint payments). Agrega anular y reabrir pagos a las operaciones de Actions.

alegrapy.payments.Payments

Bases: Actions

Operations related to payments (pagos/ingresos) endpoint.

__init__(client=None)

Initialize the resource.

Parameters:

Name Type Description Default
client AlegraClient

The AlegraClient instance.

None

reconcile(dedup_key, client_id=None, *, type=None, lookup_pages=2)

Return the newest payment carrying dedup_key, or None.

Scans the newest lookup_pages * 30 payments (no status filter, so voided payments carrying the marker are also considered; callers should check status). client_id and type are sent as server filters when given and re-checked client-side: when client_id is given, a record whose client is null or a different id does not match; a record with a different type does not match. A record that omits the client or type key still matches, so an existing payment is not missed.

Parameters:

Name Type Description Default
dedup_key str

Key used when the payment was created with create_once.

required
client_id int or str

Restrict matches to this client.

None
type str

Restrict matches to "in" or "out".

None
lookup_pages int

Number of 30-record pages to scan (default 2).

2

Returns:

Type Description
Optional[Dict[str, Any]]

dict, optional: The matching payment, or None when not found.

Raises:

Type Description
ValueError

If the key, client id, type or lookup_pages are invalid.

create_once(params, dedup_key, *, on_unverified='retry', verify_window=30.0, lookup_pages=2, precheck=True, with_outcome=False)

Create a payment at most once per dedup_key.

A unique marker (alegrapy:dedup:<key>) is appended to observations (which Alegra does not print on the document) and the payment is looked up by that marker so re-runs return the existing record instead of creating a duplicate.

Flow:

  1. With precheck, look for the marker first; return the payment if found.
  2. POST the payload. A non-ambiguous failure is raised unchanged.
  3. On an ambiguous failure (timeout, 5xx, dropped connection), look for the marker during verify_window seconds.
  4. If not found and on_unverified="retry", POST once more with the same payload, verify again, and otherwise raise :class:AlegraUnverifiedCreateError. Never more than 2 POSTs.
  5. With on_unverified="raise", raise the error right after the window.
Limitations

"retry" reduces but does not eliminate duplicates: if Alegra shows the payment later than verify_window, a second payment can be created. Use "raise" plus a quarantine queue for zero-duplicate flows. Two processes using the same key concurrently can both miss the precheck; serialize per key. client is optional on payments: without a client, the lookup window covers only the newest lookup_pages * 30 payments of the company (filtered by type when given), so high-volume accounts should pass a client or raise lookup_pages. query() itself may repeat a POST only when it provably was not sent (429 or request_not_sent), which cannot create an extra payment.

Parameters:

Name Type Description Default
params dict

Payload for create. params["client"] ({"id": x} or bare id) and params["type"] ("in" or "out") are optional and narrow the lookup when provided.

required
dedup_key str

Stable key for this logical creation, 1..64 chars of letters, digits, _, ., : or -.

required
on_unverified str

"retry" (default) or "raise".

'retry'
verify_window float

Seconds to look for the payment after an ambiguous failure (default 30).

30.0
lookup_pages int

Pages of 30 payments scanned on each lookup (default 2).

2
precheck bool

Look for the marker before posting (default True).

True
with_outcome bool

When True, return a :class:CreateOnceResult (record, outcome) instead of the bare record (default False).

False

Returns:

Name Type Description
Any Any

The created (or previously existing) payment, or a

Any

class:CreateOnceResult when with_outcome=True.

Raises:

Type Description
ValueError

If the key, client id, type, options or observations length are invalid; nothing is sent.

AlegraAPIError

The original error for a non-ambiguous failure.

AlegraUnverifiedCreateError

When an ambiguous create cannot be verified.

void(id)

Void / Annul a payment by ID.

Parameters:

Name Type Description Default
id int or str

Payment ID.

required

Returns:

Name Type Description
Any Any

Parsed JSON response from the server.

Raises:

Type Description
ValueError

If the ID is not a valid resource identifier.

open(id)

Open / Reopen a void payment by ID.

Parameters:

Name Type Description Default
id int or str

Payment ID.

required

Returns:

Name Type Description
Any Any

Parsed JSON response from the server.

Raises:

Type Description
ValueError

If the ID is not a valid resource identifier.