summaryrefslogtreecommitdiffstats
path: root/eopayment/paybox.py
blob: b9f79beab299220d3438ad48060689948ce85404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# -*- coding: utf-8

import codecs
from collections import OrderedDict
import datetime
import logging
import hashlib
import hmac
import requests
from decimal import Decimal, ROUND_DOWN
from Crypto.Signature import PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA

import six
from six.moves.urllib import parse as urlparse
from six.moves.urllib import parse as urllib

import base64
from gettext import gettext as _
import string
import warnings

from .common import (PaymentCommon, PaymentResponse, FORM, PAID, ERROR, Form,
        ORDERID_TRANSACTION_SEPARATOR, ResponseError, force_text)

__all__ = ['sign', 'Payment']

PAYBOX_KEY = '''-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDe+hkicNP7ROHUssGNtHwiT2Ew
HFrSk/qwrcq8v5metRtTTFPE/nmzSkRnTs3GMpi57rBdxBBJW5W9cpNyGUh0jNXc
VrOSClpD5Ri2hER/GcNrxVRP7RlWOqB1C03q4QYmwjHZ+zlM4OUhCCAtSWflB4wC
Ka1g88CjFwRw/PB9kwIDAQAB
-----END PUBLIC KEY-----'''

VARS = {
    'PBX_SITE': 'Numéro de site (fourni par Paybox)',
    'PBX_RANG': 'Numéro de rang (fourni par Paybox)',
    'PBX_IDENTIFIANT': 'Identifiant interne (fourni par Paybox)',
    'PBX_TOTAL': 'Montant total de la transaction',
    'PBX_DEVISE': 'Devise de la transaction',
    'PBX_CMD':  'Référence commande côté commerçant',
    'PBX_PORTEUR': 'Adresse E - mail de l’acheteur',
    'PBX_RETOUR': 'Liste des variables à retourner par Paybox',
    'PBX_HASH': 'Type d’algorit hme de hachage pour le calcul de l’empreinte',
    'PBX_TIME': 'Horodatage de la transaction',
    'PBX_HMAC': 'Signature calculée avec la clé secrète',
}

PAYBOX_ERROR_CODES = {
    '00000': 'Opération réussie.',
    '00001': 'La connexion au centre d’autorisation a échoué ou une '
    'erreur interne est survenue. Dans ce cas, il est souhaitable de faire '
    'une tentative sur le site secondaire : tpeweb1.paybox.com.',
    '001xx':  'Paiement refusé par le centre d’autorisation [voir '
    '§12.112.1 Codes réponses du centre d’autorisationCodes réponses du '
    'centre d’autorisation]. En cas d’autorisation de la transaction par '
    'le centre d’autorisation de la banque ou de l’établissement financier '
    'privatif, le code erreur “00100” sera en fait remplacé directement '
    'par “00000”.',
    '00003': 'Erreur Paybox. Dans ce cas, il est souhaitable de faire une '
    'tentative sur le site secondaire FQDN tpeweb1.paybox.com.',
    '00004': 'Numéro de porteur ou cryptogramme visuel invalide.',
    '00006': 'Accès refusé ou site/rang/identifiant incorrect.',
    '00008': 'Date de fin de validité incorrecte.',
    '00009': 'Erreur de création d’un abonnement.',
    '00010': 'Devise inconnue.',
    '00011': 'Montant incorrect.',
    '00015': 'Paiement déjà effectué.',
    '00016': 'Abonné déjà existant (inscription nouvel abonné). Valeur '
    '‘U’ de la variable PBX_RETOUR.',
    '00021': 'Carte non autorisée.',
    '00029': 'Carte non conforme. Code erreur renvoyé lors de la '
    'documentation de la variable « PBX_EMPREINTE ».',
    '00030': 'Temps d’attente > 15 mn par l’internaute/acheteur au niveau '
    'de la page de paiements.',
    '00031': 'Réservé',
    '00032': 'Réservé',
    '00033': 'Code pays de l’adresse IP du navigateur de l’acheteur non '
    'autorisé.',
    '00040': 'Opération sans authentification 3-DSecure, bloquée par le '
    'filtre.',
    '99999': 'Opération en attente de validation par l’émetteur du moyen '
    'de paiement.',
}

ALGOS = {
    'SHA512': hashlib.sha512,
    'SHA256': hashlib.sha256,
    'SHA384': hashlib.sha384,
    'SHA224': hashlib.sha224,
}

URLS = {
    'test':
        'https://preprod-tpeweb.paybox.com/cgi/MYchoix_pagepaiement.cgi',
    'prod':
        'https://tpeweb.paybox.com/cgi/MYchoix_pagepaiement.cgi',
    'backup':
        'https://tpeweb1.paybox.com/cgi/MYchoix_pagepaiement.cgi',
}

PAYBOX_DIRECT_URLS = {
    'test': 'https://preprod-ppps.paybox.com/PPPS.php',
    'prod': 'https://ppps.paybox.com/PPPS.php',
    'backup': 'https://ppps1.paybox.com/PPPS.php'
}

