Skip to content

Commit

Permalink
fixed json encoder bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Lh4cKg committed Dec 27, 2022
1 parent 8db09da commit 7a3819a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion geopayment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
TBCInstallmentProvider,
)

__version__ = "0.6.1"
__version__ = "0.6.2"
16 changes: 8 additions & 8 deletions geopayment/providers/tbc/installment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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']
30 changes: 23 additions & 7 deletions geopayment/providers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@author: Lasha Gogua
"""

import datetime
import json
from decimal import Decimal, ROUND_UP
from functools import wraps
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 7a3819a

Please sign in to comment.