Skip to content

Commit 3b13d24

Browse files
committed
update module
1 parent 259f38e commit 3b13d24

File tree

6 files changed

+97
-103
lines changed

6 files changed

+97
-103
lines changed

docs/tbc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class MyTBCProvider(TBCProvider):
3737
return 'xx.xx.xx.xx'
3838

3939
@property
40-
def merchant_url(self) -> str:
40+
def service_url(self) -> str:
4141
return 'https://ecommerce.ufc.ge:18443/ecomm2/MerchantHandler'
4242

4343
@property

geopayment/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from geopayment.providers import TBCProvider, IPayProvider
22

3-
__version__ = '0.3.2'
3+
__version__ = '0.4.0'

geopayment/providers/bog/provider.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from base64 import b64encode
1010
from typing import Optional, Any, Dict
1111

12-
from geopayment.providers.utils import bog_request
12+
from geopayment.providers.utils import _request, bog_params
1313

1414

1515
__all__ = ['IPayProvider']
@@ -76,8 +76,8 @@ def get_credentials(self) -> bytes:
7676

7777
class IPayProvider(BaseIPayProvider):
7878

79-
@bog_request(verify=True, timeout=(3, 10), endpoint='oauth2/token',
80-
method='post', api='auth')
79+
@bog_params(endpoint='oauth2/token', api='auth')
80+
@_request(verify=True, timeout=(3, 10), method='post')
8181
def get_auth(self, **kwargs: Optional[Any]) -> Dict[str, str]:
8282
"""
8383
checkout api docs: https://developer.ipay.ge/checkout
@@ -89,8 +89,8 @@ def get_auth(self, **kwargs: Optional[Any]) -> Dict[str, str]:
8989
self.access = kwargs['result']
9090
return self.access
9191

92-
@bog_request(verify=True, timeout=(3, 10), currency_code='GEL',
93-
endpoint='checkout/orders', method='post', api='checkout')
92+
@bog_params(currency_code='GEL', endpoint='checkout/orders', api='checkout')
93+
@_request(verify=True, timeout=(3, 10), method='post')
9494
def checkout(self, **kwargs: Optional[Any]) -> Dict[str, str]:
9595
"""
9696
checkout api docs: https://developer.ipay.ge/checkout
@@ -109,8 +109,8 @@ def checkout(self, **kwargs: Optional[Any]) -> Dict[str, str]:
109109
self.rel_approve = link[0]['href']
110110
return result
111111

112-
@bog_request(verify=True, timeout=(3, 10), endpoint='checkout/refund',
113-
method='post', api='refund')
112+
@bog_params(endpoint='checkout/refund', api='refund')
113+
@_request(verify=True, timeout=(3, 10), method='post')
114114
def refund(self, **kwargs: Optional[Any]) -> Dict[str, str]:
115115
"""
116116
@@ -120,9 +120,8 @@ def refund(self, **kwargs: Optional[Any]) -> Dict[str, str]:
120120

121121
return kwargs['result']
122122

123-
@bog_request(verify=True, timeout=(3, 10),
124-
endpoint='checkout/orders/status/{order_id}', method='get',
125-
api='status')
123+
@bog_params(endpoint='checkout/orders/status/{order_id}', api='status')
124+
@_request(verify=True, timeout=(3, 10), method='get')
126125
def checkout_status(self, **kwargs: Optional[Any]) -> Dict[str, str]:
127126
"""
128127
@@ -132,9 +131,8 @@ def checkout_status(self, **kwargs: Optional[Any]) -> Dict[str, str]:
132131

133132
return kwargs['result']
134133

