|
6 | 6 | from .models.facade import Facade |
7 | 7 | from .utils.rest_cli import RESTcli |
8 | 8 | 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 |
9 | 13 | from .exceptions.bitpay_exception import BitPayException |
10 | 14 |
|
11 | 15 |
|
@@ -128,61 +132,144 @@ def get_access_token(self, key: str): |
128 | 132 | # //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
129 | 133 | # //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
130 | 134 |
|
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: |
132 | 136 | try: |
133 | 137 | invoice.set_token(self.get_access_token(facade)) |
134 | 138 | invoice_json = invoice.to_json() |
135 | 139 | response_json = self.__restcli.post("invoices", invoice_json, sign_request) |
136 | 140 | 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) |
138 | 144 |
|
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: |
140 | 153 | try: |
141 | 154 | params = {"token": self.get_access_token(facade)} |
142 | 155 | response_json = self.__restcli.get("invoices/%s" % invoice_id, params, sign_request) |
143 | | - return Invoice(**response_json) |
144 | 156 | 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) |
146 | 160 |
|
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]: |
148 | 170 | try: |
149 | 171 | 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 | + |
150 | 181 | response_json = self.__restcli.get("invoices/", parameters=params) |
151 | 182 | 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) |
153 | 186 |
|
154 | | - def update_invoice(self, invoice_id, buyer_sms, sms_code, buyer_email): |
155 | 187 | 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)) |
166 | 191 |
|
167 | | - if buyer_email is not None: |
168 | | - params['buyer_email'] = buyer_email |
| 192 | + return invoices |
169 | 193 |
|
| 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} |
170 | 197 | response_json = self.__restcli.update("invoices/%s" % invoice_id, json.dumps(params)) |
171 | 198 | 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) |
173 | 202 |
|
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: |
175 | 211 | try: |
176 | 212 | params = {'token': self.get_access_token(Facade.Merchant)} |
177 | 213 | response_json = self.__restcli.delete("invoices/%s" % invoice_id, params) |
178 | 214 | 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()} |
180 | 231 |
|
181 | | - def get_invoice_webhook(self, invoice_id): |
182 | 232 | try: |
183 | | - self.get_invoice(invoice_id) |
184 | | - params = {} |
185 | | - # Conditions missing look from node |
186 | 233 | 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: |
188 | 245 | 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 |
0 commit comments