PAYBOX_DIRECT_CANCEL_OPERATION = '00005'
PAYBOX_DIRECT_VALIDATE_OPERATION = '00002'

PAYBOX_DIRECT_VERSION_NUMBER = '00104'

PAYBOX_DIRECT_SUCCESS_RESPONSE_CODE = '00000'

# payment modes
PAYMENT_MODES = {'AUTHOR_CAPTURE': 'O',
                 'IMMEDIATE': 'N'}


def sign(data, key):
    '''Take a list of tuple key, value and sign it by building a string to
       sign.
    '''
    logger = logging.getLogger(__name__)
    algo = None
    logger.debug('signature key %r', key)
    logger.debug('signed data %r', data)
    for k, v in data:
        if k == 'PBX_HASH' and v in ALGOS:
            algo = ALGOS[v]
            break
    assert algo, 'Missing or invalid PBX_HASH'
    tosign = ['%s=%s' % (k, force_text(v)) for k, v in data]
    tosign = '&'.join(tosign)
    logger.debug('signed string %r', tosign)
    tosign = tosign.encode('utf-8')
    signature = hmac.new(key, tosign, algo)
    return tuple(data) + (('PBX_HMAC', signature.hexdigest().upper()),)


def verify(data, signature, key=PAYBOX_KEY):
    '''Verify signature using SHA1withRSA by Paybox'''
    key = RSA.importKey(key)
    h = SHA.new(data)
    verifier = PKCS1_v1_5.new(key)
    return verifier.verify(h, signature)


