From 7a3819ab6c4e08f212c3ec7af213b7f195d31b56 Mon Sep 17 00:00:00 2001 From: Lasha Gogua Date: Wed, 28 Dec 2022 00:30:42 +0400 Subject: [PATCH] fixed json encoder bug --- geopayment/__init__.py | 2 +- .../providers/tbc/installment/__init__.py | 16 +++++----- geopayment/providers/utils.py | 30 ++++++++++++++----- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/geopayment/__init__.py b/geopayment/__init__.py index 8aed262..165202f 100644 --- a/geopayment/__init__.py +++ b/geopayment/__init__.py @@ -5,4 +5,4 @@ TBCInstallmentProvider, ) -__version__ = "0.6.1" +__version__ = "0.6.2" diff --git a/geopayment/providers/tbc/installment/__init__.py b/geopayment/providers/tbc/installment/__init__.py index d3741b8..198cb80 100644 --- a/geopayment/providers/tbc/installment/__init__.py +++ b/geopayment/providers/tbc/installment/__init__.py @@ -143,9 +143,9 @@ def auth(self, **kwargs: Optional[Any]) -> AuthData: """ - self.http_status_code = kwargs['http_status_code'] + self.http_status_code = kwargs['HTTP_STATUS_CODE'] result = kwargs['result'] - if 'fault' in result: + if 'fault' in result or 'error' in result: return result self.auth = AuthData(**result) return self.auth @@ -189,7 +189,7 @@ def create(self, **kwargs: Optional[Any]) -> Dict[str, str]: """ - self.http_status_code = kwargs['http_status_code'] + self.http_status_code = kwargs['HTTP_STATUS_CODE'] self.session_id = kwargs['result'].get('sessionId') self.redirect_url = kwargs['headers'].get('location') if self.session_id and self.redirect_url: @@ -224,7 +224,7 @@ def confirm(self, **kwargs: Optional[Any]) -> Dict[str, str]: """ - self.http_status_code = kwargs['http_status_code'] + self.http_status_code = kwargs['HTTP_STATUS_CODE'] return kwargs['result'] @tbc_installment_params( @@ -252,7 +252,7 @@ def cancel(self, **kwargs: Optional[Any]) -> Dict[str, str]: HTTP request has been successfully completed. """ - self.http_status_code = kwargs['http_status_code'] + self.http_status_code = kwargs['HTTP_STATUS_CODE'] return kwargs['result'] @tbc_installment_params( @@ -290,7 +290,7 @@ def status(self, **kwargs: Optional[Any]) -> Dict[str, str]: HTTP request has been successfully completed. """ - self.http_status_code = kwargs['http_status_code'] + self.http_status_code = kwargs['HTTP_STATUS_CODE'] return kwargs['result'] @tbc_installment_params( @@ -325,7 +325,7 @@ def statuses(self, **kwargs: Optional[Any]) -> Dict[str, str]: HTTP request has been successfully completed. """ - self.http_status_code = kwargs['http_status_code'] + self.http_status_code = kwargs['HTTP_STATUS_CODE'] return kwargs['result'] @tbc_installment_params( @@ -352,5 +352,5 @@ def status_sync(self, **kwargs: Optional[Any]) -> Dict[str, str]: HTTP request has been successfully completed. """ - self.http_status_code = kwargs['http_status_code'] + self.http_status_code = kwargs['HTTP_STATUS_CODE'] return kwargs['result'] diff --git a/geopayment/providers/utils.py b/geopayment/providers/utils.py index beb2390..6bce5ae 100644 --- a/geopayment/providers/utils.py +++ b/geopayment/providers/utils.py @@ -5,7 +5,7 @@ @author: Lasha Gogua """ - +import datetime import json from decimal import Decimal, ROUND_UP from functools import wraps @@ -88,6 +88,17 @@ def get_currency_code(code): return ALLOW_CURRENCY_CODES[code] +class JsonEncoder(json.JSONEncoder): + + def default(self, o): + if isinstance(o, datetime.datetime): + return o.isoformat() + if isinstance(o, Decimal): + return str(o) + + return super().default(o) + + def perform_http_response(response: requests.Response): """ :param response: Response object from HTTP Request @@ -122,24 +133,29 @@ def wrapped(*args, **kwargs): method = kwargs['method'] request_params['url'] = kwargs.get('url', klass.service_url) request_params['method'] = method - request_params.update(kwargs['payload']) + payload = kwargs['payload'] + if 'json' in payload: + payload['json'] = json.loads( + json.dumps(payload.pop('json'), cls=JsonEncoder) + ) + request_params.update(payload) request_params['headers'] = kwargs.get('headers') - request_params['verify'] = kwargs['verify'] - request_params['timeout'] = kwargs['timeout'] + request_params['verify'] = kwargs.get('verify', True) + request_params['timeout'] = kwargs.get('timeout', 4) request_params['cert'] = getattr(klass, 'cert', None) if method == 'get': request_params['allow_redirects'] = True try: resp = requests.request(**request_params) - kwargs['http_status_code'] = resp.status_code + kwargs['HTTP_STATUS_CODE'] = resp.status_code result = perform_http_response(resp) kwargs['headers'] = resp.headers except requests.exceptions.RequestException as e: result = {'ERROR': str(e)} kwargs['headers'] = dict() - if 'http_status_code' not in kwargs: - kwargs['http_status_code'] = 'N/A' + if 'HTTP_STATUS_CODE' not in kwargs: + kwargs['HTTP_STATUS_CODE'] = 'N/A' return f(result=result, *args, **kwargs)