Skip to content

Commit 02dbb99

Browse files
committed
Written test cases for invoice and created ledger: model,exceptions and methods
1 parent c084814 commit 02dbb99

File tree

12 files changed

+587
-29
lines changed

12 files changed

+587
-29
lines changed

src/bitpay_sdk/client.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
import json
33

44
from .config import Config
5-
from .models.bill.bill import Bill
65
from .tokens import Tokens
76
from .models.facade import Facade
7+
from .models.bill.bill import Bill
88
from .utils.rest_cli import RESTcli
9+
from .models.ledger.ledger import Ledger
910
from .models.wallet.wallet import Wallet
1011
from .models.invoice.refund import Refund
1112
from .models.invoice.invoice import Invoice
13+
from .models.ledger.ledger_entry import LedgerEntry
1214
from .exceptions.bitpay_exception import BitPayException
1315
from .exceptions.bill_query_exception import BillQueryException
1416
from .exceptions.bill_update_exception import BillUpdateException
17+
from .exceptions.ledger_query_exception import LedgerQueryException
1518
from .exceptions.wallet_query_exception import WalletQueryException
1619
from .exceptions.refund_query_exception import RefundQueryException
1720
from .exceptions.bill_creation_exception import BillCreationException
@@ -58,6 +61,7 @@ def build_config_from_file(self, config_file_path: str):
5861
json_data = json.loads(read_file.read())
5962
self.__env = json_data['BitPayConfiguration']['Environment']
6063
env_config = json_data["BitPayConfiguration"]["EnvConfig"][self.__env]
64+
read_file.close()
6165
except Exception as e:
6266
raise BitPayException("Error when reading configuration file", str(e))
6367

@@ -455,4 +459,40 @@ def deliver_bill(self, bill_id: str, bill_token: str) -> bool:
455459
raise BillDeliveryException("failed to deserialize BitPay server response (Bill) : %s" % str(e))
456460
return bill
457461

462+
def get_ledger(self, currency: str, start_date: str, end_date: str) -> [Ledger]:
463+
try:
464+
params = {"token": self.get_access_token(Facade.Merchant)}
465+
466+
if currency:
467+
params["currency"] = currency
468+
469+
if start_date:
470+
params["startDate"] = start_date
471+
472+
if end_date:
473+
params["endDate"] = end_date
474+
475+
response_json = self.__restcli.get("ledgers/%s" % currency, params)
476+
except BitPayException as e:
477+
raise LedgerQueryException("failed to serialize Ledger object : %s" % str(e),
478+
e.get_api_code())
479+
480+
try:
481+
ledger = LedgerEntry(**response_json)
482+
except Exception as e:
483+
raise LedgerQueryException("failed to deserialize BitPay server response (Ledger) : %s" % str(e))
484+
return ledger
485+
486+
def get_ledgers(self) -> [Ledger]:
487+
try:
488+
params = {"token": self.get_access_token(Facade.Merchant)}
489+
response_json = self.__restcli.get("ledgers", params)
490+
except BitPayException as e:
491+
raise LedgerQueryException("failed to serialize Ledger object : %s" % str(e),
492+
e.get_api_code())
458493

494+
try:
495+
ledgers = Ledger(**response_json)
496+
except Exception as e:
497+
raise LedgerQueryException("failed to deserialize BitPay server response (Ledger) : %s" % str(e))
498+
return ledgers
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .bitpay_exception import BitPayException
2+
3+
4+
class LedgerException(BitPayException):
5+
__bitpay_message = "An unexpected error occurred while trying to manage the ledger"
6+
__bitpay_code = "BITPAY-LEDGER-GENERIC"
7+
__api_code = ""
8+
9+
def __init__(self, message="", code=131, api_code="000000"):
10+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
11+
self.__api_code = api_code
12+
super().__init__(message, code)
13+
14+
def get_api_code(self):
15+
return self.__api_code
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from src.bitpay_sdk.exceptions.ledger_exception import LedgerException
2+
3+
4+
class LedgerQueryException(LedgerException):
5+
__bitpay_message = "Failed to retrieve ledger"
6+
__bitpay_code = "BITPAY-LEDGER-GET"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=132, api_code="000000"):
10+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
11+
self.__api_code = api_code
12+
super.__init__(message, code)
13+
14+
def get_api_code(self):
15+
return self.__api_code

