Projet

Général

Profil

0001-add-support-for-sips2-cancel-validate-cash-managemen.patch

Frédéric Péters, 27 juillet 2016 13:15

Télécharger (5,92 ko)

Voir les différences:

Subject: [PATCH] add support for sips2 cancel/validate cash management
 operations (#12761)

 eopayment/__init__.py | 20 +++++++++++++++
 eopayment/sips2.py    | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 setup.py              |  3 ++-
 3 files changed, 89 insertions(+), 1 deletion(-)
eopayment/__init__.py
134 134

  
135 135
        '''
136 136
        return self.backend.response(query_string, **kwargs)
137

  
138
    def cancel(self, amount, bank_data, **kwargs):
139
        '''
140
           Cancel or edit the amount of a transaction sent to the bank.
141

  
142
           Arguments:
143
           - amount -- the amount of money to cancel
144
           - bank_data -- the transaction dictionary received from the bank
145
        '''
146
        return self.backend.cancel(amount, bank_data, **kwargs)
147

  
148
    def validate(self, amount, bank_data, **kwargs):
149
        '''
150
           Validate and trigger the transmission of a transaction to the bank.
151

  
152
           Arguments:
153
           - amount -- the amount of money
154
           - bank_data -- the transaction dictionary received from the bank
155
        '''
156
        return self.backend.validate(amount, bank_data, **kwargs)
eopayment/sips2.py
1 1
# -*- coding: utf-8 -*-
2 2
import collections
3
import json
3 4
import urlparse
4 5
import string
5 6
from decimal import Decimal
6 7
import uuid
7 8
import hashlib
9
import hmac
8 10
from gettext import gettext as _
11
import requests
9 12
import warnings
10 13

  
11 14
from common import (PaymentCommon, FORM, Form, PaymentResponse, PAID, ERROR,
......
33 36
        'test': 'https://payment-webinit.simu.sips-atos.com/paymentInit',
34 37
        'prod': 'https://payment-webinit.sips-atos.com/paymentInit',
35 38
    }
39
    WS_URL = {
40
        'test': 'https://office-server.test.sips-atos.com',
41
        'prod': 'https://office-server.sips-atos.com',
42
    }
43

  
36 44
    INTERFACE_VERSION = 'HP_2.3'
37 45
    RESPONSE_CODES = {
38 46
        '00': 'Authorisation accepted',
......
228 236
            transaction_id=data.get('authorisationId'),
229 237
            bank_status=self.RESPONSE_CODES.get(response_code, u'unknown code - ' + response_code),
230 238
            test=test)
239

  
240
    def get_seal_for_json_ws_data(self, data):
241
        data_to_send = []
242
        for key in sorted(data.keys()):
243
            if key in ('keyVersion', 'sealAlgorithm', 'seal'):
244
                continue
245
            data_to_send.append(unicode(data[key]))
246
        data_to_send_str = u''.join(data_to_send).encode('utf-8')
247
        return hmac.new(unicode(self.secret_key).encode('utf-8'), data_to_send_str, hashlib.sha256).hexdigest()
248

  
249
    def perform_cash_management_operation(self, endpoint, data):
250
        data['merchantId'] = self.merchant_id
251
        data['interfaceVersion'] = 'CR_WS_2.3'
252
        data['keyVersion'] = self.key_version
253
        data['currencyCode'] = self.currency_code
254
        data['seal'] = self.get_seal_for_json_ws_data(data)
255
        url = self.WS_URL.get(self.platform) + '/rs-services/v2/cashManagement/%s' % endpoint
256
        self.logger.debug('posting %r to %s endpoint', data, endpoint)
257
        response = requests.post(url, data=json.dumps(data),
258
                headers={'content-type': 'application/json',
259
                         'accept': 'application/json'})
260
        self.logger.debug('received %r', response.content)
261
        response.raise_for_status()
262
        json_response = response.json()
263
        if self.platform == 'prod':
264
            # test environment doesn't set seal
265
            if json_response.get('seal') != self.get_seal_for_json_ws_data(json_response):
266
                raise ResponseError('wrong signature on response')
267
        if json_response.get('responseCode') != '00':
268
            raise ResponseError('non-zero response code (%s)' % json_response)
269
        return json_response
270

  
271
    def cancel(self, amount, bank_data, **kwargs):
272
        data = {}
273
        data['operationAmount'] = unicode(int(Decimal(amount) * 100))
274
        data['transactionReference'] = bank_data.get('transactionReference')
275
        return self.perform_cash_management_operation('cancel', data)
276

  
277
    def validate(self, amount, bank_data, **kwargs):
278
        data = {}
279
        data['operationAmount'] = unicode(int(Decimal(amount) * 100))
280
        data['transactionReference'] = bank_data.get('transactionReference')
281
        return self.perform_cash_management_operation('validate', data)
282

  
283
    def diagnostic(self, amount, bank_data, **kwargs):
284
        data = {}
285
        data['transactionReference'] = bank_data.get('transactionReference')
286
        data['merchantId'] = self.merchant_id
287
        data['interfaceVersion'] = 'DR_WS_2.9'
288
        data['keyVersion'] = self.key_version
289
        data['seal'] = self.get_seal_for_json_ws_data(data)
290
        url = self.WS_URL.get(self.platform) + '/rs-services/v2/diagnostic/getTransactionData'
291
        self.logger.debug('posting %r to %s endpoint', data, 'diagnostic')
292
        response = requests.post(url, data=json.dumps(data),
293
                headers={'content-type': 'application/json',
294
                         'accept': 'application/json'})
295
        self.logger.debug('received %r', response.content)
296
        response.raise_for_status()
297
        return response.json()
setup.py
94 94
    maintainer_email="bdauvergne@entrouvert.com",
95 95
    packages=['eopayment'],
96 96
    install_requires=[
97
        'pycrypto >= 2.5'
97
        'pycrypto >= 2.5',
98
        'requests',
98 99
    ],
99 100
)
100
-