Skip to content

Commit

Permalink
feat: Implement TransactionRef to store end-to-end IDs, etc.
Browse files Browse the repository at this point in the history
See #1.
  • Loading branch information
tkarabela committed Nov 17, 2023
1 parent 5e96146 commit dfaba44
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 23 deletions.
74 changes: 68 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pip install okane
AccountId(iban='LT6632xxxxxx', id=None)
>>> statement.transactions[5].related_account_bank_id
BankId(bic='REVOLT21', id=None)
>>> statement.transactions[3].ref
TransactionRef(message_id='XXX', end_to_end_id='XXX', account_servicer_ref=None, payment_invocation_id=None, instruction_id=None, mandate_id=None, cheque_number=None, clearing_system_ref=None)

### Command-line interface

Expand Down Expand Up @@ -80,7 +82,17 @@ okane ./tests/data/test2.xml
},
"transactions": [
{
"ref": "XXX-REF-1",
"ref": {
"message_id": "XXX",
"end_to_end_id": "XXX",
"account_servicer_ref": null,
"payment_invocation_id": null,
"instruction_id": null,
"mandate_id": null,
"cheque_number": null,
"clearing_system_ref": null
},
"entry_ref": "XXX-REF-1",
"amount": "-100.00",
"currency": "CZK",
"val_date": "2023-03-01",
Expand All @@ -90,7 +102,17 @@ okane ./tests/data/test2.xml
"related_account_bank_id": null
},
{
"ref": "XXX-REF-2",
"ref": {
"message_id": "XXX",
"end_to_end_id": "XXX",
"account_servicer_ref": null,
"payment_invocation_id": null,
"instruction_id": null,
"mandate_id": null,
"cheque_number": null,
"clearing_system_ref": null
},
"entry_ref": "XXX-REF-2",
"amount": "-200.00",
"currency": "CZK",
"val_date": "2023-03-02",
Expand All @@ -106,7 +128,17 @@ okane ./tests/data/test2.xml
}
},
{
"ref": "XXX-REF-3",
"ref": {
"message_id": "XXX",
"end_to_end_id": null,
"account_servicer_ref": null,
"payment_invocation_id": null,
"instruction_id": null,
"mandate_id": null,
"cheque_number": null,
"clearing_system_ref": null
},
"entry_ref": "XXX-REF-3",
"amount": "1000.00",
"currency": "CZK",
"val_date": "2023-03-07",
Expand All @@ -122,7 +154,17 @@ okane ./tests/data/test2.xml
}
},
{
"ref": "XXX-REF-4",
"ref": {
"message_id": "XXX",
"end_to_end_id": "XXX",
"account_servicer_ref": null,
"payment_invocation_id": null,
"instruction_id": null,
"mandate_id": null,
"cheque_number": null,
"clearing_system_ref": null
},
"entry_ref": "XXX-REF-4",
"amount": "400.00",
"currency": "CZK",
"val_date": "2023-03-08",
Expand All @@ -138,7 +180,17 @@ okane ./tests/data/test2.xml
}
},
{
"ref": "XXX-REF-5",
"ref": {
"message_id": "XXX",
"end_to_end_id": "XXX",
"account_servicer_ref": null,
"payment_invocation_id": null,
"instruction_id": null,
"mandate_id": null,
"cheque_number": null,
"clearing_system_ref": null
},
"entry_ref": "XXX-REF-5",
"amount": "-100.00",
"currency": "CZK",
"val_date": "2023-03-31",
Expand All @@ -148,7 +200,17 @@ okane ./tests/data/test2.xml
"related_account_bank_id": null
},
{
"ref": "XXX-REF-6",
"ref": {
"message_id": "XXX",
"end_to_end_id": null,
"account_servicer_ref": null,
"payment_invocation_id": null,
"instruction_id": null,
"mandate_id": null,
"cheque_number": null,
"clearing_system_ref": null
},
"entry_ref": "XXX-REF-6",
"amount": "1000.00",
"currency": "CZK",
"val_date": "2023-03-07",
Expand Down
37 changes: 35 additions & 2 deletions okane.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,45 @@ def from_xml(cls, root: _Element) -> Optional["AccountId"]:
return None


class TransactionRef(BaseModel):
message_id: str | None = None
end_to_end_id: str | None = None
account_servicer_ref: str | None = None
payment_invocation_id: str | None = None
instruction_id: str | None = None
mandate_id: str | None = None
cheque_number: str | None = None
clearing_system_ref: str | None = None

def __str__(self) -> str:
return ", ".join(f"{k}={v}" for k, v in self.model_dump().items() if v is not None)

@classmethod
def from_xml(cls, root: _Element | None) -> "TransactionRef":
if root is None:
return cls()
else:
return cls(
message_id=get_text_or_none(root, "MsgId"),
account_servicer_ref=get_text_or_none(root, "AcctSvcrRef"),
payment_invocation_id=get_text_or_none(root, "PmtInfId"),
instruction_id=get_text_or_none(root, "InstrId"),
end_to_end_id=get_text_or_none(root, "EndToEndId"),
mandate_id=get_text_or_none(root, "MndtId"),
cheque_number=get_text_or_none(root, "ChqNb"),
clearing_system_ref=get_text_or_none(root, "ClrSysRef"),
)


class Balance(BaseModel):
amount: Decimal
currency: str
date: datetime.date


class Transaction(BaseModel):
ref: str
ref: TransactionRef
entry_ref: str
amount: Decimal
currency: str
val_date: datetime.date
Expand Down Expand Up @@ -254,7 +285,8 @@ def parse_transactions(stmt: _Element) -> list[Transaction]:


def parse_transaction(ntry: _Element) -> Transaction:
ref = get_text(ntry, "NtryRef")
entry_ref = get_text(ntry, "NtryRef")
ref = TransactionRef.from_xml(ntry.find("NtryDtls/TxDtls/Refs"))

amt = get_element(ntry, "Amt")
currency = get_attribute(amt, "Ccy")
Expand Down Expand Up @@ -283,6 +315,7 @@ def parse_transaction(ntry: _Element) -> Transaction:
related_account_bank_id = None

return Transaction(
entry_ref=entry_ref,
ref=ref,
amount=amount,
currency=currency,
Expand Down
6 changes: 4 additions & 2 deletions tests/test_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ def test_1():
t1 = statement.transactions[0]
assert t1.amount == 1500
assert t1.currency == "CZK"
assert t1.ref == "XXX-REF-1"
assert t1.entry_ref == "XXX-REF-1"
assert t1.ref == okane.TransactionRef(account_servicer_ref="XXX")
assert str(t1.ref) == 'account_servicer_ref=XXX'
assert t1.val_date == datetime.date(2023, 4, 1)
assert t1.info == 'Incoming payment'

t2 = statement.transactions[1]
assert t2.amount == -500
assert t2.currency == "CZK"
assert t2.ref == "XXX-REF-2"
assert t2.entry_ref == "XXX-REF-2"
assert t2.val_date == datetime.date(2023, 4, 1)
assert t2.info == 'Outbound payment'
12 changes: 6 additions & 6 deletions tests/test_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ def test_2():
t1 = statement.transactions[0]
assert t1.amount == -100
assert t1.currency == "CZK"
assert t1.ref == "XXX-REF-1"
assert t1.entry_ref == "XXX-REF-1"
assert t1.val_date == datetime.date(2023, 3, 1)
assert t1.info == 'Nákup dne 27.2.2023, částka 100.00 CZK'

t2 = statement.transactions[1]
assert t2.amount == -200
assert t2.currency == "CZK"
assert t2.ref == "XXX-REF-2"
assert t2.entry_ref == "XXX-REF-2"
assert t2.val_date == datetime.date(2023, 3, 2)
assert t2.info == 'transaction note'

t3 = statement.transactions[2]
assert t3.amount == 1000
assert t3.currency == "CZK"
assert t3.ref == "XXX-REF-3"
assert t3.entry_ref == "XXX-REF-3"
assert t3.val_date == datetime.date(2023, 3, 7)
assert t3.info == ""
assert t3.related_account_id == okane.AccountId(id='XXX-OTHER-ACC')
Expand All @@ -52,7 +52,7 @@ def test_2():
t4 = statement.transactions[3]
assert t4.amount == 400
assert t4.currency == "CZK"
assert t4.ref == "XXX-REF-4"
assert t4.entry_ref == "XXX-REF-4"
assert t4.val_date == datetime.date(2023, 3, 8)
assert t4.info == 'description / RECIPIENT NAME'
assert t4.related_account_id == okane.AccountId(id='XXX-OTHER-ACC')
Expand All @@ -61,14 +61,14 @@ def test_2():
t5 = statement.transactions[4]
assert t5.amount == -100
assert t5.currency == "CZK"
assert t5.ref == "XXX-REF-5"
assert t5.entry_ref == "XXX-REF-5"
assert t5.val_date == datetime.date(2023, 3, 31)
assert t5.info == 'transaction description'

t6 = statement.transactions[5]
assert t6.amount == 1000
assert t6.currency == "CZK"
assert t6.ref == "XXX-REF-6"
assert t6.entry_ref == "XXX-REF-6"
assert t6.val_date == datetime.date(2023, 3, 7)
assert t6.info == ""
assert t6.related_account_id == okane.AccountId(iban='LT6632xxxxxx')
Expand Down
63 changes: 56 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,35 @@ def test_cli(capsys):
'date': '2023-03-01'},
'statement_id': 'XXX-STATEMENT-ID',
'to_time': '2023-03-31T23:59:59.999000+02:00',
'transactions': [{'additional_transaction_info': 'Nákup dne 27.2.2023, částka 100.00 CZK',
'transactions': [{'additional_transaction_info': 'Nákup dne 27.2.2023, částka '
'100.00 CZK',
'amount': '-100.00',
'currency': 'CZK',
'ref': 'XXX-REF-1',
'entry_ref': 'XXX-REF-1',
'ref': {'account_servicer_ref': None,
'cheque_number': None,
'clearing_system_ref': None,
'end_to_end_id': 'XXX',
'instruction_id': None,
'mandate_id': None,
'message_id': 'XXX',
'payment_invocation_id': None},
'related_account_bank_id': None,
'related_account_id': None,
'remote_info': 'Nákup dne 27.2.2023, částka 100.00 CZK',
'val_date': '2023-03-01'},
{'additional_transaction_info': 'transaction note',
'amount': '-200.00',
'currency': 'CZK',
'ref': 'XXX-REF-2',
'entry_ref': 'XXX-REF-2',
'ref': {'account_servicer_ref': None,
'cheque_number': None,
'clearing_system_ref': None,
'end_to_end_id': 'XXX',
'instruction_id': None,
'mandate_id': None,
'message_id': 'XXX',
'payment_invocation_id': None},
'related_account_bank_id': {'bic': None,
'id': 'XXX-OTHER-BANK'},
'related_account_id': {'iban': None, 'id': 'XXX-OTHER-ACC'},
Expand All @@ -40,7 +57,15 @@ def test_cli(capsys):
{'additional_transaction_info': None,
'amount': '1000.00',
'currency': 'CZK',
'ref': 'XXX-REF-3',
'entry_ref': 'XXX-REF-3',
'ref': {'account_servicer_ref': None,
'cheque_number': None,
'clearing_system_ref': None,
'end_to_end_id': None,
'instruction_id': None,
'mandate_id': None,
'message_id': 'XXX',
'payment_invocation_id': None},
'related_account_bank_id': {'bic': None,
'id': 'XXX-OTHER-BANK'},
'related_account_id': {'iban': None, 'id': 'XXX-OTHER-ACC'},
Expand All @@ -49,7 +74,15 @@ def test_cli(capsys):
{'additional_transaction_info': 'RECIPIENT NAME',
'amount': '400.00',
'currency': 'CZK',
'ref': 'XXX-REF-4',
'entry_ref': 'XXX-REF-4',
'ref': {'account_servicer_ref': None,
'cheque_number': None,
'clearing_system_ref': None,
'end_to_end_id': 'XXX',
'instruction_id': None,
'mandate_id': None,
'message_id': 'XXX',
'payment_invocation_id': None},
'related_account_bank_id': {'bic': None,
'id': 'XXX-OTHER-BANK'},
'related_account_id': {'iban': None, 'id': 'XXX-OTHER-ACC'},
Expand All @@ -58,15 +91,31 @@ def test_cli(capsys):
{'additional_transaction_info': None,
'amount': '-100.00',
'currency': 'CZK',
'ref': 'XXX-REF-5',
'entry_ref': 'XXX-REF-5',
'ref': {'account_servicer_ref': None,
'cheque_number': None,
'clearing_system_ref': None,
'end_to_end_id': 'XXX',
'instruction_id': None,
'mandate_id': None,
'message_id': 'XXX',
'payment_invocation_id': None},
'related_account_bank_id': None,
'related_account_id': None,
'remote_info': 'transaction description',
'val_date': '2023-03-31'},
{'additional_transaction_info': None,
'amount': '1000.00',
'currency': 'CZK',
'ref': 'XXX-REF-6',
'entry_ref': 'XXX-REF-6',
'ref': {'account_servicer_ref': None,
'cheque_number': None,
'clearing_system_ref': None,
'end_to_end_id': None,
'instruction_id': None,
'mandate_id': None,
'message_id': 'XXX',
'payment_invocation_id': None},
'related_account_bank_id': {'bic': 'REVOLT21', 'id': None},
'related_account_id': {'iban': 'LT6632xxxxxx', 'id': None},
'remote_info': None,
Expand Down
Loading

0 comments on commit dfaba44

Please sign in to comment.