src/bitpay_sdk/models/currency.py

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,91 @@ def is_valid(self, value):
210210
def __init__(self):
211211
pass
212212

213-
# TODO: Add get set methods
213+
def get_code(self):
214+
return self.__code
214215

215-
216+
def set_code(self, code):
217+
self.__code = code
218+
219+
def get_symbol(self):
220+
return self.__symbol
221+
222+
def set_symbol(self, symbol):
223+
self.__symbol = symbol
224+
225+
def get_precision(self):
226+
return self.__precision
227+
228+
def set_precision(self, precision):
229+
self.__precision = precision
230+
231+
def get_currently_settled(self):
232+
return self.__currently_settled
233+
234+
def set_currently_settled(self, currently_settled):
235+
self.__currently_settled = currently_settled
236+
237+
def get_name(self):
238+
return self.__name
239+
240+
def set_name(self, name):
241+
self.__name = name
242+
243+
def get_plural(self):
244+
return self.__plural
245+
246+
def set_plural(self, plural):
247+
self.__plural = plural
248+
249+
def get_alts(self):
250+
return self.__alts
251+
252+
def set_alts(self, alts):
253+
self.__alts = alts
254+
255+
def get_minimum(self):
256+
return self.__minimum
257+
258+
def set_minimum(self, minimum):
259+
self.__minimum = minimum
260+
261+
def get_sanctioned(self):
262+
return self.__sanctioned
263+
264+
def set_sanctioned(self, sanctioned):
265+
self.__sanctioned = sanctioned
266+
267+
def get_decimals(self):
268+
return self.__decimals
269+
270+
def set_decimals(self, decimals):
271+
self.__decimals = decimals
272+
273+
def get_payout_fields(self):
274+
return self.__payout_fields
275+
276+
def set_payout_fields(self, payout_fields):
277+
self.__payout_fields = payout_fields
278+
279+
def get_settlement_minimum(self):
280+
return self.__settlement_minimum
281+
282+
def set_settlement_minimum(self, settlement_minimum):
283+
self.__settlement_minimum = settlement_minimum
284+
285+
def to_json(self):
286+
data = {
287+
"code": self.get_code(),
288+
"symbol": self.get_symbol(),
289+
"precision": self.get_precision(),
290+
"currentlySettled": self.get_currently_settled(),
291+
"name": self.get_name(),
292+
"plural": self.get_plural(),
293+
"alts": self.get_alts(),
294+
"minimum": self.get_minimum(),
295+
"sanctioned": self.get_sanctioned(),
296+
"decimals": self.get_decimals(),
297+
"payoutFields": self.get_payout_fields(),
298+
"settlementMinimum": self.get_settlement_minimum(),
299+
}
300+
return data

src/bitpay_sdk/models/invoice/buyer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,18 @@ def get_notify(self):
7979

8080
def set_notify(self, notify):
8181
self.__notify = notify
82+
83+
def to_json(self):
84+
data = {
85+
"name": self.get_name(),
86+
"address1": self.get_address1(),
87+
"address2": self.get_address2(),
88+
"locality": self.get_locality(),
89+
"region": self.get_region(),
90+
"postalCode": self.get_postal_code(),
91+
"country": self.get_country(),
92+
"email": self.get_email(),
93+
"phone": self.get_phone(),
94+
"notify": self.get_notify(),
95+
}
96+
return data

src/bitpay_sdk/models/invoice/invoice.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ class Invoice(object):
6767
__display_amount_paid = None
6868
__exchange_rates = None
6969

70-
# Newly added on January 7
71-
__payment_string = None # added
72-
__verification_link = None # added
73-
__buyer_email = None # added
74-
__merchant_name = None # added
70+
__payment_string = None
71+
__verification_link = None
72+
__buyer_email = None
73+
__merchant_name = None
7574
__forced_buyer_selected_wallet = None
7675
__forced_buyer_selected_transaction_currency = None
77-
__itemized_details = ItemizedDetails() # added but have doubt in set method
78-
__universal_codes = UniversalCodes() # added
76+
__itemized_details = ItemizedDetails()
77+
__universal_codes = UniversalCodes()
7978
__is_cancelled = None
8079
__bitpay_id_required = None
80+
__rate_refresh_time = None
8181

8282
def __init__(self, price=None, currency=None, **kwargs):
8383