class Payment(PaymentCommon):
    '''Paybox backend for eopayment.

       If you want to handle Instant Payment Notification, you must pass
       provide a automatic_return_url option specifying the URL of the
       callback endpoint.

       Email is mandatory to emit payment requests with paybox.

       IP adresses to authorize:
                                 IN           OUT
           test           195.101.99.73 195.101.99.76
           production     194.2.160.66  194.2.122.158
           backup         195.25.7.146  195.25.7.166
    '''
    callback = None

    description = {
        'caption': _('Paybox'),
        'parameters': [
            {
                'name': 'normal_return_url',
                'caption': _('Normal return URL'),
                'default': '',
                'required': False,
            },
            {
                'name': 'automatic_return_url',
                'caption': _('Automatic return URL'),
                'required': False,
            },
            {
                'name': 'platform',
                'caption': _('Plateforme cible'),
                'default': 'test',
                'validation': lambda x: isinstance(x, basestring) and
                x.lower() in ('test', 'prod'),
            },
            {
                'name': 'site',
                'caption': _('Numéro de site'),
                'required': True,
                'validation': lambda x: isinstance(x, basestring) and
                x.isdigit() and len(x) == 7,
            },
            {
                'name': 'cle',
                'caption': _('Site key'),
                'required': False,
                'validation': lambda x: isinstance(x, basestring),
            },
            {
                'name': 'rang',
                'caption': _('Numéro de rang'),
                'required': True,
                'validation': lambda x: isinstance(x, basestring) and
                x.isdigit() and len(x) == 2,
            },
            {
                'name': 'identifiant',
                'caption': _('Identifiant'),
                'required': True,
                'validation': lambda x: isinstance(x, basestring) and
                x.isdigit() and (0 < len(x) < 10),
            },
            {
                'name': 'shared_secret',
                'caption': _('Secret partagé'),
                'validation': lambda x: isinstance(x, str) and
                all(a.lower() in '0123456789ABCDEF' for a in x),
                'required': True,
            },
            {
                'name': 'devise',
                'caption': _('Devise'),
                'default': '978',
                'choices': (
                    ('978', 'Euro'),
                ),
            },
            {
                'name': 'callback',
                'caption': _('Callback URL'),
                'deprecated': True,
            },
            {
                'name': 'capture_day',
                'caption': _('Nombre de jours pour un paiement différé'),
                'default': '',
                'required': False,
                'validation': lambda x: isinstance(x, basestring) and
                x.isdigit() and (1 <= len(x) <= 2)
            },
            {
                'name': 'capture_mode',
                'caption': _('Capture Mode'),
                'default': 'IMMEDIATE',
                'required': False,
                'choices': PAYMENT_MODES.keys()
            },
        ]
    }

    def request(self, amount, email, name=None, orderid=None, **kwargs):
        d = OrderedDict()
        d['PBX_SITE'] = force_text(self.site)
        d['PBX_RANG'] = force_text(self.rang).strip()[-2:]
        d['PBX_IDENTIFIANT'] = force_text(self.identifiant)
        d['PBX_TOTAL'] = (amount * Decimal(100)).to_integral_value(ROUND_DOWN)
        d['PBX_DEVISE'] = force_text(self.devise)
        transaction_id = kwargs.get('transaction_id') or \
            self.transaction_id(12, string.digits, 'paybox', self.site,
                                self.rang, self.identifiant)
        d['PBX_CMD'] = force_text(transaction_id)
        # prepend order id command reference
        if orderid:
            d['PBX_CMD'] = orderid + ORDERID_TRANSACTION_SEPARATOR + d['PBX_CMD']
        d['PBX_PORTEUR'] = force_text(email)
        d['PBX_RETOUR'] = 'montant:M;reference:R;code_autorisation:A;erreur:E;numero_appel:T;numero_transaction:S;signature:K'
        d['PBX_HASH'] = 'SHA512'
        d['PBX_TIME'] = kwargs.get('time') or (force_text(datetime.datetime.utcnow().isoformat('T')).split('.')[0]+'+00:00')
        d['PBX_ARCHIVAGE'] = transaction_id
        if self.normal_return_url:
            d['PBX_EFFECTUE'] = self.normal_return_url
            d['PBX_REFUSE'] = self.normal_return_url
            d['PBX_ANNULE'] = self.normal_return_url
            d['PBX_ATTENTE'] = self.normal_return_url
        automatic_return_url = self.automatic_return_url
        if not automatic_return_url and self.callback:
            warnings.warn("callback option is deprecated, "
                          "use automatic_return_url", DeprecationWarning)
            automatic_return_url = self.callback
        if self.capture_day:
            d['PBX_DIFF'] = self.capture_day.zfill(2)
        d['PBX_AUTOSEULE'] = PAYMENT_MODES[self.capture_mode]
        if automatic_return_url:
            d['PBX_REPONDRE_A'] = force_text(automatic_return_url)
        d = d.items()

        if six.PY3:
            shared_secret = codecs.decode(bytes(self.shared_secret, 'ascii'), 'hex')
        else:
            shared_secret = codecs.decode(bytes(self.shared_secret), 'hex')
        d = sign(d, shared_secret)
        url = URLS[self.platform]
        fields = []
        for k, v in d:
            fields.append({
                'type': u'hidden',
                'name': force_text(k),
                'value': force_text(v),
            })
        form = Form(url, 'POST', fields, submit_name=None,
                    submit_value=u'Envoyer', encoding='utf-8')
        return transaction_id, FORM, form

    def response(self, query_string, callback=False, **kwargs):
        d = urlparse.parse_qs(query_string, True, False)
        if not set(d) >= set(['erreur', 'reference']):
            raise ResponseError('missing erreur or reference')
        signed = False
        if 'signature' in d:
            sig = d['signature'][0]
            sig = base64.b64decode(sig)
            data = []
            if callback:
                for key in ('montant', 'reference', 'code_autorisation',
                            'erreur', 'numero_appel', 'numero_transaction'):
                    data.append('%s=%s' % (key, urllib.quote(d[key][0])))
            else:
                for key, value in urlparse.parse_qsl(query_string, True, True):
                    if key == 'signature':
                        break
                    data.append('%s=%s' % (key, urllib.quote(value)))
            data = '&'.join(data)
            signed = verify(data, sig)
        if d['erreur'][0] == '00000':
            result = PAID
        else:
            result = ERROR
        for l in (5, 3):
            prefix = d['erreur'][0][:l]
            suffix = 'x' * (5-l)
            bank_status = PAYBOX_ERROR_CODES.get(prefix + suffix)
            if bank_status is not None:
                break
        orderid = d['reference'][0]
        # decode order id from returned reference
        if ORDERID_TRANSACTION_SEPARATOR in orderid:
            orderid, transaction_id = orderid.split(ORDERID_TRANSACTION_SEPARATOR, 1)
        return PaymentResponse(
            order_id=orderid,
            signed=signed,
            bank_data=d,
            result=result,
            bank_status=bank_status)

    def perform(self, amount, bank_data, operation):
        logger = logging.getLogger(__name__)
        url = PAYBOX_DIRECT_URLS[self.platform]
        params = {'VERSION': PAYBOX_DIRECT_VERSION_NUMBER,
                  'TYPE': operation,
                  'SITE': force_text(self.site),
                  'RANG': self.rang.strip(),
                  'CLE': force_text(self.cle),
                  'NUMQUESTION': bank_data['numero_transaction'][0].zfill(10),
                  'MONTANT': (amount * Decimal(100)).to_integral_value(ROUND_DOWN),
                  'DEVISE': force_text(self.devise),
                  'NUMTRANS': bank_data['numero_transaction'][0], # paybox transaction number
                  'NUMAPPEL': bank_data['numero_appel'][0],
                  'REFERENCE': bank_data['reference'][0],
                  'DATEQ': datetime.datetime.now().strftime('%d%m%Y%H%M%S'),
        }
        response = requests.post(url, params)
        response.raise_for_status()
        logger.debug('received %r', response.content)
        data = dict(urlparse.parse_qsl(response.content, True, True))
        data['COMMENTAIRE'] = data.get('COMMENTAIRE', '').decode('iso-8859-1')
        if data.get('CODEREPONSE') != PAYBOX_DIRECT_SUCCESS_RESPONSE_CODE:
            raise ResponseError(data['COMMENTAIRE'].encode('utf-8'))
        return data

    def validate(self, amount, bank_data, **kwargs):
        return self.perform(amount, bank_data, PAYBOX_DIRECT_VALIDATE_OPERATION)

    def cancel(self, amount, bank_data, **kwargs):
        return self.perform(amount, bank_data, PAYBOX_DIRECT_CANCEL_OPERATION)