components.payment_gateway.public.ledgers ¶
This module defines the public API for the ledgers subcomponent.
Only business logic is exposed here. Basic entities and enums are exposed in separate modules to avoid loading the entire subcomponent with its models and dependencies when they are not needed.
Attributes¶
Classes¶
LedgerActions ¶
This class contains all the actions related to ledgers.
Functions¶
create_ledger ¶
Create a ledger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
description
|
str
|
The description of the ledger. |
required |
reference
|
str | None
|
An optional reference to the ledger. |
None
|
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/actions/ledger_actions.py
record_entry ¶
record_entry(
session,
/,
id,
amount,
occurred_at,
description=None,
reference=None,
metadata=None,
external_transaction_id=None,
)
Record a new entry in a ledger.
This entry will become the latest entry in the ledger. This operation performs the required bookkeeping on balances in the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
id
|
LedgerId
|
The ID of the ledger. |
required |
amount
|
int
|
The amount of the entry. |
required |
occurred_at
|
datetime
|
The time the event occurred (not the time we process it). |
required |
description
|
str | None
|
An optional description of the entry. |
None
|
reference
|
str | None
|
An optional reference to the entry. |
None
|
metadata
|
dict | None
|
An optional metadata dictionary. |
None
|
external_transaction_id
|
str | None
|
An optional external transaction ID. Useful for recording transactions from external systems. |
None
|
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/actions/ledger_actions.py
record_entry_overwriting_created_at ¶
record_entry_overwriting_created_at(
session,
/,
id,
amount,
occurred_at,
created_at,
description=None,
reference=None,
metadata=None,
external_transaction_id=None,
)
Record a new entry in a ledger overwriting the created_at param. ⚠️ WARNING: This method should be used carefully, as it can break the linear history of a ledger if not used carefully
Should only be called from the LedgerActions.record_entry_overwriting_created_at method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
id
|
LedgerId
|
The ID of the ledger. |
required |
amount
|
int
|
The amount of the entry. |
required |
occurred_at
|
datetime
|
The time the event occurred (not the time we process it). |
required |
description
|
str | None
|
An optional description of the entry. |
None
|
reference
|
str | None
|
An optional reference to the entry. |
None
|
metadata
|
dict | None
|
An optional metadata dictionary. |
None
|
external_transaction_id
|
str | None
|
An optional external transaction ID. Useful for recording transactions from external systems. |
None
|
created_at
|
datetime
|
An optional time at which the entry was created. Allows to write an entry in the past and recomputes all entries past this date. This param should only be passed from the record_entry_overwriting_created_at method. |
required |
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/actions/ledger_actions.py
terminate_ledger ¶
Terminate a ledger.
The operation is idempotent, i.e. it has no effect on already terminated entities.
Ledgers in terminal state cannot be modified or used anymore. Any
attempt to use or retrieve a terminated ledger will raise a
LedgerTerminatedException.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
id
|
LedgerId
|
The ID of the ledger to terminate. |
required |
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/actions/ledger_actions.py
LedgerEntryNotFoundException ¶
Bases: PaymentLedgerException
Exception raised when trying to use a non-existing Ledger Entry.
LedgerNotFoundException ¶
Bases: PaymentLedgerException
Exception raised when trying to use a non-existing Ledger.
LedgerQueries ¶
This class contains all the queries related to ledgers.
Functions¶
find_ledger_entries_by_reference_prefix ¶
Get all the ledger entries for a reference prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
ledger_id
|
LedgerId
|
The ID of the ledger. |
required |
reference_prefix
|
str
|
The reference prefix of the ledger entries. |
required |
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/queries/ledger_queries.py
get_entry ¶
Get a ledger entry entity from its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
id
|
LedgerEntryId
|
The ID of the ledger entry. |
required |
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/queries/ledger_queries.py
get_ledger ¶
Get a ledger entity from its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
id
|
LedgerId
|
The ID of the ledger. |
required |
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/queries/ledger_queries.py
get_ledger_balance ¶
Get current balance of a ledger.
This is the ending balance of the entry that is effective at the given date, or the last entry if no date is given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The session to use for the database operations. |
required |
id
|
LedgerId
|
The ID of the ledger. |
required |
effective_at
|
datetime | None
|
The point in time to get the ledger balance for. |
None
|
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/queries/ledger_queries.py
get_ledger_entry_effective_at ¶
Get the ledger entry for a given ledger at a certain point in time.
The logic is based on entries' created_at and not the occured_at
time to ensure accurate time travel. We want to know same ledger balance
that we would have seen at that time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
ledger_id
|
LedgerId
|
The ID of the ledger. |
required |
effective_at
|
datetime | None
|
The point in time to get the ledger entry for. |
required |
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/queries/ledger_queries.py
get_ledger_ids_by_reference ¶
Get all the ledger IDs for a reference.
Returns 0-n ledger IDs because nothing prevents a reference from being shared between multiple ledgers (this is a business concern and there's no unicity constraint).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The Session to use to make requests. |
required |
reference
|
str
|
The reference of the ledger(s). |
required |
Source code in components/payment_gateway/subcomponents/ledgers/protected/business_logic/queries/ledger_queries.py
LedgerTerminatedException ¶
Bases: PaymentLedgerException
Exception raised when trying to use a terminated Ledger.
PaymentLedgerException ¶
Bases: PaymentGatewayException
Base class for all Ledger exceptions.