Skip to content

Commit 7a3819a

Browse files
committed
fixed json encoder bug
1 parent 8db09da commit 7a3819a

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

geopayment/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
TBCInstallmentProvider,
66
)
77

8-
__version__ = "0.6.1"
8+
__version__ = "0.6.2"

geopayment/providers/tbc/installment/__init__.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ def auth(self, **kwargs: Optional[Any]) -> AuthData:
143143
144144
"""
145145

146-
self.http_status_code = kwargs['http_status_code']
146+
self.http_status_code = kwargs['HTTP_STATUS_CODE']
147147
result = kwargs['result']
148-
if 'fault' in result:
148+
if 'fault' in result or 'error' in result:
149149
return result
150150
self.auth = AuthData(**result)
151151
return self.auth
@@ -189,7 +189,7 @@ def create(self, **kwargs: Optional[Any]) -> Dict[str, str]:
189189
190190
"""
191191

192-
self.http_status_code = kwargs['http_status_code']
192+
self.http_status_code = kwargs['HTTP_STATUS_CODE']
193193
self.session_id = kwargs['result'].get('sessionId')
194194
self.redirect_url = kwargs['headers'].get('location')
195195
if self.session_id and self.redirect_url:
@@ -224,7 +224,7 @@ def confirm(self, **kwargs: Optional[Any]) -> Dict[str, str]:
224224
225225
"""
226226

227-
self.http_status_code = kwargs['http_status_code']
227+
self.http_status_code = kwargs['HTTP_STATUS_CODE']
228228
return kwargs['result']
229229

230230
@tbc_installment_params(
@@ -252,7 +252,7 @@ def cancel(self, **kwargs: Optional[Any]) -> Dict[str, str]:
252252
HTTP request has been successfully completed.
253253
254254
"""
255-
self.http_status_code = kwargs['http_status_code']
255+
self.http_status_code = kwargs['HTTP_STATUS_CODE']
256256
return kwargs['result']
257257

258258
@tbc_installment_params(
@@ -290,7 +290,7 @@ def status(self, **kwargs: Optional[Any]) -> Dict[str, str]:
290290
HTTP request has been successfully completed.
291291
292292
"""
293-
self.http_status_code = kwargs['http_status_code']
293+
self.http_status_code = kwargs['HTTP_STATUS_CODE']
294294
return kwargs['result']
295295

296296
@tbc_installment_params(
@@ -325,7 +325,7 @@ def statuses(self, **kwargs: Optional[Any]) -> Dict[str, str]:
325325
HTTP request has been successfully completed.
326326
327327
"""
328-
self.http_status_code = kwargs['http_status_code']
328+
self.http_status_code = kwargs['HTTP_STATUS_CODE']
329329
return kwargs['result']
330330

331331
@tbc_installment_params(
@@ -352,5 +352,5 @@ def status_sync(self, **kwargs: Optional[Any]) -> Dict[str, str]:
352352
HTTP request has been successfully completed.
353353
354354
"""
355-
self.http_status_code = kwargs['http_status_code']
355+
self.http_status_code = kwargs['HTTP_STATUS_CODE']
356356
return kwargs['result']

geopayment/providers/utils.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
@author: Lasha Gogua
77
"""
8-
8+
import datetime
99
import json
1010
from decimal import Decimal, ROUND_UP
1111
from functools import wraps
@@ -88,6 +88,17 @@ def get_currency_code(code):
8888
return ALLOW_CURRENCY_CODES[code]
8989

9090

91+
class JsonEncoder(json.JSONEncoder):
92+
93+
def default(self, o):
94+
if isinstance(o, datetime.datetime):
95+
return o.isoformat()
96+
if isinstance(o, Decimal):
97+
return str(o)
98+
99+
return super().default(o)
100+
101+
91102
def perform_http_response(response: requests.Response):
92103
"""
93104
:param response: Response object from HTTP Request
@@ -122,24 +133,29 @@ def wrapped(*args, **kwargs):
122133
method = kwargs['method']
123134
request_params['url'] = kwargs.get('url', klass.service_url)
124135
request_params['method'] = method
125-
request_params.update(kwargs['payload'])
136+
payload = kwargs['payload']
137+
if 'json' in payload:
138+
payload['json'] = json.loads(
139+
json.dumps(payload.pop('json'), cls=JsonEncoder)
140+
)
141+
request_params.update(payload)
126142
request_params['headers'] = kwargs.get('headers')
127-
request_params['verify'] = kwargs['verify']
128-
request_params['timeout'] = kwargs['timeout']
143+
request_params['verify'] = kwargs.get('verify', True)
144+
request_params['timeout'] = kwargs.get('timeout', 4)
129145
request_params['cert'] = getattr(klass, 'cert', None)
130146
if method == 'get':
131147
request_params['allow_redirects'] = True
132148

133149
try:
134150
resp = requests.request(**request_params)
135-
kwargs['http_status_code'] = resp.status_code
151+
kwargs['HTTP_STATUS_CODE'] = resp.status_code
136152
result = perform_http_response(resp)
137153
kwargs['headers'] = resp.headers
138154
except requests.exceptions.RequestException as e:
139155
result = {'ERROR': str(e)}
140156
kwargs['headers'] = dict()
141-
if 'http_status_code' not in kwargs:
142-
kwargs['http_status_code'] = 'N/A'
157+
if 'HTTP_STATUS_CODE' not in kwargs:
158+
kwargs['HTTP_STATUS_CODE'] = 'N/A'
143159

144160
return f(result=result, *args, **kwargs)
145161

0 commit comments

Comments
 (0)