8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-27 15:12:25 +00:00
re2o/cotisations/payment_utils/comnpay.py

82 lines
2.4 KiB
Python
Raw Normal View History

2018-01-12 00:07:25 +00:00
import time
from random import randrange
import base64
import hashlib
from collections import OrderedDict
from itertools import chain
2018-04-13 18:58:29 +00:00
2018-01-12 00:07:25 +00:00
class Payment():
vad_number = ""
secret_key = ""
urlRetourOK = ""
urlRetourNOK = ""
urlIPN = ""
source = ""
typeTr = "D"
2018-04-13 18:58:29 +00:00
def __init__(self, vad_number="", secret_key="", urlRetourOK="",
urlRetourNOK="", urlIPN="", source="", typeTr="D"):
2018-01-12 00:07:25 +00:00
self.vad_number = vad_number
self.secret_key = secret_key
self.urlRetourOK = urlRetourOK
self.urlRetourNOK = urlRetourNOK
self.urlIPN = urlIPN
self.source = source
self.typeTr = typeTr
2018-04-13 18:58:29 +00:00
def buildSecretHTML(self, produit="Produit", montant="0.00",
idTransaction=""):
2018-01-12 00:07:25 +00:00
if idTransaction == "":
2018-04-13 18:58:29 +00:00
self.idTransaction = str(time.time())
self.idTransaction += self.vad_number
self.idTransaction += str(randrange(999))
2018-01-12 00:07:25 +00:00
else:
self.idTransaction = idTransaction
2018-04-13 18:58:29 +00:00
array_tpe = OrderedDict(
montant=str(montant),
idTPE=self.vad_number,
idTransaction=self.idTransaction,
devise="EUR",
lang='fr',
nom_produit=produit,
source=self.source,
urlRetourOK=self.urlRetourOK,
urlRetourNOK=self.urlRetourNOK,
typeTr=str(self.typeTr)
2018-01-12 00:07:25 +00:00
)
2018-04-13 18:58:29 +00:00
if self.urlIPN != "":
2018-01-12 00:07:25 +00:00
array_tpe['urlIPN'] = self.urlIPN
2018-04-13 18:58:29 +00:00
array_tpe['key'] = self.secret_key
strWithKey = base64.b64encode(bytes(
'|'.join(array_tpe.values()),
'utf-8'
))
2018-01-12 00:07:25 +00:00
del array_tpe["key"]
array_tpe['sec'] = hashlib.sha512(strWithKey).hexdigest()
ret = ""
for key in array_tpe:
2018-04-13 18:58:29 +00:00
ret += '<input type="hidden" name="{k}" value="{v}"/>'.format(
'k'=key,
'v'=array_type[key]
)
2018-01-12 00:07:25 +00:00
return ret
def validSec(self, values, secret_key):
if "sec" in values:
sec = values['sec']
del values["sec"]
2018-04-13 18:58:29 +00:00
strWithKey = hashlib.sha512(base64.b64encode(bytes(
'|'.join(values.values()) + "|" + secret_key,
'utf-8'
))).hexdigest()
2018-01-12 00:07:25 +00:00
return strWithKey.upper() == sec.upper()
else:
return False