Skip to content

Commit d8d7b06

Browse files
committed
Created wallet and refund models, exceptions for refund and invoice, created methods for refund and wallet
1 parent 0ecc928 commit d8d7b06

18 files changed

+695
-51
lines changed

src/bitpay_sdk/client.py

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
from .tokens import Tokens
66
from .models.facade import Facade
77
from .utils.rest_cli import RESTcli
8+
from .models.wallet.wallet import Wallet
9+
from .models.invoice.refund import Refund
810
from .models.invoice.invoice import Invoice
11+
from .exceptions.bitpay_exception import BitPayException
12+
from .exceptions.wallet_query_exception import WalletQueryException
13+
from .exceptions.refund_query_exception import RefundQueryException
14+
from .exceptions.refund_update_exception import RefundUpdateException
915
from .exceptions.invoice_query_exception import InvoiceQueryException
1016
from .exceptions.invoice_update_exception import InvoiceUpdateException
17+
from .exceptions.refund_creation_exception import RefundCreationException
18+
from .exceptions.refund_notification_exception import RefundNotificationException
19+
from .exceptions.refund_cancellation_exception import RefundCancellationException
1120
from .exceptions.invoice_cancellation_exception import InvoiceCancellationException
1221
from .exceptions.invoice_notification_exception import InvoiceNotificationException
13-
from .exceptions.bitpay_exception import BitPayException
1422

1523

1624
class Client:
@@ -234,42 +242,124 @@ def request_invoice_notifications(self, invoice_id: str) -> bool:
234242
except Exception as e:
235243
raise InvoiceNotificationException("failed to deserialize BitPay server response (Invoice) : %s" % str(e))
236244

237-
def create_refund(self, invoice_id, amount, currency, preview=False, immediate=False, buyer_pays_refund_fee=False):
245+
def create_refund(self, invoice_id: str, amount: float, currency: str, preview: bool = False,
246+
immediate: bool = False, buyer_pays_refund_fee: bool = False) -> Refund:
238247
try:
239248
params = {"token": self.get_access_token(Facade.Merchant), "invoiceId": invoice_id, "amount": amount,
240249
"currency": currency, "preview": preview, "immediate": immediate,
241250
"buyerPaysRefundFee": buyer_pays_refund_fee}
242251

243252
response_json = self.__restcli.post("refunds", params, True)
253+
except BitPayException as e:
254+
raise RefundCreationException("failed to serialize refund object : %s" % e.get_message(),
255+
e.get_api_code())
244256
except Exception as e:
245-
print(e)
257+
raise RefundCreationException("failed to serialize refund object : %s" % e)
246258

247-
def get_refund(self, refund_id):
259+
try:
260+
refund = Refund(**response_json)
261+
except Exception as e:
262+
raise RefundCreationException("failed to deserialize BitPay server response (Refund) : %s" % str(e))
263+
264+
return refund
265+
266+
def get_refund(self, refund_id: str) -> Refund:
248267
try:
249268
params = {"token": self.get_access_token(Facade.Merchant)}
250269
response_json = self.__restcli.get("refunds/%s" % refund_id, params)
270+
except BitPayException as e:
271+
raise RefundQueryException("failed to serialize refund object : %s" % e.get_message(),
272+
e.get_api_code())
251273
except Exception as e:
252-
print(e)
274+
raise RefundQueryException("failed to serialize refund object : %s" % e)
275+
try:
276+
refund = Refund(**response_json)
277+
except Exception as e:
278+
raise RefundQueryException("failed to deserialize BitPay server response (Refund) : %s" % str(e))
279+
280+
return refund
253281

254-
def get_refunds(self, invoice_id):
282+
def get_refunds(self, invoice_id: str) -> [Refund]:
255283
try:
256284
params = {"token": self.get_access_token(Facade.Merchant), "invoiceId": invoice_id}
257285
response_json = self.__restcli.get("refunds", params)
286+
except BitPayException as e:
287+
raise RefundQueryException("failed to serialize refund object : %s" % e.get_message(),
288+
e.get_api_code())
258289
except Exception as e:
259-
print(e)
290+
raise RefundQueryException("failed to serialize refund object : %s" % e)
291+
292+
try:
293+
refunds = Refund(**response_json)
294+
except Exception as e:
295+
raise RefundQueryException("failed to deserialize BitPay server response (Refund) : %s" % str(e))
260296

261-
def update_refund(self, refund_id, status):
262-
params = {"token": self.get_access_token(Facade.Merchant)}
297+
return refunds
263298

