|
5 | 5 | from .tokens import Tokens |
6 | 6 | from .models.facade import Facade |
7 | 7 | from .utils.rest_cli import RESTcli |
| 8 | +from .models.wallet.wallet import Wallet |
| 9 | +from .models.invoice.refund import Refund |
8 | 10 | 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 |
9 | 15 | from .exceptions.invoice_query_exception import InvoiceQueryException |
10 | 16 | 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 |
11 | 20 | from .exceptions.invoice_cancellation_exception import InvoiceCancellationException |
12 | 21 | from .exceptions.invoice_notification_exception import InvoiceNotificationException |
13 | | -from .exceptions.bitpay_exception import BitPayException |
14 | 22 |
|
15 | 23 |
|
16 | 24 | class Client: |
@@ -234,42 +242,124 @@ def request_invoice_notifications(self, invoice_id: str) -> bool: |
234 | 242 | except Exception as e: |
235 | 243 | raise InvoiceNotificationException("failed to deserialize BitPay server response (Invoice) : %s" % str(e)) |
236 | 244 |
|
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: |
238 | 247 | try: |
239 | 248 | params = {"token": self.get_access_token(Facade.Merchant), "invoiceId": invoice_id, "amount": amount, |
240 | 249 | "currency": currency, "preview": preview, "immediate": immediate, |
241 | 250 | "buyerPaysRefundFee": buyer_pays_refund_fee} |
242 | 251 |
|
243 | 252 | 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()) |
244 | 256 | except Exception as e: |
245 | | - print(e) |
| 257 | + raise RefundCreationException("failed to serialize refund object : %s" % e) |
246 | 258 |
|
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: |
248 | 267 | try: |
249 | 268 | params = {"token": self.get_access_token(Facade.Merchant)} |
250 | 269 | 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()) |
251 | 273 | 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 |
253 | 281 |
|
254 | | - def get_refunds(self, invoice_id): |
| 282 | + def get_refunds(self, invoice_id: str) -> [Refund]: |
255 | 283 | try: |
256 | 284 | params = {"token": self.get_access_token(Facade.Merchant), "invoiceId": invoice_id} |
257 | 285 | 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()) |
258 | 289 | 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)) |
260 | 296 |
|
261 | | - def update_refund(self, refund_id, status): |
262 | | - params = {"token": self.get_access_token(Facade.Merchant)} |
| 297 | + return refunds |
263 | 298 |
|
264 | | - if status: |
265 | | - params["status"] = status |
| 299 | + def update_refund(self, refund_id: str, status: str) -> Refund: |
266 | 300 | try: |
| 301 | + params = {"token": self.get_access_token(Facade.Merchant), "status": status} |
| 302 | + |
267 | 303 | 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()) |
268 | 307 | 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 |
270 | 316 |
|
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)) |
273 | 364 |
|
274 | | - def request_refund_notification(self, refund_id): |
275 | | - pass |
| 365 | + return wallet |
0 commit comments