Skip to content

Commit b6920a5

Browse files
committed
Created models and exceptions related to invoice
1 parent 5682e32 commit b6920a5

15 files changed

+501
-109
lines changed

src/bitpay_sdk/client.py

Lines changed: 115 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
from .models.facade import Facade
77
from .utils.rest_cli import RESTcli
88
from .models.invoice.invoice import Invoice
9+
from .exceptions.invoice_query_exception import InvoiceQueryException
10+
from .exceptions.invoice_update_exception import InvoiceUpdateException
11+
from .exceptions.invoice_cancellation_exception import InvoiceCancellationException
12+
from .exceptions.invoice_notification_exception import InvoiceNotificationException
913
from .exceptions.bitpay_exception import BitPayException
1014

1115

@@ -128,61 +132,144 @@ def get_access_token(self, key: str):
128132
# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129133
# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130134

131-
def create_invoice(self, invoice: Invoice, facade, sign_request=True):
135+
def create_invoice(self, invoice: Invoice, facade: str = Facade.Merchant, sign_request: bool = True) -> Invoice:
132136
try:
133137
invoice.set_token(self.get_access_token(facade))
134138
invoice_json = invoice.to_json()
135139
response_json = self.__restcli.post("invoices", invoice_json, sign_request)
136140
except BitPayException as e:
137-
print(e)
141+
raise InvoiceQueryException("failed to serialize Invoice object : %s" % e.get_message(), e.get_api_code())
142+
except Exception as e:
143+
raise InvoiceQueryException("failed to serialize Invoice object : %s" % e)
138144

139-
def get_invoice(self, invoice_id, facade, sign_request=True):
145+
try:
146+
invoice = Invoice(**response_json)
147+
except Exception as e:
148+
raise InvoiceQueryException("failed to deserialize BitPay server response (Invoice) : %s" % str(e))
149+
150+
return invoice
151+
152+
def get_invoice(self, invoice_id: str, facade: str = Facade.Merchant, sign_request: bool = True) -> Invoice:
140153
try:
141154
params = {"token": self.get_access_token(facade)}
142155
response_json = self.__restcli.get("invoices/%s" % invoice_id, params, sign_request)
143-
return Invoice(**response_json)
144156
except BitPayException as e:
145-
print(e)
157+
raise InvoiceQueryException("failed to serialize Invoice object : %s" % e.get_message(), e.get_api_code())
158+
except Exception as e:
159+
raise InvoiceQueryException("failed to serialize Invoice object : %s" % e)
146160

147-
def get_invoices(self, date_start, date_end, status, order_id, limit, offset):
161+
try:
162+
invoice = Invoice(**response_json)
163+
except Exception as e:
164+
raise InvoiceQueryException("failed to deserialize BitPay server response (Invoice) : %s" % str(e))
165+
166+
return invoice
167+
168+
def get_invoices(self, date_start: str, date_end: str, status: str = None, order_id: str = None, limit: int = None,
169+
offset: int = None) -> [Invoice]:
148170
try:
149171
params = {"token": self.get_access_token(Facade.Merchant), "dateStart": date_start, "date_end": date_end}
172+
if status:
173+
params["status"] = status
174+
if order_id:
175+
params["order_id"] = order_id
176+
if limit:
177+
params["limit"] = limit
178+
if offset:
179+
params["offset"] = offset
180+
150181
response_json = self.__restcli.get("invoices/", parameters=params)
151182
except BitPayException as e:
152-
print(e)
183+
raise InvoiceQueryException("failed to serialize Invoice object : %s" % e.get_message(), e.get_api_code())
184+
except Exception as e:
185+
raise InvoiceQueryException("failed to serialize Invoice object : %s" % e)
153186

