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 |
required |
client_id
|
int or str
|
Restrict matches to this client. |
None
|
type
|
str
|
Restrict matches to |
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 |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the key, client id, |
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:
- With
precheck, look for the marker first; return the payment if found. - POST the payload. A non-ambiguous failure is raised unchanged.
- On an ambiguous failure (timeout, 5xx, dropped connection), look for the
marker during
verify_windowseconds. - 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. - 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 |
required |
dedup_key
|
str
|
Stable key for this logical creation, 1..64 chars of
letters, digits, |
required |
on_unverified
|
str
|
|
'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: |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The created (or previously existing) payment, or a |
Any
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the key, client id, |
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. |