0001-add-keyware-payment-method-39377.patch
eopayment/__init__.py | ||
---|---|---|
11 | 11 | |
12 | 12 |
__all__ = ['Payment', 'URL', 'HTML', 'FORM', 'SIPS', |
13 | 13 |
'SYSTEMPAY', 'SPPLUS', 'TIPI', 'DUMMY', 'get_backend', 'RECEIVED', 'ACCEPTED', |
14 |
'PAID', 'DENIED', 'CANCELED', 'CANCELLED', 'ERROR', 'WAITING', 'get_backends', 'PAYFIP_WS'] |
|
14 |
'PAID', 'DENIED', 'CANCELED', 'CANCELLED', 'ERROR', 'WAITING', 'get_backends', 'PAYFIP_WS', |
|
15 |
'KEYWARE'] |
|
15 | 16 | |
16 | 17 |
SIPS = 'sips' |
17 | 18 |
SIPS2 = 'sips2' |
... | ... | |
23 | 24 |
PAYBOX = 'paybox' |
24 | 25 |
PAYZEN = 'payzen' |
25 | 26 |
PAYFIP_WS = 'payfip_ws' |
27 |
KEYWARE = 'keyware' |
|
26 | 28 | |
27 | 29 |
logger = logging.getLogger(__name__) |
28 | 30 | |
... | ... | |
31 | 33 |
module = importlib.import_module('.' + kind, package='eopayment') |
32 | 34 |
return module.Payment |
33 | 35 | |
34 |
__BACKENDS = [ DUMMY, SIPS, SIPS2, SYSTEMPAY, SPPLUS, OGONE, PAYBOX, PAYZEN, TIPI, PAYFIP_WS ] |
|
36 |
__BACKENDS = [ DUMMY, SIPS, SIPS2, SYSTEMPAY, SPPLUS, OGONE, PAYBOX, PAYZEN, TIPI, PAYFIP_WS, KEYWARE ]
|
|
35 | 37 | |
36 | 38 |
def get_backends(): |
37 | 39 |
'''Return a dictionnary mapping existing eopayment backends name to their |
eopayment/keyware.py | ||
---|---|---|
1 |
# eopayment - online payment library |
|
2 |
# Copyright (C) 2011-2020 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software: you can redistribute it and/or modify it |
|
5 |
# under the terms of the GNU Affero General Public License as published |
|
6 |
# by the Free Software Foundation, either version 3 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU Affero General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU Affero General Public License |
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
import logging |
|
18 |
import string |
|
19 |
from decimal import ROUND_DOWN, Decimal |
|
20 |
from gettext import gettext as _ |
|
21 | ||
22 |
import requests |
|
23 |
from six.moves.urllib.parse import parse_qs, urljoin |
|
24 | ||
25 |
from .common import (ACCEPTED, CANCELLED, ERROR, PAID, URL, WAITING, PaymentCommon, |
|
26 |
PaymentException, PaymentResponse, ResponseError) |
|
27 | ||
28 |
__all__ = ['Payment'] |
|
29 | ||
30 |
SERVICE_URL = 'https://api.online.emspay.eu/v1/' |
|
31 | ||
32 | ||
33 |
class Payment(PaymentCommon): |
|
34 |
''' |
|
35 |
Produce request for and verify response from the Keyware payment |
|
36 |
gateway. |
|
37 |
''' |
|
38 |
service_url = SERVICE_URL |
|
39 | ||
40 |
description = { |
|
41 |
'caption': 'Keyware payment backend', |
|
42 |
'parameters': [ |
|
43 |
{ |
|
44 |
'name': 'normal_return_url', |
|
45 |
'caption': _('Normal return URL'), |
|
46 |
'required': True, |
|
47 |
}, |
|
48 |
{ |
|
49 |
'name': 'automatic_return_url', |
|
50 |
'caption': _('Asychronous return URL'), |
|
51 |
'required': True, |
|
52 |
}, |
|
53 |
{'name': 'service_url', |
|
54 |
'caption': _('URL of the payment service'), |
|
55 |
'default': service_url, |
|
56 |
'type': str, |
|
57 |
}, |
|
58 |
{ |
|
59 |
'name': 'api_key', |
|
60 |
'caption': _('API key'), |
|
61 |
'required': True, |
|
62 |
}, |
|
63 |
], |
|
64 |
} |
|
65 | ||
66 |
def request(self, amount, email=None, first_name=None, last_name=None, **kwargs): |
|
67 |
amount = self._clean_amount(amount) |
|
68 | ||
69 |
body = { |
|
70 |
'currency': 'EUR', |
|
71 |
'amount': amount, |
|
72 |
'return_url': self.normal_return_url, |
|
73 |
'webhook_url': self.automatic_return_url, |
|
74 |
'customer': { |
|
75 |
'email_address': email, |
|
76 |
'first_name': first_name, |
|
77 |
'last_name': last_name, |
|
78 |
} |
|
79 |
} |
|
80 | ||
81 |
resp = self.call_endpoint('POST', 'orders', data=body) |
|
82 |
return resp['id'], URL, resp['order_url'] |
|
83 | ||
84 |
def response(self, query_string, **kwargs): |
|
85 |
fields = parse_qs(query_string, keep_blank_values=True) |
|
86 |
order_id = fields['order_id'][0] |
|
87 |
resp = self.call_endpoint('GET', 'orders/' + order_id) |
|
88 | ||
89 |
if len(resp['transactions']) > 1: |
|
90 |
raise ResponseError('unexpected multiple transactions for order %s' % order_id) |
|
91 |
transaction = resp['transactions'][0] |
|
92 | ||
93 |
status = transaction['status'] |
|
94 |
if status == 'accepted': |
|
95 |
result = ACCEPTED |
|
96 |
elif status == 'completed': |
|
97 |
result = PAID |
|
98 |
elif status == 'pending': |
|
99 |
result = WAITING |
|
100 |
elif status == 'cancelled': |
|
101 |
result = CANCELLED |
|
102 |
else: |
|
103 |
result = ERROR |
|
104 | ||
105 |
response = PaymentResponse( |
|
106 |
result=result, |
|
107 |
signed=True, |
|
108 |
bank_data=resp, |
|
109 |
order_id=order_id, |
|
110 |
transaction_id=transaction.get('id'), |
|
111 |
bank_status=transaction.get('reason'), |
|
112 |
test=bool('is-test' in resp.get('flags', [])) |
|
113 |
) |
|
114 |
return response |
|
115 | ||
116 |
def validate(self, amount, bank_data, **kwargs): |
|
117 |
return {} |
|
118 | ||
119 |
def cancel(self, amount, bank_data, **kwargs): |
|
120 |
return {} |
|
121 | ||
122 |
def call_endpoint(self, method, endpoint, data=None): |
|
123 |
url = urljoin(self.service_url, endpoint) |
|
124 |
try: |
|
125 |
response = requests.request(method, url, auth=(self.api_key, ''), json=data) |
|
126 |
except requests.exceptions.RequestException as e: |
|
127 |
raise PaymentException('%s error on endpoint "%s": %s' % (method, endpoint, e)) |
|
128 |
try: |
|
129 |
result = response.json() |
|
130 |
except ValueError: |
|
131 |
raise PaymentException('%s on endpoint "%s" returned invalid JSON: %s' % |
|
132 |
(method, endpoint, response.text)) |
|
133 |
try: |
|
134 |
response.raise_for_status() |
|
135 |
except requests.exceptions.HTTPError as e: |
|
136 |
raise PaymentException( |
|
137 |
'%s error on endpoint "%s": %s "%s"' % (method, endpoint, e, result)) |
|
138 |
return result |
|
139 | ||
140 |
@staticmethod |
|
141 |
def _clean_amount(amount): |
|
142 |
try: |
|
143 |
amount = Decimal(amount) |
|
144 |
except ValueError: |
|
145 |
raise ValueError('invalid amount %s: it must be a decimal integer with two digits' |
|
146 |
'at most after the decimal point', amount) |
|
147 |
amount *= Decimal('100') # convert to cents |
|
148 |
amount = amount.to_integral_value(ROUND_DOWN) |
|
149 |
if not (Decimal('1') <= amount <= Decimal('10000000')): |
|
150 |
raise ValueError('amount must not be less than 0.01 or more than 100 000') |
|
151 |
return int(amount) |
tests/test_keyware.py | ||
---|---|---|
1 |
# eopayment - online payment library |
|
2 |
# Copyright (C) 2011-2020 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software: you can redistribute it and/or modify it |
|
5 |
# under the terms of the GNU Affero General Public License as published |
|
6 |
# by the Free Software Foundation, either version 3 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU Affero General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU Affero General Public License |
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
import json |
|
18 | ||
19 |
import pytest |
|
20 |
import requests |
|
21 |
import six |
|
22 |
from httmock import response, urlmatch, HTTMock, with_httmock, all_requests, remember_called |
|
23 | ||
24 |
import eopayment |
|
25 |
from eopayment.keyware import Payment |
|
26 | ||
27 |
pytestmark = pytest.mark.skipif(six.PY2, reason='this payment module only supports python3') |
|
28 | ||
29 |
WEBHOOK_URL = 'https://callback.example.com' |
|
30 |
RETURN_URL = 'https://return.example.com' |
|
31 |
API_KEY = 'test' |
|
32 | ||
33 |
ORDER_ID = "1c969951-f5f1-4290-ae41-6177961fb3cb" |
|
34 |
QUERY_STRING = 'order_id=' + ORDER_ID |
|
35 | ||
36 | ||
37 |
POST_ORDER_RESPONSE = { |
|
38 |
"amount": 995, |
|
39 |
"client": { |
|
40 |
"user_agent": "Testing API" |
|
41 |
}, |
|
42 |
"created": "2016-07-04T11:41:57.121017+00:00", |
|
43 |
"currency": "EUR", |
|
44 |
"description": "Example description", |
|
45 |
"id": ORDER_ID, |
|
46 |
"merchant_id": "7131b462-1b7d-489f-aba9-de2f0eadc9dc", |
|
47 |
"modified": "2016-07-04T11:41:57.183822+00:00", |
|
48 |
"order_url": "https://api.online.emspay.eu/pay/1c969951-f5f1-4290-ae41-6177961fb3cb/", |
|
49 |
"project_id": "1ef558ed-d77d-470d-b43b-c0f4a131bcef", |
|
50 |
"status": "new" |
|
51 |
} |
|
52 | ||
53 |
GET_ORDER_RESPONSE = { |
|
54 |
"amount": 995, |
|
55 |
"client": { |
|
56 |
"user_agent": "Testing API" |
|
57 |
}, |
|
58 |
"created": "2016-07-04T11:41:55.635115+00:00", |
|
59 |
"currency": "EUR", |
|
60 |
"description": "Example order #1", |
|
61 |
"id": ORDER_ID, |
|
62 |
"last_transaction_added": "2016-07-04T11:41:55.831655+00:00", |
|
63 |
"merchant_id": "7131b462-1b7d-489f-aba9-de2f0eadc9dc", |
|
64 |
"merchant_order_id": "EXAMPLE001", |
|
65 |
"modified": "2016-07-04T11:41:56.215543+00:00", |
|
66 |
"project_id": "1ef558ed-d77d-470d-b43b-c0f4a131bcef", |
|
67 |
"return_url": "http://www.example.com/", |
|
68 |
"status": "new", |
|
69 |
"transactions": [ |
|
70 |
{ |
|
71 |
"amount": 995, |
|
72 |
"balance": "internal", |
|
73 |
"created": "2016-07-04T11:41:55.831655+00:00", |
|
74 |
"credit_debit": "credit", |
|
75 |
"currency": "EUR", |
|
76 |
"description": "Example order #1", |
|
77 |
"events": [ |
|
78 |
{ |
|
79 |
"event": "new", |
|
80 |
"id": "0c4bd0cd-f197-446b-b218-39cbeb028290", |
|
81 |
"noticed": "2016-07-04T11:41:55.987468+00:00", |
|
82 |
"occurred": "2016-07-04T11:41:55.831655+00:00", |
|
83 |
"source": "set_status" |
|
84 |
} |
|
85 |
], |
|
86 |
"expiration_period": "PT60M", |
|
87 |
"id": "6c81499c-14e4-4974-99e5-fe72ce019411", |
|
88 |
"merchant_id": "7131b462-1b7d-489f-aba9-de2f0eadc9dc", |
|
89 |
"modified": "2016-07-04T11:41:56.065147+00:00", |
|
90 |
"order_id": ORDER_ID, |
|
91 |
"payment_method": "ideal", |
|
92 |
"payment_method_details": { |
|
93 |
"issuer_id": "INGBNL2A", |
|
94 |
}, |
|
95 |
"payment_url": "https://api.online.emspay.eu/redirect/6c81499c-14e4-4974-99e5-fe72ce019411/to/payment/", |
|
96 |
"project_id": "1ef558ed-d77d-470d-b43b-c0f4a131bcef", |
|
97 |
"status": "completed" |
|
98 |
} |
|
99 |
] |
|
100 |
} |
|
101 | ||
102 | ||
103 |
@remember_called |
|
104 |
@urlmatch(scheme='https', netloc='api.online.emspay.eu', path='/v1/orders', method='POST') |
|
105 |
def add_order(url, request): |
|
106 |
return response(200, POST_ORDER_RESPONSE, request=request) |
|
107 | ||
108 |
@remember_called |
|
109 |
@urlmatch(scheme='https', netloc='api.online.emspay.eu', path='/v1/orders', method='GET') |
|
110 |
def successful_order(url, request): |
|
111 |
return response(200, GET_ORDER_RESPONSE, request=request) |
|
112 | ||
113 |
@remember_called |
|
114 |
@urlmatch(scheme='https', netloc='api.online.emspay.eu', path='/v1/orders', method='GET') |
|
115 |
def error_order(url, request): |
|
116 |
resp = GET_ORDER_RESPONSE.copy() |
|
117 |
resp['transactions'][0]['status'] = 'error' |
|
118 |
return response(200, resp, request=request) |
|
119 | ||
120 |
@remember_called |
|
121 |
@urlmatch(scheme='https', netloc='api.online.emspay.eu', path='/v1/orders', method='GET') |
|
122 |
def connection_error(url, request): |
|
123 |
raise requests.ConnectionError('test msg') |
|
124 | ||
125 |
@remember_called |
|
126 |
@urlmatch(scheme='https', netloc='api.online.emspay.eu', path='/v1/orders', method='GET') |
|
127 |
def http_error(url, request): |
|
128 |
error_payload = {'error': {'status': 400, 'type': 'Bad request', 'value': 'error'}} |
|
129 |
return response(400, error_payload, request=request) |
|
130 | ||
131 |
@remember_called |
|
132 |
@urlmatch(scheme='https', netloc='api.online.emspay.eu', path='/v1/orders', method='GET') |
|
133 |
def invalid_json(url, request): |
|
134 |
return response(200, '{', request=request) |
|
135 | ||
136 |
@pytest.fixture |
|
137 |
def keyware(): |
|
138 |
return Payment({ |
|
139 |
'normal_return_url': RETURN_URL, |
|
140 |
'automatic_return_url': WEBHOOK_URL, |
|
141 |
'api_key': API_KEY |
|
142 |
}) |
|
143 | ||
144 | ||
145 |
@with_httmock(add_order) |
|
146 |
def test_keyware_request(keyware): |
|
147 |
email = 'test@test.com' |
|
148 |
order_id, kind, url = keyware.request(2.5, email=email) |
|
149 | ||
150 |
assert order_id == ORDER_ID |
|
151 |
assert kind == eopayment.URL |
|
152 |
assert 'api.online.emspay.eu/pay/' in url |
|
153 | ||
154 |
body = json.loads(add_order.call['requests'][0].body) |
|
155 |
assert body['currency'] == 'EUR' |
|
156 |
assert body['customer']['email_address'] == email |
|
157 |
assert isinstance(body['amount'], int) |
|
158 |
assert body['amount'] == 250 |
|
159 |
assert body['webhook_url'] == WEBHOOK_URL |
|
160 |
assert body['return_url'] == RETURN_URL |
|
161 | ||
162 | ||
163 |
@with_httmock(successful_order) |
|
164 |
def test_keyware_response(keyware): |
|
165 |
payment_response = keyware.response(QUERY_STRING) |
|
166 | ||
167 |
assert payment_response.result == eopayment.PAID |
|
168 |
assert payment_response.signed is True |
|
169 |
assert payment_response.bank_data == GET_ORDER_RESPONSE |
|
170 |
assert payment_response.order_id == ORDER_ID |
|
171 |
assert payment_response.transaction_id == "6c81499c-14e4-4974-99e5-fe72ce019411" |
|
172 |
assert not payment_response.bank_status |
|
173 |
assert payment_response.test is False |
|
174 | ||
175 |
request = successful_order.call['requests'][0] |
|
176 |
assert ORDER_ID in request.url |
|
177 | ||
178 | ||
179 |
@with_httmock(error_order) |
|
180 |
def test_keyware_response_error(keyware): |
|
181 |
payment_response = keyware.response(QUERY_STRING) |
|
182 |
assert payment_response.result == eopayment.ERROR |
|
183 | ||
184 | ||
185 |
@with_httmock(connection_error) |
|
186 |
def test_keyware_endpoint_connection_error(keyware): |
|
187 |
with pytest.raises(eopayment.common.PaymentException) as excinfo: |
|
188 |
keyware.call_endpoint('GET', 'orders') |
|
189 |
assert 'test msg' in str(excinfo.value) |
|
190 | ||
191 | ||
192 |
@with_httmock(http_error) |
|
193 |
def test_keyware_endpoint_http_error(keyware): |
|
194 |
with pytest.raises(eopayment.common.PaymentException) as excinfo: |
|
195 |
keyware.call_endpoint('GET', 'orders') |
|
196 |
assert 'Bad request' in str(excinfo.value) |
|
197 | ||
198 | ||
199 |
@with_httmock(invalid_json) |
|
200 |
def test_keyware_endpoint_json_error(keyware): |
|
201 |
with pytest.raises(eopayment.common.PaymentException) as excinfo: |
|
202 |
keyware.call_endpoint('GET', 'orders') |
|
203 |
assert 'JSON' in str(excinfo.value) |
|
0 |
- |