154-
def update_invoice(self, invoice_id, buyer_sms, sms_code, buyer_email):
155187
try:
156-
params = {'token': self.get_access_token(Facade.Merchant)}
157-
158-
if buyer_sms & sms_code:
159-
pass
160-
161-
if buyer_sms is not None:
162-
params['buyer_sms'] = buyer_sms
163-
164-
if sms_code is not None:
165-
params['sms_code'] = sms_code
188+
invoices = Invoice(**response_json)
189+
except Exception as e:
190+
raise InvoiceQueryException("failed to deserialize BitPay server response (Invoice) : %s" % str(e))
166191

167-
if buyer_email is not None:
168-
params['buyer_email'] = buyer_email
192+
return invoices
169193

194+
def update_invoice(self, invoice_id: str, buyer_email: str) -> Invoice:
195+
try:
196+
params = {'token': self.get_access_token(Facade.Merchant), 'buyer_email': buyer_email}
170197
response_json = self.__restcli.update("invoices/%s" % invoice_id, json.dumps(params))
171198
except BitPayException as e:
172-
print(e)
199+
raise InvoiceUpdateException("failed to serialize Invoice object : %s" % e.get_message(), e.get_api_code())
200+
except Exception as e:
201+
raise InvoiceUpdateException("failed to serialize Invoice object : %s" % e)
173202

174-
def cancel_invoice(self, invoice_id):
203+
try:
204+
invoice = Invoice(**response_json)
205+
except Exception as e:
206+
raise InvoiceUpdateException("failed to deserialize BitPay server response (Invoice) : %s" % str(e))
207+
208+
return invoice
209+
210+
def cancel_invoice(self, invoice_id: str) -> Invoice:
175211
try:
176212
params = {'token': self.get_access_token(Facade.Merchant)}
177213
response_json = self.__restcli.delete("invoices/%s" % invoice_id, params)
178214
except BitPayException as e:
179-
print(e)
215+
raise InvoiceCancellationException("failed to serialize Invoice object : %s" % e.get_message(), e.get_api_code())
216+
except Exception as e:
217+
raise InvoiceCancellationException("failed to serialize Invoice object : %s" % e)
218+
219+
try:
220+
invoice = Invoice(**response_json)
221+
except Exception as e:
222+
raise InvoiceCancellationException("failed to deserialize BitPay server response (Invoice) : %s" % str(e))
223+
return invoice
224+
225+
def request_invoice_notifications(self, invoice_id: str) -> bool:
226+
try:
227+
invoice = self.get_invoice(invoice_id)
228+
except Exception as e:
229+
raise InvoiceNotificationException(f"Invoice with ID: " + {invoice_id} + " Not Found : " + e.message, str(e))
230+
params = {"token": invoice.get_token()}
180231

181-
def get_invoice_webhook(self, invoice_id):
182232
try:
183-
self.get_invoice(invoice_id)
184-
params = {}
185-
# Conditions missing look from node
186233
response_json = self.__restcli.post("invoices/%s" % invoice_id + "/notifications", params)
187-
except BitPayException as e:
234+
except Exception as e:
235+
raise InvoiceNotificationException("failed to deserialize BitPay server response (Invoice) : %s" % str(e))
236+
237+
def create_refund(self, invoice_id, amount, currency, preview=False, immediate=False, buyer_pays_refund_fee=False):
238+
try:
239+
params = {"token": self.get_access_token(Facade.Merchant), "invoiceId": invoice_id, "amount": amount,
240+
"currency": currency, "preview": preview, "immediate": immediate,
241+
"buyerPaysRefundFee": buyer_pays_refund_fee}
242+
243+
response_json = self.__restcli.post("refunds", params, True)
244+
except Exception as e:
188245
print(e)
246+
247+
def get_refund(self, refund_id):
248+
try:
249+
params = {"token": self.get_access_token(Facade.Merchant)}
250+
response_json = self.__restcli.get("refunds/%s" % refund_id, params)
251+
except Exception as e:
252+
print(e)
253+
254+
def get_refunds(self, invoice_id):
255+
try:
256+
params = {"token": self.get_access_token(Facade.Merchant), "invoiceId": invoice_id}
257+
response_json = self.__restcli.get("refunds", params)
258+
except Exception as e:
259+
print(e)
260+
261+
def update_refund(self, refund_id, status):
262+
params = {"token": self.get_access_token(Facade.Merchant)}
263+
264+
if status:
265+
params["status"] = status
266+
try:
267+
response_json = self.__restcli.update("refunds/%s" % refund_id, params)
268+
except Exception as e:
269+
print(e)
270+
271+
def cancel_refund(self, refund_id):
272+
pass
273+
274+
def request_refund_notification(self, refund_id):
275+
pass
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1+
2+
13
class BitPayException(Exception):
24
__bitpay_message = "Unexpected Bitpay exception."
35
__bitpay_code = "BITPAY-GENERIC";
4-
__apicode = ""
5-
6-
"""
7-
Construct the BitPayException.
8-
9-
message (string) message [optional] The Exception message to throw.
10-
code (int) $code [optional] The Exception code to throw.
11-
@param string $apiCode [optional] The API Exception code to throw.
12-
"""
6+
__api_code = ""
137

