2011-04-30 19:05:36 +02:00
|
|
|
import urllib
|
|
|
|
import string
|
2012-02-20 17:14:30 +01:00
|
|
|
import logging
|
|
|
|
|
2011-04-30 19:05:36 +02:00
|
|
|
try:
|
|
|
|
from cgi import parse_qs
|
|
|
|
except:
|
|
|
|
from urlparse import parse_qs
|
|
|
|
|
2012-02-17 17:49:51 +01:00
|
|
|
from common import PaymentCommon, URL, PaymentResponse
|
2011-04-30 19:05:36 +02:00
|
|
|
|
|
|
|
__all__ = [ 'Payment' ]
|
|
|
|
|
|
|
|
SERVICE_URL = 'http://dummy-payment.demo.entrouvert.com/'
|
|
|
|
ALPHANUM = string.letters + string.digits
|
2012-02-20 17:14:30 +01:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
2011-04-30 19:05:36 +02:00
|
|
|
|
|
|
|
class Payment(PaymentCommon):
|
2011-05-05 15:50:42 +02:00
|
|
|
'''
|
|
|
|
Dummy implementation of the payment interface.
|
|
|
|
|
|
|
|
It is used with a dummy implementation of a bank payment service that
|
|
|
|
you can find on:
|
|
|
|
|
|
|
|
http://dummy-payment.demo.entrouvert.com/
|
2011-05-27 15:55:26 +02:00
|
|
|
|
|
|
|
You must pass the following keys inside the options dictionnary:
|
|
|
|
- dummy_service_url, the URL of the dummy payment service, it defaults
|
|
|
|
to the one operated by Entr'ouvert.
|
|
|
|
- direct_notification_url: where to POST to notify the service of a
|
|
|
|
payment
|
|
|
|
- origin: a human string to display to the user about the origin of
|
|
|
|
the request.
|
|
|
|
- siret: an identifier for the eCommerce site, fake.
|
|
|
|
- next_url: the return URL for the user (can be overriden on a per
|
|
|
|
request basis).
|
2011-05-05 15:50:42 +02:00
|
|
|
'''
|
2011-04-30 19:05:36 +02:00
|
|
|
description = {
|
|
|
|
'caption': 'Dummy payment backend',
|
|
|
|
'parameters': {
|
2011-05-27 15:55:26 +02:00
|
|
|
'dummy_service_url': {
|
|
|
|
'caption': 'URL of the dummy payment service',
|
|
|
|
'default': SERVICE_URL,
|
2012-02-17 18:46:55 +01:00
|
|
|
'type': str,
|
2011-05-27 15:55:26 +02:00
|
|
|
},
|
2011-04-30 19:05:36 +02:00
|
|
|
'direct_notification_url': {
|
|
|
|
'caption': 'direct notification url',
|
2012-02-17 18:46:55 +01:00
|
|
|
'type': str,
|
2011-04-30 19:05:36 +02:00
|
|
|
},
|
2011-05-05 15:50:42 +02:00
|
|
|
'origin': {
|
|
|
|
'caption': 'name of the requesting service, '
|
2012-02-17 17:49:51 +01:00
|
|
|
'to present in the user interface',
|
2012-02-17 18:46:55 +01:00
|
|
|
'type': str,
|
2012-02-17 17:49:51 +01:00
|
|
|
|
2011-05-05 15:50:42 +02:00
|
|
|
},
|
2011-04-30 19:05:36 +02:00
|
|
|
'siret': {
|
|
|
|
'caption': 'dummy siret parameter',
|
2012-02-17 18:46:55 +01:00
|
|
|
'type': str,
|
2011-04-30 19:05:36 +02:00
|
|
|
},
|
2011-05-05 16:43:09 +02:00
|
|
|
'next_url': {
|
|
|
|
'caption': 'Return URL for the user',
|
2012-02-17 18:46:55 +01:00
|
|
|
'type': str,
|
2012-02-17 17:49:51 +01:00
|
|
|
},
|
|
|
|
'consider_all_response_signed': {
|
2012-02-17 18:42:41 +01:00
|
|
|
'caption': 'All response will be considered as signed '
|
|
|
|
'(to test payment locally for example, as you '
|
|
|
|
'cannot received the signed callback)',
|
2012-02-17 17:49:51 +01:00
|
|
|
'type': bool,
|
|
|
|
'default': False,
|
|
|
|
},
|
2011-04-30 19:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 17:14:30 +01:00
|
|
|
def request(self, montant, email=None, next_url=None, logger=LOGGER):
|
2011-04-30 19:05:36 +02:00
|
|
|
transaction_id = self.transaction_id(30, ALPHANUM, 'dummy', self.siret)
|
2011-05-05 16:43:09 +02:00
|
|
|
if self.next_url:
|
|
|
|
next_url = self.next_url
|
2011-04-30 19:05:36 +02:00
|
|
|
query = {
|
|
|
|
'transaction_id': transaction_id,
|
2011-05-05 15:50:42 +02:00
|
|
|
'siret': self.siret,
|
2011-04-30 19:05:36 +02:00
|
|
|
'amount': montant,
|
|
|
|
'email': email,
|
|
|
|
'return_url': next_url or '',
|
2011-05-05 15:50:42 +02:00
|
|
|
'direct_notification_url': self.direct_notification_url,
|
|
|
|
'origin': self.origin
|
2011-04-30 19:05:36 +02:00
|
|
|
}
|
|
|
|
url = '%s?%s' % (SERVICE_URL, urllib.urlencode(query))
|
|
|
|
return transaction_id, URL, url
|
|
|
|
|
2012-02-20 17:14:30 +01:00
|
|
|
def response(self, query_string, logger=LOGGER):
|
2011-04-30 19:05:36 +02:00
|
|
|
form = parse_qs(query_string)
|
|
|
|
transaction_id = form.get('transaction_id',[''])[0]
|
2012-01-30 17:47:47 +01:00
|
|
|
form[self.BANK_ID] = transaction_id
|
2011-04-30 19:05:36 +02:00
|
|
|
|
2012-02-17 18:11:58 +01:00
|
|
|
signed = 'signed' in form
|
|
|
|
if signed:
|
2011-04-30 19:05:36 +02:00
|
|
|
content = 'signature ok'
|
|
|
|
else:
|
|
|
|
content = None
|
2012-02-20 09:54:06 +01:00
|
|
|
signed = signed or self.consider_all_response_signed
|
2012-02-17 18:11:58 +01:00
|
|
|
result = 'ok' in form
|
|
|
|
signed_result = result if signed else None
|
2012-02-17 17:49:51 +01:00
|
|
|
|
2012-02-17 18:11:58 +01:00
|
|
|
response = PaymentResponse(result=result,
|
|
|
|
signed_result=signed_result,
|
2012-02-17 17:49:51 +01:00
|
|
|
bank_data=form,
|
|
|
|
return_content=content,
|
|
|
|
order_id=transaction_id,
|
|
|
|
transaction_id=transaction_id,
|
|
|
|
bank_status=form.get('reason'))
|
|
|
|
return response
|
2011-04-30 19:05:36 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
options = {
|
|
|
|
'direct_notification_url': 'http://example.com/direct_notification_url',
|
2011-05-05 15:50:42 +02:00
|
|
|
'siret': '1234',
|
|
|
|
'origin': 'Mairie de Perpette-les-oies'
|
2011-04-30 19:05:36 +02:00
|
|
|
}
|
|
|
|
p = Payment(options)
|
|
|
|
retour = 'http://example.com/retour?amount=10.0&direct_notification_url=http%3A%2F%2Fexample.com%2Fdirect_notification_url&email=toto%40example.com&transaction_id=6Tfw2e1bPyYnz7CedZqvdHt7T9XX6T&return_url=http%3A%2F%2Fexample.com%2Fretour&nok=1'
|
|
|
|
r = p.response(retour.split('?',1)[1])
|
|
|
|
assert not r[0]
|
|
|
|
assert r[1] == '6Tfw2e1bPyYnz7CedZqvdHt7T9XX6T'
|
|
|
|
assert r[3] is None
|
|
|
|
retour = 'http://example.com/retour?amount=10.0&direct_notification_url=http%3A%2F%2Fexample.com%2Fdirect_notification_url&email=toto%40example.com&transaction_id=6Tfw2e1bPyYnz7CedZqvdHt7T9XX6T&return_url=http%3A%2F%2Fexample.com%2Fretour&ok=1&signed=1'
|
|
|
|
r = p.response(retour.split('?',1)[1])
|
|
|
|
assert r[0]
|
|
|
|
assert r[1] == '6Tfw2e1bPyYnz7CedZqvdHt7T9XX6T'
|
|
|
|
assert r[3] == 'signature ok'
|
|
|
|
|
|
|
|
|