135-
@bog_request(verify=True, timeout=(3, 10),
136-
endpoint='checkout/orders/{order_id}', method='get',
137-
api='details')
134+
@bog_params(endpoint='checkout/orders/{order_id}', api='details')
135+
@_request(verify=True, timeout=(3, 10), method='get')
138136
def checkout_details(self, **kwargs: Optional[Any]) -> Dict[str, str]:
139137
"""
140138
@@ -144,9 +142,8 @@ def checkout_details(self, **kwargs: Optional[Any]) -> Dict[str, str]:
144142

145143
return kwargs['result']
146144

147-
@bog_request(verify=True, timeout=(3, 10),
148-
endpoint='checkout/payment/{order_id}', method='get',
149-
api='payment')
145+
@bog_params(endpoint='checkout/payment/{order_id}', api='payment')
146+
@_request(verify=True, timeout=(3, 10), method='get')
150147
def payment_details(self, **kwargs: Optional[Any]) -> Dict[str, str]:
151148
"""
152149

geopayment/providers/tbc/provider.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from typing import Dict, Any, Optional, Tuple
1010

11-
from geopayment.providers.utils import tbc_params, tbc_request
11+
from geopayment.providers.utils import _request, tbc_params
1212

1313

1414
__all__ = ['TBCProvider']
@@ -24,8 +24,8 @@ def __init__(self) -> None:
2424
'`client_ip` must be property, not callable'
2525
assert callable(self.cert) is False, \
2626
'`cert` must be property, not callable'
27-
assert callable(self.merchant_url) is False, \
28-
'`merchant_url` must be property, not callable'
27+
assert callable(self.service_url) is False, \
28+
'`service_url` must be property, not callable'
2929

3030
@property
3131
def description(self) -> str:
@@ -58,21 +58,21 @@ def cert(self) -> Tuple[str, str]:
5858
)
5959

6060
@property
61-
def merchant_url(self) -> str:
61+
def service_url(self) -> str:
6262
"""
6363
6464
:return: merchant service url
6565
"""
6666
raise NotImplementedError(
67-
'Provider needs implement `merchant_url` function'
67+
'Provider needs implement `service_url` function'
6868
)
6969

7070

7171
class TBCProvider(BaseTBCProvider):
7272

7373
@tbc_params('amount', 'currency', 'client_ip_addr',
7474
'description', command='v', language='ka', msg_type='SMS')
75-
@tbc_request(verify=False, timeout=(3, 10))
75+
@_request(verify=False, timeout=(3, 10), method='post')
7676
def get_trans_id(self, **kwargs: Optional[Any]) -> Dict[str, str]:
7777
"""
7878
command: Transaction type
@@ -97,7 +97,7 @@ def get_trans_id(self, **kwargs: Optional[Any]) -> Dict[str, str]:
9797
return result
9898

9999
@tbc_params('trans_id', 'client_ip_addr', command='c')
100-
@tbc_request(verify=False, timeout=(3, 10))
100+
@_request(verify=False, timeout=(3, 10), method='post')
101101
def check_trans_status(self, **kwargs: Optional[Any]) -> Dict[str, str]:
102102
"""
103103
command: Transaction type
@@ -124,7 +124,7 @@ def check_trans_status(self, **kwargs: Optional[Any]) -> Dict[str, str]:
124124
return kwargs['result']
125125

126126
@tbc_params('trans_id', 'amount', command='r')
127-
@tbc_request(verify=False, timeout=(3, 10))
127+
@_request(verify=False, timeout=(3, 10), method='post')
128128
def reversal_trans(self, **kwargs: Optional[Any]) -> Dict[str, str]:
129129
"""
130130
command: Transaction type
@@ -146,7 +146,7 @@ def reversal_trans(self, **kwargs: Optional[Any]) -> Dict[str, str]:
146146
return kwargs['result']
147147

148148
@tbc_params('trans_id', 'amount', command='k')
149-
@tbc_request(verify=False, timeout=(3, 10))
149+
@_request(verify=False, timeout=(3, 10), method='post')
150150
def refund_trans(self, **kwargs: Optional[Any]) -> Dict[str, str]:
151151
"""
152152
command: Transaction type
@@ -170,7 +170,7 @@ def refund_trans(self, **kwargs: Optional[Any]) -> Dict[str, str]:
170170

