2018-04-14 13:39:51 +00:00
|
|
|
"""cotisations.payment_utils.comnpay
|
|
|
|
The module in charge of handling the negociation with Comnpay
|
|
|
|
for online payment
|
|
|
|
"""
|
|
|
|
|
2018-01-12 00:07:25 +00:00
|
|
|
import base64
|
|
|
|
import hashlib
|
2021-02-10 10:06:09 +00:00
|
|
|
import time
|
2018-01-12 00:07:25 +00:00
|
|
|
from collections import OrderedDict
|
2021-02-10 10:06:09 +00:00
|
|
|
from random import randrange
|
2018-01-12 00:07:25 +00:00
|
|
|
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
class Transaction:
|
2021-02-10 10:06:09 +00:00
|
|
|
"""The class representing a transaction with all the functions
|
2018-04-14 13:39:51 +00:00
|
|
|
used during the negociation
|
|
|
|
"""
|
2018-01-12 00:07:25 +00:00
|
|
|
|
2019-11-04 16:55:03 +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-14 13:39:51 +00:00
|
|
|
self.idTransaction = ""
|
2018-01-12 00:07:25 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
def buildSecretHTML(self, produit="Produit", montant="0.00", idTransaction=""):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Build an HTML hidden form with the different parameters for the
|
2018-04-14 13:39:51 +00:00
|
|
|
transaction
|
|
|
|
"""
|
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(
|
2018-04-14 13:39:51 +00:00
|
|
|
montant=str(montant),
|
|
|
|
idTPE=self.vad_number,
|
|
|
|
idTransaction=self.idTransaction,
|
|
|
|
devise="EUR",
|
2019-11-04 16:55:03 +00:00
|
|
|
lang="fr",
|
2018-04-14 13:39:51 +00:00
|
|
|
nom_produit=produit,
|
|
|
|
source=self.source,
|
|
|
|
urlRetourOK=self.urlRetourOK,
|
|
|
|
urlRetourNOK=self.urlRetourNOK,
|
2019-11-04 16:55:03 +00:00
|
|
|
typeTr=str(self.typeTr),
|
2018-01-12 00:07:25 +00:00
|
|
|
)
|
|
|
|
|
2018-04-13 18:58:29 +00:00
|
|
|
if self.urlIPN != "":
|
2019-11-04 16:55:03 +00:00
|
|
|
array_tpe["urlIPN"] = self.urlIPN
|
2018-01-12 00:07:25 +00:00
|
|
|
|
2019-11-04 16:55:03 +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"]
|
2019-11-04 16:55:03 +00:00
|
|
|
array_tpe["sec"] = hashlib.sha512(strWithKey).hexdigest()
|
2018-01-12 00:07:25 +00:00
|
|
|
|
|
|
|
ret = ""
|
|
|
|
for key in array_tpe:
|
2018-04-13 18:58:29 +00:00
|
|
|
ret += '<input type="hidden" name="{k}" value="{v}"/>'.format(
|
2019-11-04 16:55:03 +00:00
|
|
|
k=key, v=array_tpe[key]
|
2018-04-13 18:58:29 +00:00
|
|
|
)
|
2018-01-12 00:07:25 +00:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2018-04-14 13:39:51 +00:00
|
|
|
@staticmethod
|
|
|
|
def validSec(values, secret_key):
|
|
|
|
""" Check if the secret value is correct """
|
2018-01-12 00:07:25 +00:00
|
|
|
if "sec" in values:
|
2019-11-04 16:55:03 +00:00
|
|
|
sec = values["sec"]
|
2018-01-12 00:07:25 +00:00
|
|
|
del values["sec"]
|
2019-11-04 16:55:03 +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
|