Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Validate transfer transaction data #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions intasend/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class IntaSendUnauthorized(Exception):

class IntaSendServerError(Exception):
pass


class NarrativeExceedsLengthLimit(ValueError):
pass
18 changes: 16 additions & 2 deletions intasend/transfers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from __future__ import annotations
from typing import Dict, Iterable

from intasend.exceptions import NarrativeExceedsLengthLimit
from .client import APIBase

class Transfer(APIBase):
def send_money(self, provider, currency, transactions, callback_url=None, wallet_id=None, requires_approval='YES'):
if not transactions:
raise ValueError("Transaction details required")

payload = {
"provider": provider,
"currency": currency,
Expand All @@ -10,6 +17,7 @@ def send_money(self, provider, currency, transactions, callback_url=None, wallet
"callback_url": callback_url,
"wallet_id": wallet_id
}
_ = _validate_transaction_data(transactions)
return self.send_request("POST", "send-money/initiate/", payload)

def approve(self, payload):
Expand Down Expand Up @@ -41,7 +49,13 @@ def get_bank_codes(self):
return self.send_request("GET", "send-money/bank-codes/ke/", {}, noauth=True)

def airtime(self, currency="KES", transactions=None, requires_approval="YES", callback_url=None, wallet_id=None):
if not transactions:
raise ValueError("Transantion details requiired")
provider = "AIRTIME"
return self.send_money(provider, currency, transactions, callback_url, wallet_id, requires_approval)


def _validate_transaction_data(transactions: Iterable[Dict[str, str]]) -> None:
for transaction in transactions:
if transaction['narrative']:
if len(transaction['narrative']) > 22:
errmsg = "String values beyond 22 chars are truncated by default."
raise NarrativeExceedsLengthLimit(errmsg)