171171
@tbc_params('amount', 'currency', 'client_ip_addr', 'description',
172172
command='a', language='ka', msg_type='DMS')
173-
@tbc_request(verify=False, timeout=(3, 10))
173+
@_request(verify=False, timeout=(3, 10), method='post')
174174
def pre_auth_trans(self, **kwargs: Optional[Any]) -> Dict[str, str]:
175175
"""
176176
command: Transaction type
@@ -195,7 +195,7 @@ def pre_auth_trans(self, **kwargs: Optional[Any]) -> Dict[str, str]:
195195

196196
@tbc_params('trans_id', 'amount', 'currency', 'client_ip_addr',
197197
'description', command='t', language='ka', msg_type='DMS')
198-
@tbc_request(verify=False, timeout=(3, 10))
198+
@_request(verify=False, timeout=(3, 10), method='post')
199199
def confirm_pre_auth_trans(self, **kwargs: Optional[Any]) -> Dict[str, str]:
200200
"""
201201
command: Transaction type
@@ -222,7 +222,7 @@ def confirm_pre_auth_trans(self, **kwargs: Optional[Any]) -> Dict[str, str]:
222222
return kwargs['result']
223223

224224
@tbc_params(command='b')
225-
@tbc_request(verify=False, timeout=(3, 10))
225+
@_request(verify=False, timeout=(3, 10), method='post')
226226
def end_of_business_day(self, **kwargs: Optional[Any]) -> Dict[str, str]:
227227
"""
228228
command: Transaction type

geopayment/providers/utils.py

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import json
1010
from decimal import Decimal, ROUND_UP
1111
from functools import wraps
12+
from typing import Dict, Any
13+
1214
import requests
1315

1416

@@ -82,6 +84,57 @@ def get_currency_code(code):
8284
return ALLOW_CURRENCY_CODES[code]
8385

8486

87+
def _request(**kw):
88+
def wrapper(f):
89+
@wraps(f)
90+
def wrapped(*args, **kwargs):
91+
request_params: Dict[str, Any] = dict()
92+
for k, v in kw.items():
93+
if k in kwargs:
94+
continue
95+
kwargs[k] = v
96+
97+
klass = args[0]
98+
method = kwargs['method']
99+
request_params['url'] = kwargs.get('url', klass.service_url)
100+
request_params['method'] = method
101+
request_params.update(kwargs['payload'])
102+
request_params['headers'] = kwargs.get('headers')
103+
request_params['verify'] = kwargs['verify']
104+
request_params['timeout'] = kwargs['timeout']
105+
request_params['cert'] = getattr(klass, 'cert', None)
106+
if method == 'get':
107+
request_params['allow_redirects'] = True
108+
109+
try:
110+
resp = requests.request(**request_params)
111+
if resp.status_code == 200:
112+
try:
113+
result = resp.json()
114+
except (ValueError, json.decoder.JSONDecodeError):
115+
result = parse_response(resp.text)
116+
except Exception:
117+
result = resp.text
118+
else:
119+
try:
120+
result = resp.json()
121+
except (ValueError, json.decoder.JSONDecodeError):
122+
result = {
123+
'RESULT': resp.text,
124+
'STATUS_CODE': resp.status_code,
125+
}
126+
except requests.exceptions.RequestException as e:
127+
result = {
128+
'RESULT': str(e)
129+
}
130+
131+
return f(result=result, *args, **kwargs)
132+
133+
return wrapped
134+
135+
return wrapper
136+
137+
85138
def tbc_params(*arg_params, **kwarg_params):
86139
"""
87140
Decorator that pops all accepted parameters from method's kwargs and puts
@@ -116,48 +169,14 @@ def wrapped(*a, **kw):
116169
payload[param] = gel_to_tetri(kw[param])
117170
else:
118171
payload[param] = kw[param]
119-
return f(payload=payload, *a, **kw)
172+
return f(payload={'data': payload}, *a, **kw)
120173

