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()
|