0001-systempayv2-allow-arbitrary-date-for-deferred-paymen.patch
eopayment/systempayv2.py | ||
---|---|---|
266 | 266 | |
267 | 267 |
def request(self, amount, name=None, first_name=None, last_name=None, |
268 | 268 |
address=None, email=None, phone=None, orderid=None, info1=None, |
269 |
info2=None, info3=None, next_url=None, **kwargs): |
|
269 |
info2=None, info3=None, next_url=None, capture_date=None, **kwargs):
|
|
270 | 270 |
''' |
271 | 271 |
Create the URL string to send a request to SystemPay |
272 | 272 |
''' |
273 | 273 |
self.logger.debug('%s amount %s name %s address %s email %s phone %s ' |
274 |
'next_url %s info1 %s info2 %s info3 %s kwargs: %s', |
|
274 |
'next_url %s info1 %s info2 %s info3 %s capture_date %s kwargs: %s',
|
|
275 | 275 |
__name__, amount, name, address, email, phone, info1, |
276 |
info2, info3, next_url, kwargs) |
|
276 |
info2, info3, next_url, capture_date, kwargs)
|
|
277 | 277 |
# amount unit is cents |
278 | 278 |
amount = '%.0f' % (100 * amount) |
279 | 279 |
kwargs.update(add_vads({'amount': force_text(amount)})) |
... | ... | |
332 | 332 |
fields[name] = parameter.default() |
333 | 333 |
else: |
334 | 334 |
fields[name] = parameter.default |
335 | ||
336 |
if capture_date: |
|
337 |
try: |
|
338 |
capture_date = dt.datetime.strptime(capture_date, "%Y-%m-%d").date() |
|
339 |
except (TypeError, ValueError): |
|
340 |
raise ValueError('Bad format for capture date, it should be yyyy-mm-dd.') |
|
341 |
vads_capture_delay = ( |
|
342 |
capture_date - |
|
343 |
dt.datetime.strptime(fields[VADS_TRANS_DATE], "%Y%m%d%H%M%S").date() |
|
344 |
).days |
|
345 |
if vads_capture_delay <= 0: |
|
346 |
raise ValueError("capture_date needs to be superior to the transaction date") |
|
347 |
fields['vads_capture_delay'] = force_text(vads_capture_delay) |
|
348 | ||
335 | 349 |
check_vads(fields) |
336 | 350 |
fields[SIGNATURE] = force_text(self.signature(fields)) |
337 | 351 |
self.logger.debug('%s request contains fields: %s', __name__, fields) |
tests/test_systempayv2.py | ||
---|---|---|
1 | 1 |
# -*- coding: utf-8 -*- |
2 | 2 | |
3 |
from datetime import datetime, timedelta |
|
3 | 4 |
import pytest |
4 | 5 |
from six.moves.urllib import parse as urlparse |
5 | 6 | |
... | ... | |
14 | 15 |
'vads_trans_date': u'20090501193530', |
15 | 16 |
} |
16 | 17 | |
18 | ||
19 |
def get_field(form, field_name): |
|
20 |
for field in form.fields: |
|
21 |
if field['name'] == field_name: |
|
22 |
return field |
|
23 | ||
24 | ||
17 | 25 |
def test_systempayv2(): |
18 | 26 |
p = Payment(PARAMS) |
19 | 27 |
data = {'amount': 15.24, 'orderid': '654321', |
... | ... | |
49 | 57 |
# bad response |
50 | 58 |
with pytest.raises(ResponseError, match='missing signature, vads_ctx_mode or vads_auth_result'): |
51 | 59 |
p.response('foo=bar') |
60 | ||
61 | ||
62 |
def test_systempayv2_deferred_payment(): |
|
63 |
default_params = { |
|
64 |
'secret_test': u'1122334455667788', |
|
65 |
'vads_site_id': u'12345678', |
|
66 |
'vads_ctx_mode': u'TEST', |
|
67 |
} |
|
68 |
default_data = { |
|
69 |
'amount': 15.24, 'orderid': '654321', 'first_name': u'John', |
|
70 |
'last_name': u'Doe' |
|
71 |
} |
|
72 |
p = Payment(default_params.copy()) |
|
73 |
data = default_data.copy() |
|
74 |
data['capture_date'] = (datetime.now().date() + timedelta(days=3)).isoformat() |
|
75 |
transaction_id, f, form = p.request(**data) |
|
76 |
assert get_field(form, 'vads_capture_delay')['value'] == '3' |
|
77 | ||
78 |
# default vads_capture_delay used if no capture date |
|
79 |
params = default_params.copy() |
|
80 |
params['vads_capture_delay'] = 1 |
|
81 |
p = Payment(params) |
|
82 |
transaction_id, f, form = p.request(**default_data.copy()) |
|
83 |
assert get_field(form, 'vads_capture_delay')['value'] == '1' |
|
84 | ||
85 |
# capture date override vads_capture_delay |
|
86 |
params = default_params.copy() |
|
87 |
params['vads_capture_delay'] = 1 |
|
88 |
p = Payment(params) |
|
89 |
data = default_data.copy() |
|
90 |
data['capture_date'] = (datetime.now().date() + timedelta(days=3)).isoformat() |
|
91 |
transaction_id, f, form = p.request(**data) |
|
92 |
assert get_field(form, 'vads_capture_delay')['value'] == '3' |
|
93 | ||
94 |
# capture date can't be inferior to the transaction date |
|
95 |
params = default_params.copy() |
|
96 |
params['vads_trans_date'] = (datetime.utcnow() + timedelta(days=3)).strftime("%Y%m%d%H%M%S") |
|
97 |
p = Payment(params) |
|
98 |
data = default_data.copy() |
|
99 |
data['capture_date'] = (datetime.now().date()).isoformat() |
|
100 | ||
101 |
with pytest.raises( |
|
102 |
ValueError, match='capture_date needs to be superior to the transaction date'): |
|
103 |
transaction_id, f, form = p.request(**data) |
|
104 | ||
105 |
# capture date should have the right format |
|
106 |
p = Payment(params) |
|
107 |
data = default_data.copy() |
|
108 |
data['capture_date'] = 'not a date' |
|
109 |
with pytest.raises( |
|
110 |
ValueError, match='Bad format for capture date, it should be yyyy-mm-dd.'): |
|
111 |
transaction_id, f, form = p.request(**data) |
|
52 |
- |