264-
if status:
265-
params["status"] = status
299+
def update_refund(self, refund_id: str, status: str) -> Refund:
266300
try:
301+
params = {"token": self.get_access_token(Facade.Merchant), "status": status}
302+
267303
response_json = self.__restcli.update("refunds/%s" % refund_id, params)
304+
except BitPayException as e:
305+
raise RefundUpdateException("failed to serialize refund object : %s" % e.get_message(),
306+
e.get_api_code())
268307
except Exception as e:
269-
print(e)
308+
raise RefundUpdateException("failed to serialize refund object : %s" % e)
309+
310+
try:
311+
refund = Refund(**response_json)
312+
except Exception as e:
313+
raise RefundUpdateException("failed to deserialize BitPay server response (Refund) : %s" % str(e))
314+
315+
return refund
270316

271-
def cancel_refund(self, refund_id):
272-
pass
317+
def cancel_refund(self, refund_id: str) -> Refund:
318+
try:
319+
params = {"token": self.get_access_token(Facade.Merchant)}
320+
response_json = self.__restcli.delete("refunds/%s" % refund_id, params)
321+
except BitPayException as e:
322+
raise RefundCancellationException("failed to serialize refund object : %s" % e.get_message(),
323+
e.get_api_code())
324+
except Exception as e:
325+
raise RefundCancellationException("failed to serialize refund object : %s" % e)
326+
327+
try:
328+
refund = Refund(**response_json)
329+
except Exception as e:
330+
raise RefundCancellationException("failed to deserialize BitPay server response (Refund) : %s" % str(e))
331+
332+
return refund
333+
334+
def request_refund_notification(self, refund_id: str) -> bool:
335+
try:
336+
params = {"token": self.get_access_token(Facade.Merchant)}
337+
response_json = self.__restcli.post("refunds/%s" % refund_id + "/notifications", params, True)
338+
except BitPayException as e:
339+
raise RefundNotificationException("failed to serialize refund object : %s" % e.get_message(),
340+
e.get_api_code())
341+
except Exception as e:
342+
raise RefundNotificationException("failed to serialize refund object : %s" % e)
343+
344+
try:
345+
refund = Refund(**response_json)
346+
except Exception as e:
347+
raise RefundNotificationException("failed to deserialize BitPay server response (Refund) : %s" % str(e))
348+
349+
return refund
350+
351+
def get_supported_wallets(self) -> [Wallet]:
352+
try:
353+
response_json = self.__restcli.get("supportedWallets/", None)
354+
except BitPayException as e:
355+
raise WalletQueryException("failed to serialize wallet object : %s" % e.get_message(),
356+
e.get_api_code())
357+
except Exception as e:
358+
raise WalletQueryException("failed to serialize wallet object : %s" % e)
359+
360+
try:
361+
wallet = Wallet(**response_json)
362+
except Exception as e:
363+
raise WalletQueryException("failed to deserialize BitPay server response (Wallet) : %s" % str(e))
273364

274-
def request_refund_notification(self, refund_id):
275-
pass
365+
return wallet
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from src.bitpay_sdk.exceptions.refund_exception import RefundException
2+
3+
4+
class RefundCancellationException(RefundException):
5+
__bitpay_message = "Failed to cancel refund object"
6+
__bitpay_code = "BITPAY-REFUND-CANCEL"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=165, 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.refund_exception import RefundException
2+
3+
4+
class RefundCreationException(RefundException):
5+
__bitpay_message = "Failed to create refund"
6+
__bitpay_code = "BITPAY-REFUND-CREATE"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=162, 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 .bitpay_exception import BitPayException
2+
3+
4+
class RefundException(BitPayException):
5+
__bitpay_message = "An unexpected error occurred while trying to manage the refund"
6+
__bitpay_code = "BITPAY-REFUND-GENERIC"
7+
__api_code = ""
8+
9+
def __init__(self, message="", code=161, 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.refund_exception import RefundException
2+
3+
4+
class RefundNotificationException(RefundException):
5+
__bitpay_message = "Failed to send refund notification"
6+
__bitpay_code = "BITPAY-REFUND-NOTIFICATION"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=166, 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.refund_exception import RefundException
2+
3+
4+
class RefundQueryException(RefundException):
5+
__bitpay_message = "Failed to retrieve refund"
6+
__bitpay_code = "BITPAY-REFUND-GET"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=163, 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.refund_exception import RefundException
2+
3+
4+
class RefundUpdateException(RefundException):
5+
__bitpay_message = "Failed to update refund"
6+
__bitpay_code = "BITPAY-REFUND-UPDATE"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=164, 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 .bitpay_exception import BitPayException
2+
3+
4+
class WalletException(BitPayException):
5+
__bitpay_message = "An unexpected error occurred while trying to manage the wallet"
6+
__bitpay_code = "BITPAY-WALLET-GENERIC"
7+
__api_code = ""
8+
9+
def __init__(self, message="", code=181, 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.wallet_exception import WalletException
2+
3+
4+
class WalletQueryException(WalletException):
5+
__bitpay_message = "Failed to retrieve supported wallets"
6+
__bitpay_code = "BITPAY-WALLET-GET"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=183, 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

0 commit comments

Comments
 (0)