121174
return wrapped
122175

123176
return wrapper
124177

125178

126-
def tbc_request(**kw):
127-
128-
def wrapper(f):
129-
@wraps(f)
130-
def wrapped(*args, **kwargs):
131-
for k, v in kw.items():
132-
if k in kwargs:
133-
continue
134-
kwargs[k] = v
135-
klass = args[0]
136-
try:
137-
resp = requests.post(
138-
klass.merchant_url, data=kwargs['payload'],
139-
verify=kwargs['verify'], timeout=kwargs['timeout'],
140-
cert=klass.cert
141-
)
142-
if resp.status_code == 200:
143-
result = parse_response(resp.text)
144-
else:
145-
result = {
146-
'RESULT': resp.text,
147-
'STATUS_CODE': resp.status_code,
148-
}
149-
except requests.exceptions.RequestException as e:
150-
result = {
151-
'RESULT': str(e)
152-
}
153-
return f(result=result, *args, **kwargs)
154-
155-
return wrapped
156-
157-
return wrapper
158-
159-
160-
def bog_request(**kw):
179+
def bog_params(**kw):
161180
"""
162181
:param kw:
163182
:return:
@@ -172,7 +191,7 @@ def wrapped(*args, **kwargs):
172191
kwargs[k] = v
173192

174193
klass = args[0]
175-
data, headers, request_params = dict(), dict(), dict()
194+
data, headers, payload = dict(), dict(), dict()
176195
endpoint = kw['endpoint']
177196
api = kw['api']
178197
if api == 'auth':
@@ -184,7 +203,7 @@ def wrapped(*args, **kwargs):
184203
data['grant_type'] = kwargs['grant_type']
185204
else:
186205
data['grant_type'] = 'client_credentials'
187-
request_params.update({'data': data})
206+
payload.update({'data': data})
188207
elif api == 'checkout':
189208
headers['accept'] = 'application/json'
190209
headers['Content-Type'] = 'application/json'
@@ -230,7 +249,7 @@ def wrapped(*args, **kwargs):
230249
'industry_type': 'ECOMMERCE'
231250
}
232251
]
233-
request_params.update({'json': data})
252+
payload.update({'json': data})
234253
elif api == 'refund':
235254
if 'order_id' not in kwargs:
236255
raise ValueError(
@@ -246,7 +265,7 @@ def wrapped(*args, **kwargs):
246265
}
247266
headers['accept'] = 'application/json'
248267
headers['Content-Type'] = 'application/x-www-form-urlencoded'
249-
request_params.update({'data': data})
268+
payload.update({'data': data})
250269
elif api in ('status', 'details', 'payment'):
251270
if 'order_id' not in kwargs:
252271
raise ValueError(
@@ -273,34 +292,12 @@ def wrapped(*args, **kwargs):
273292
)
274293
headers['Authorization'] = f'Bearer {access_token}'
275294

276-
request_params.update({
277-
'method': kw['method'],
295+
kwargs = {
278296
'url': f'{klass.service_url}{endpoint}',
279-
'headers': headers,
280-
'verify': kwargs['verify'],
281-
'timeout': kwargs['timeout']
282-
})
283-
284-
if kw['method'] == 'get':
285-
request_params.setdefault('allow_redirects', True)
297+
'headers': headers
298+
}
286299

287-
try:
288-
resp = requests.request(**request_params)
289-
if resp.status_code == 200:
290-
result = resp.json()
291-
else:
292-
try:
293-
result = resp.json()
294-
except (ValueError, json.decoder.JSONDecodeError):
295-
result = {
296-
'RESULT': resp.text,
297-
'STATUS_CODE': resp.status_code,
298-
}
299-
except requests.exceptions.RequestException as e:
300-
result = {
301-
'RESULT': str(e)
302-
}
303-
return f(result=result, *args, **kwargs)
300+
return f(payload=payload, *args, **kwargs)
304301

305302
return wrapped
306303

0 commit comments

Comments
 (0)