From 8cc1bfdee3af4125f8770a5f1e2b8c056bf7ead7 Mon Sep 17 00:00:00 2001 From: Eddington Chamunorwa Date: Sun, 22 Sep 2024 20:04:39 +0200 Subject: [PATCH] Remove declaration of instance attributes as static attributes. This helps by allowing each instance to have its own unique copies, Reverting the bug of having a different instances containing the same attributes values. Also rewriting some methods with semantically intuitive syntax!! :-) --- paynow/model.py | 69 +++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/paynow/model.py b/paynow/model.py index dca351d..db744ae 100644 --- a/paynow/model.py +++ b/paynow/model.py @@ -1,4 +1,5 @@ import requests +from operator import itemgetter import hashlib from six.moves.urllib_parse import quote_plus, parse_qs @@ -14,31 +15,31 @@ def __init__(self, message): class StatusResponse: - paid=bool + # paid=bool """ bool: Boolean value indication whether the transaction was paid or not """ - status=str + # status=str """ str: The status of the transaction in Paynow """ - amount=float + # amount=float """ float: The total amount of the transaction """ - reference=str + # reference=str """ any: The unique identifier for the transaction """ - paynow_reference=str + # paynow_reference=str """ any: Paynow's unique identifier for the transaction """ - hash=str + # hash=str """ any: Hash of the transaction in paynow """ @@ -74,41 +75,41 @@ class InitResponse: """ - success=bool + # success=bool """ bool: Boolean indicating whether initiate request was successful or not """ - instructions=str + # instructions=str """ bool: Boolean indicating whether the response contains a url to redirect to """ - has_redirect=bool + # has_redirect=bool """ bool: Boolean indicating whether the response contains a url to redirect to """ - hash=str + # hash=str """ str: Hashed transaction returned from Paynow """ - redirect_url=str + # redirect_url=str """ str: The url the user should be taken to so they can make a payment """ - error=str + # error=str """ str: the error message from Paynow, if any """ - poll_url=str + # poll_url=str """ str: The poll URL sent from Paynow """ - data = list + # data = list """ str: The poll URL sent from Paynow """ @@ -119,20 +120,18 @@ def __init__(self, data): self.has_redirect = 'browserurl' in data self.hash = 'hash' in data self.data = data - - if not self.success: - return + self.error=None + self.redirect_url=None + self.instruction=None self.poll_url = data['pollurl'] - if not self.success: - self.error = data['error'] + self.error = data.get('error', None) if self.has_redirect: self.redirect_url = data['browserurl'] - if 'instructions' in data: - self.instruction = data['instructions'] + self.instruction = data.get('instructions', None) class Payment: @@ -143,17 +142,17 @@ class Payment: items ([]): Array of items in the 'cart' """ - reference=str + # reference=str """ str: Unique identifier for the transaction """ - items=[] + # items=[] """ []: Array of items in the 'cart' """ - auth_email=str + # auth_email=str """ str: The user's email address. """ @@ -161,6 +160,8 @@ class Payment: def __init__(self, reference, auth_email): self.reference = reference self.auth_email = auth_email + self.items=items=list() + self._append_item=items.append def add(self, title, amount): """ Add an item to the 'cart' @@ -170,7 +171,7 @@ def add(self, title, amount): amount (float): The cost of the item """ # TODO: Validate - self.items.append([title, amount]) + self._append_item([title, amount]) return self def total(self): @@ -179,10 +180,7 @@ def total(self): Returns: float: The total """ - total = 0.0 - for item in self.items: - total += float(item[1]) - return total + return sum(map(itemgetter(1), self.items)) def info(self): """Generate text which represents the items in cart @@ -190,10 +188,7 @@ def info(self): Returns: str: The text representation of the cart """ - out = "" - for item in self.items: - out += (item[0] + ", ") - return out + return ", ".join(map(itemgetter(0), self.items)) class Paynow: @@ -223,22 +218,22 @@ class Paynow: str: Transaction initation url (constant) """ - integration_id=str + # integration_id=str """ str: Merchant's integration id """ - integration_key=str + # integration_key=str """ str: Merchant's integration key """ - return_url = "" + # return_url = "" """ str: Merchant's return url """ - result_url = "" + # result_url = "" """ str: Merchant's result url """