8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-23 15:33:45 +00:00
re2o/cotisations/payment_methods/comnpay/comnpay.py
Hugo Levy-Falk c4a104b3b6 I like my black.
Just ran black on the whoe repository. Fix #210.
2019-11-04 22:47:24 +01:00

90 lines
2.6 KiB
Python

"""cotisations.payment_utils.comnpay
The module in charge of handling the negociation with Comnpay
for online payment
"""
import time
from random import randrange
import base64
import hashlib
from collections import OrderedDict
class Transaction:
""" The class representing a transaction with all the functions
used during the negociation
"""
def __init__(
self,
vad_number="",
secret_key="",
urlRetourOK="",
urlRetourNOK="",
urlIPN="",
source="",
typeTr="D",
):
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
self.idTransaction = ""
def buildSecretHTML(self, produit="Produit", montant="0.00", idTransaction=""):
""" Build an HTML hidden form with the different parameters for the
transaction
"""
if idTransaction == "":
self.idTransaction = str(time.time())
self.idTransaction += self.vad_number
self.idTransaction += str(randrange(999))
else:
self.idTransaction = idTransaction
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),
)
if self.urlIPN != "":
array_tpe["urlIPN"] = self.urlIPN
array_tpe["key"] = self.secret_key
strWithKey = base64.b64encode(bytes("|".join(array_tpe.values()), "utf-8"))
del array_tpe["key"]
array_tpe["sec"] = hashlib.sha512(strWithKey).hexdigest()
ret = ""
for key in array_tpe:
ret += '<input type="hidden" name="{k}" value="{v}"/>'.format(
k=key, v=array_tpe[key]
)
return ret
@staticmethod
def validSec(values, secret_key):
""" Check if the secret value is correct """
if "sec" in values:
sec = values["sec"]
del values["sec"]
strWithKey = hashlib.sha512(
base64.b64encode(
bytes("|".join(values.values()) + "|" + secret_key, "utf-8")
)
).hexdigest()
return strWithKey.upper() == sec.upper()
else:
return False