Skip to content

Commit 3d934c6

Browse files
committed
refacto Response + tests
1 parent 4fd13a8 commit 3d934c6

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ else:
9696

9797
According to the Redsys documentation:
9898

99-
- `response.is_paid()`: Returns `True` if the response code is
99+
- `response.is_paid`: Returns `True` if the response code is
100100
between 0 and 99 (both included).
101-
- `response.is_canceled()`: Returns `True` if the response code
101+
- `response.is_canceled`: Returns `True` if the response code
102102
is 400.
103-
- `response.is_refunded()`: Returns `True` if the response code
103+
- `response.is_refunded`: Returns `True` if the response code
104104
is 900.
105-
- `response.is_authorized()`: Returns `True` if the response is
105+
- `response.is_authorized`: Returns `True` if the response is
106106
**paid**, **refunded** or **canceled**.
107107

108108
Also, you can directly access the code or the message defined in Redsys

redsys/response.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from decimal import Decimal
32
from typing import Any, Dict
43

@@ -111,17 +110,24 @@ def __getattr__(self, item: str) -> Any:
111110

112111
def __setattr__(self, key: str, value: Any):
113112
if key in MERCHANT_PARAMETERS_MAP:
114-
self._parameters[key] = value
113+
clean = getattr(self, "clean_%s" % MERCHANT_PARAMETERS_MAP[key], None)
114+
self._parameters[MERCHANT_PARAMETERS_MAP[key]] = (
115+
clean(value) if clean else value
116+
)
115117

118+
@property
116119
def is_authorized(self):
117120
return (0 <= self.code <= 99) or self.code == 900 or self.code == 400
118121

122+
@property
119123
def is_paid(self):
120124
return 0 <= self.code <= 99
121125

126+
@property
122127
def is_refunded(self):
123128
return self.code == 900
124129

130+
@property
125131
def is_canceled(self):
126132
return self.code == 400
127133

@@ -131,7 +137,7 @@ def code(self):
131137

132138
@property
133139
def message(self):
134-
return RESPONSE_MAP["0000"] if self.is_paid() else RESPONSE_MAP[self.response]
140+
return RESPONSE_MAP["0000"] if self.is_paid else RESPONSE_MAP[self.response]
135141

136142
@staticmethod
137143
def clean_amount(value):

redsys/tests/test_response.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from redsys.response import Response
2+
3+
4+
class TestResponse:
5+
def test_create_response(self):
6+
parameters = {
7+
"Ds_Response": "90",
8+
"Ds_MerchantCode": "1056",
9+
"Ds_Terminal": "1",
10+
"Ds_TransactionType": "1",
11+
"Ds_Order": "000000001",
12+
"Ds_Amount": "10054",
13+
"Ds_MerchantData": "test merchant data",
14+
"Ds_Currency": 978,
15+
}
16+
response = Response(parameters)
17+
assert response.code == 90
18+
assert (
19+
response.message == "Transacción autorizada para pagos y preautorizaciones"
20+
)
21+
assert len(response._parameters.keys()) == 8
22+
assert response.is_authorized is True
23+
assert response.is_paid is True
24+
assert response.is_canceled is False
25+
assert response.is_refunded is False

0 commit comments

Comments
 (0)