@@ -89,7 +89,13 @@ def __init__(self, price=None, currency=None, **kwargs):
8989
if key in ["buyer", "buyerProvidedInfo", "shopper", "supportedTransactionCurrencies", "minerFees",
9090
"refundInfo", "universalCodes", "itemizedDetails"]:
9191
klass = globals()[key[0].upper() + key[1:]]
92-
value = klass(**value)
92+
93+
if isinstance(value, list):
94+
value = []
95+
for obj in value:
96+
value.append(klass(**obj))
97+
else:
98+
value = klass(**value)
9399
getattr(self, 'set_%s' % change_camel_case_to_snake_case(key))(value)
94100
except AttributeError as e:
95101
print(e)
@@ -190,10 +196,10 @@ def get_payment_display_totals(self):
190196
def set_payment_display_totals(self, payment_display_totals):
191197
self.__payment_display_totals = payment_display_totals
192198

193-
def get_payment_display_subtotals(self):
199+
def get_payment_display_sub_totals(self):
194200
return self.__payment_display_subtotals
195201

196-
def set_payment_display_subtotals(self, payment_display_subtotals):
202+
def set_payment_display_sub_totals(self, payment_display_subtotals):
197203
self.__payment_display_subtotals = payment_display_subtotals
198204

199205
def get_payment_codes(self):
@@ -226,10 +232,10 @@ def get_auto_redirect(self):
226232
def set_auto_redirect(self, auto_redirect):
227233
self.__auto_redirect = auto_redirect
228234

229-
def get_json_paypro_required(self):
235+
def get_json_pay_pro_required(self):
230236
return self.__json_paypro_required
231237

232-
def set_json_paypro_required(self, json_paypro_required):
238+
def set_json_pay_pro_required(self, json_paypro_required):
233239
self.__json_paypro_required = json_paypro_required
234240

235241
def get_id(self):
@@ -448,6 +454,12 @@ def get_bitpay_id_required(self):
448454
def set_bitpay_id_required(self, bitpay_id_required):
449455
self.__bitpay_id_required = bitpay_id_required
450456

457+
def get_rate_refresh_time(self):
458+
return self.__rate_refresh_time
459+
460+
def set_rate_refresh_time(self, rate_refresh_time):
461+
self.__rate_refresh_time = rate_refresh_time
462+
451463
def get_universal_codes(self):
452464
return self.__universal_codes
453465

src/bitpay_sdk/models/invoice/supported_transaction_currencies.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class SupportedTransactionCurrencies(object):
77
__eth = SupportedTransactionCurrency()
88
__usdc = SupportedTransactionCurrency()
99
__gusd = SupportedTransactionCurrency()
10+
__busd = SupportedTransactionCurrency()
1011
__pax = SupportedTransactionCurrency()
1112
__xrp = SupportedTransactionCurrency()
1213
__doge = SupportedTransactionCurrency()
@@ -15,7 +16,7 @@ class SupportedTransactionCurrencies(object):
1516
def __init__(self, **kwargs):
1617
for key, value in kwargs.items():
1718
try:
18-
if key in ["BTC", "BCH", "ETH", "USDC", "GUSD", "PAX", "XRP", "DOGE", "LTC"]:
19+
if key in ["BTC", "BCH", "ETH", "USDC", "GUSD", "BUSD", "PAX", "XRP", "DOGE", "LTC"]:
1920
value = SupportedTransactionCurrency(**value)
2021
getattr(self, 'set_%s' % key.lower())(value)
2122
except AttributeError as e:
@@ -51,6 +52,12 @@ def get_gusd(self):
5152
def set_gusd(self, gusd: SupportedTransactionCurrency):
5253
self.__gusd = gusd
5354

55+
def get_busd(self):
56+
return self.__busd
57+
58+
def set_busd(self, busd: SupportedTransactionCurrency):
59+
self.__busd = busd
60+
5461
def get_pax(self):
5562
return self.__pax
5663

@@ -82,6 +89,7 @@ def to_json(self):
8289
"eth": self.get_eth(),
8390
"usdc": self.get_usdc(),
8491
"gusd": self.get_gusd(),
92+
"busd": self.get_busd(),
8593
"pax": self.get_pax(),
8694
"xrp": self.get_xrp(),
8795
"doge": self.get_doge(),

0 commit comments

Comments
 (0)