14-
def __init__(self, message="", code=100, apicode="000000"):
15-
if not message:
16-
message = self.__bitpay_code + ": " + self.__bitpaymessage + ":" + message
17-
self.__apicode = apicode
18-
super().__init__(message)
8+
def __init__(self, message, code=100, api_code="000000"):
9+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
10+
self.__api_code = api_code
11+
super().__init__(message,code)
1912

20-
"""
21-
@return string Error code provided by the BitPay REST API
22-
"""
23-
def get_apicode(self):
24-
return self.__apicode
13+
def get_api_code(self):
14+
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.invoice_exception import InvoiceException
2+
3+
4+
class InvoiceCancellationException(InvoiceException):
5+
__bitpay_message = "Failed to cancel invoice object"
6+
__bitpay_code = "BITPAY-INVOICE-CANCEL"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=104, 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.invoice_exception import InvoiceException
2+
3+
4+
class InvoiceCreationException(InvoiceException):
5+
__bitpay_message = "Failed to create invoice"
6+
__bitpay_code = "BITPAY-INVOICE-CREATE"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=102, 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: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
1-
from bitpay_exception import BitPayException
1+
from .bitpay_exception import BitPayException
22

33

44
class InvoiceException(BitPayException):
55
__bitpay_message = "An unexpected error occurred while trying to manage the invoice"
66
__bitpay_code = "BITPAY-INVOICE-GENERIC"
7-
__apicode = ""
7+
__api_code = ""
88

9-
'''
10-
Construct the InvoiceException.
11-
@param string $message [optional] The Exception message to throw.
12-
@param int $code [optional] The Exception code to throw.
13-
@param string $apicode [optional] The API Exception code to throw.
14-
'''
9+
def __init__(self, message="", code=100, api_code="000000"):
10+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
11+
self.__api_code = api_code
12+
super().__init__(message, code)
1513

16-
def __init__(self, message="", code=100, apicode="000000"):
17-
if not message:
18-
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
19-
self.__apicode = apicode
20-
super().__init__(message)
21-
22-
"""
23-
@return string Error code provided by the BitPay REST API
24-
"""
25-
26-
def get_apicode(self):
27-
return self.__apicode
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.invoice_exception import InvoiceException
2+
3+
4+
class InvoiceNotificationException(InvoiceException):
5+
__bitpay_message = "Failed to send invoice notification"
6+
__bitpay_code = "BITPAY-INVOICE-NOTIFICATION"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=102, 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.invoice_exception import InvoiceException
2+
3+
4+
class InvoiceQueryException(InvoiceException):
5+
__bitpay_message = "Failed to retrieve invoice"
6+
__bitpay_code = "BITPAY-INVOICE-GET"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=103, 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.invoice_exception import InvoiceException
2+
3+
4+
class InvoiceUpdateException(InvoiceException):
5+
__bitpay_message = "Failed to update invoice"
6+
__bitpay_code = "BITPAY-INVOICE-UPDATE"
7+
__api_code = ""
8+
9+
def __init__(self, message, code=104, 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)