2020-04-10 11:10:15 +02:00
|
|
|
# eopayment - online payment library
|
|
|
|
# Copyright (C) 2011-2020 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-10-20 16:57:28 +02:00
|
|
|
|
2020-04-10 11:10:15 +02:00
|
|
|
from xml.etree import ElementTree as ET
|
|
|
|
|
2021-02-20 19:44:15 +01:00
|
|
|
import pytest
|
2018-03-26 09:56:16 +02:00
|
|
|
from six.moves.urllib import parse as urllib
|
2016-02-08 18:40:45 +01:00
|
|
|
|
|
|
|
import eopayment
|
|
|
|
import eopayment.ogone as ogone
|
2016-03-09 22:31:40 +01:00
|
|
|
from eopayment import ResponseError
|
2016-02-08 18:40:45 +01:00
|
|
|
|
2021-11-26 11:38:58 +01:00
|
|
|
PSPID = '2352566ö'
|
2016-02-08 18:40:45 +01:00
|
|
|
|
2021-02-20 19:44:15 +01:00
|
|
|
|
2021-02-20 19:44:45 +01:00
|
|
|
@pytest.fixture(params=[None, 'iso-8859-1', 'utf-8'])
|
2021-02-20 19:44:15 +01:00
|
|
|
def params(request):
|
|
|
|
params = {
|
|
|
|
'environment': ogone.ENVIRONMENT_TEST,
|
|
|
|
'pspid': PSPID,
|
2021-11-26 11:38:58 +01:00
|
|
|
'sha_in': 'sécret',
|
|
|
|
'sha_out': 'sécret',
|
|
|
|
'automatic_return_url': 'http://example.com/autömatic_réturn_url'
|
2021-02-20 19:44:15 +01:00
|
|
|
}
|
2021-02-20 19:44:45 +01:00
|
|
|
encoding = request.param
|
|
|
|
if encoding:
|
|
|
|
params['encoding'] = encoding
|
2021-02-20 19:44:15 +01:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def test_request(params):
|
|
|
|
ogone_backend = eopayment.Payment('ogone', params)
|
|
|
|
amount = '42.42'
|
2021-11-26 11:38:58 +01:00
|
|
|
order_id = 'my ordér'
|
2021-02-20 19:44:15 +01:00
|
|
|
reference, kind, what = ogone_backend.request(
|
|
|
|
amount=amount,
|
|
|
|
orderid=order_id,
|
|
|
|
email='foo@example.com')
|
2020-10-10 13:33:08 +02:00
|
|
|
assert len(reference) == 32
|
2021-02-20 19:44:15 +01:00
|
|
|
root = ET.fromstring(str(what))
|
|
|
|
assert root.tag == 'form'
|
|
|
|
assert root.attrib['method'] == 'POST'
|
|
|
|
assert root.attrib['action'] == ogone.ENVIRONMENT_TEST_URL
|
|
|
|
values = {
|
2021-11-26 11:38:58 +01:00
|
|
|
'CURRENCY': 'EUR',
|
2020-10-10 13:33:08 +02:00
|
|
|
'ORDERID': order_id,
|
2021-02-20 19:44:15 +01:00
|
|
|
'PSPID': PSPID,
|
|
|
|
'EMAIL': 'foo@example.com',
|
|
|
|
'AMOUNT': amount.replace('.', ''),
|
|
|
|
'LANGUAGE': 'fr_FR',
|
2020-10-10 13:33:08 +02:00
|
|
|
'COMPLUS': reference,
|
2021-02-20 19:44:15 +01:00
|
|
|
}
|
|
|
|
values.update({'SHASIGN': ogone_backend.backend.sha_sign_in(values)})
|
|
|
|
for node in root:
|
|
|
|
assert node.attrib['type'] in ('hidden', 'submit')
|
2021-11-26 11:38:58 +01:00
|
|
|
assert set(node.attrib.keys()), {'type', 'name' == 'value'}
|
2021-02-20 19:44:15 +01:00
|
|
|
name = node.attrib['name']
|
|
|
|
if node.attrib['type'] == 'hidden':
|
|
|
|
assert name in values
|
|
|
|
assert node.attrib['value'] == values[name]
|
|
|
|
|
|
|
|
|
|
|
|
def test_response(params):
|
|
|
|
ogone_backend = eopayment.Payment('ogone', params)
|
|
|
|
order_id = 'myorder'
|
2021-11-26 11:38:58 +01:00
|
|
|
data = {'orderid': 'myorder', 'status': '9', 'payid': '3011229363',
|
|
|
|
'cn': 'Usér', 'ncerror': '0',
|
|
|
|
'trxdate': '10/24/16', 'acceptance': 'test123',
|
|
|
|
'currency': 'eur', 'amount': '7.5'}
|
2021-02-20 19:44:45 +01:00
|
|
|
data['shasign'] = ogone_backend.backend.sha_sign_out(
|
|
|
|
data, encoding=params.get('encoding', 'iso-8859-1'))
|
2021-02-20 19:44:15 +01:00
|
|
|
# uniformize to utf-8 first
|
|
|
|
for k in data:
|
2021-02-20 19:44:45 +01:00
|
|
|
data[k] = eopayment.common.force_byte(data[k], encoding=params.get('encoding', 'iso-8859-1'))
|
2021-02-20 19:44:15 +01:00
|
|
|
response = ogone_backend.response(urllib.urlencode(data))
|
|
|
|
assert response.signed
|
|
|
|
assert response.order_id == order_id
|
|
|
|
|
|
|
|
|
|
|
|
def test_iso_8859_1_response():
|
|
|
|
params = {
|
|
|
|
'environment': ogone.ENVIRONMENT_TEST,
|
|
|
|
'pspid': PSPID,
|
2021-11-26 11:38:58 +01:00
|
|
|
'sha_in': 'sécret',
|
|
|
|
'sha_out': 'sécret',
|
|
|
|
'automatic_return_url': 'http://example.com/autömatic_réturn_url'
|
2021-02-20 19:44:15 +01:00
|
|
|
}
|
|
|
|
ogone_backend = eopayment.Payment('ogone', params)
|
|
|
|
order_id = 'lRXK4Rl1N2yIR3R5z7Kc'
|
|
|
|
backend_response = (
|
|
|
|
'orderID=lRXK4Rl1N2yIR3R5z7Kc¤cy=EUR&amount=7%2E5'
|
|
|
|
'&PM=CreditCard&ACCEPTANCE=test123&STATUS=9'
|
|
|
|
'&CARDNO=XXXXXXXXXXXX9999&ED=0118'
|
|
|
|
'&CN=Miha%EF+Serghe%EF&TRXDATE=10%2F24%2F16'
|
|
|
|
'&PAYID=3011228911&NCERROR=0&BRAND=MasterCard'
|
|
|
|
'&IP=80%2E12%2E92%2E47&SHASIGN=C429BE892FACFBFCE5E2CC809B102D866DD3D48C'
|
|
|
|
)
|
|
|
|
response = ogone_backend.response(backend_response)
|
|
|
|
assert response.signed
|
|
|
|
assert response.order_id == order_id
|
|
|
|
|
|
|
|
|
|
|
|
def test_bad_response(params):
|
|
|
|
ogone_backend = eopayment.Payment('ogone', params)
|
|
|
|
data = {'payid': '32100123', 'status': 9, 'ncerror': 0}
|
|
|
|
with pytest.raises(ResponseError, match='missing ORDERID, PAYID, STATUS or NCERROR'):
|
|
|
|
ogone_backend.response(urllib.urlencode(data))
|
|
|
|
|
|
|
|
|
|
|
|
def test_bank_transfer_response(params):
|
|
|
|
ogone_backend = eopayment.Payment('ogone', params)
|
|
|
|
data = {
|
2021-11-26 11:38:58 +01:00
|
|
|
'orderid': 'myorder',
|
|
|
|
'status': '41',
|
|
|
|
'payid': '3011229363',
|
|
|
|
'cn': 'User',
|
|
|
|
'ncerror': '0',
|
|
|
|
'trxdate': '10/24/16',
|
2021-02-20 19:44:15 +01:00
|
|
|
'brand': 'Bank transfer',
|
|
|
|
'pm': 'bank transfer',
|
2021-11-26 11:38:58 +01:00
|
|
|
'currency': 'eur',
|
|
|
|
'amount': '7.5',
|
|
|
|
'shasign': '944CBD1E010BA4945415AE4B16CC40FD533F6CE2',
|
2021-02-20 19:44:15 +01:00
|
|
|
}
|
|
|
|
# uniformize to expected encoding
|
|
|
|
for k in data:
|
2021-02-20 19:44:45 +01:00
|
|
|
data[k] = eopayment.common.force_byte(data[k], encoding=params.get('encoding', 'iso-8859-1'))
|
2021-02-20 19:44:15 +01:00
|
|
|
response = ogone_backend.response(urllib.urlencode(data))
|
|
|
|
assert response.signed
|
|
|
|
assert response.result == eopayment.WAITING
|
|
|
|
|
|
|
|
# check utf-8 based signature is also ok
|
|
|
|
data['shasign'] = b'0E35F687ACBEAA6CA769E0ADDBD0863EB6C1678A'
|
|
|
|
response = ogone_backend.response(urllib.urlencode(data))
|
|
|
|
assert response.signed
|
|
|
|
assert response.result == eopayment.WAITING
|
|
|
|
|
|
|
|
# check invalid signature is not marked ok
|
|
|
|
data['shasign'] = b'0000000000000000000000000000000000000000'
|
|
|
|
response = ogone_backend.response(urllib.urlencode(data))
|
|
|
|
assert not response.signed
|