2018-07-07 22:01:06 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2020-11-23 16:06:37 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
|
2018-07-07 22:01:06 +00:00
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2018 Hugo Levy-Falk
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2018-06-21 18:03:46 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.shortcuts import render
|
2018-07-03 12:49:13 +00:00
|
|
|
from django.urls import reverse
|
2018-06-23 17:54:20 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-06-21 18:03:46 +00:00
|
|
|
|
2018-07-02 19:13:13 +00:00
|
|
|
from cotisations.models import Paiement
|
|
|
|
from cotisations.payment_methods.mixins import PaymentMethodMixin
|
2018-06-21 18:03:46 +00:00
|
|
|
|
2018-07-09 18:35:25 +00:00
|
|
|
from re2o.aes_field import AESEncryptedField
|
2018-07-03 12:49:13 +00:00
|
|
|
from .comnpay import Transaction
|
2018-06-21 18:03:46 +00:00
|
|
|
|
|
|
|
|
2018-07-02 19:13:13 +00:00
|
|
|
class ComnpayPayment(PaymentMethodMixin, models.Model):
|
2018-06-21 18:03:46 +00:00
|
|
|
"""
|
2018-07-02 19:13:13 +00:00
|
|
|
The model allowing you to pay with COMNPAY.
|
2018-06-21 18:03:46 +00:00
|
|
|
"""
|
2018-07-15 12:58:04 +00:00
|
|
|
|
|
|
|
class Meta:
|
2018-06-23 17:54:20 +00:00
|
|
|
verbose_name = _("ComNpay")
|
2018-07-15 12:58:04 +00:00
|
|
|
|
2018-07-02 19:13:13 +00:00
|
|
|
payment = models.OneToOneField(
|
|
|
|
Paiement,
|
2018-07-07 22:01:06 +00:00
|
|
|
on_delete=models.CASCADE,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="payment_method_comnpay",
|
|
|
|
editable=False,
|
2018-07-02 19:13:13 +00:00
|
|
|
)
|
2018-06-21 18:03:46 +00:00
|
|
|
payment_credential = models.CharField(
|
2019-11-04 16:55:03 +00:00
|
|
|
max_length=255, default="", blank=True, verbose_name=_("ComNpay VAT Number")
|
2018-06-21 18:03:46 +00:00
|
|
|
)
|
|
|
|
payment_pass = AESEncryptedField(
|
2019-11-04 16:55:03 +00:00
|
|
|
max_length=255, null=True, blank=True, verbose_name=_("ComNpay secret key")
|
2018-06-21 18:03:46 +00:00
|
|
|
)
|
2018-07-13 18:19:57 +00:00
|
|
|
minimum_payment = models.DecimalField(
|
2019-11-16 14:02:32 +00:00
|
|
|
verbose_name=_("minimum payment"),
|
2019-11-04 16:55:03 +00:00
|
|
|
help_text=_(
|
2019-11-16 14:02:32 +00:00
|
|
|
"The minimal amount of money you have to use when paying with"
|
|
|
|
" ComNpay."
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2018-07-13 18:19:57 +00:00
|
|
|
max_digits=5,
|
|
|
|
decimal_places=2,
|
|
|
|
default=1,
|
|
|
|
)
|
2018-08-11 23:09:48 +00:00
|
|
|
production = models.BooleanField(
|
|
|
|
default=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_(
|
2019-11-16 14:02:32 +00:00
|
|
|
"production mode enabled (production URL, instead of homologation)"
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2018-08-11 23:09:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def return_url_comnpay(self):
|
|
|
|
if self.production:
|
2019-11-04 16:55:03 +00:00
|
|
|
return "https://secure.comnpay.com"
|
2018-08-11 23:09:48 +00:00
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
return "https://secure.homologation.comnpay.com"
|
2018-06-21 18:03:46 +00:00
|
|
|
|
|
|
|
def end_payment(self, invoice, request):
|
2018-07-03 12:49:13 +00:00
|
|
|
"""
|
|
|
|
Build a request to start the negociation with Comnpay by using
|
|
|
|
a facture id, the price and the secret transaction data stored in
|
|
|
|
the preferences.
|
|
|
|
"""
|
|
|
|
host = request.get_host()
|
|
|
|
p = Transaction(
|
|
|
|
str(self.payment_credential),
|
|
|
|
str(self.payment_pass),
|
2019-11-04 16:55:03 +00:00
|
|
|
"https://"
|
|
|
|
+ host
|
|
|
|
+ reverse(
|
|
|
|
"cotisations:comnpay:accept_payment", kwargs={"factureid": invoice.id}
|
2018-07-03 12:49:13 +00:00
|
|
|
),
|
2019-11-04 16:55:03 +00:00
|
|
|
"https://" + host + reverse("cotisations:comnpay:refuse_payment"),
|
|
|
|
"https://" + host + reverse("cotisations:comnpay:ipn"),
|
2018-07-03 12:49:13 +00:00
|
|
|
"",
|
2019-11-04 16:55:03 +00:00
|
|
|
"D",
|
2018-07-03 12:49:13 +00:00
|
|
|
)
|
2018-08-11 23:09:48 +00:00
|
|
|
|
2018-07-03 12:49:13 +00:00
|
|
|
r = {
|
2019-11-04 16:55:03 +00:00
|
|
|
"action": self.return_url_comnpay(),
|
|
|
|
"method": "POST",
|
|
|
|
"content": p.buildSecretHTML(
|
|
|
|
_("Pay invoice number ") + str(invoice.id),
|
2018-07-03 12:49:13 +00:00
|
|
|
invoice.prix_total(),
|
2019-11-04 16:55:03 +00:00
|
|
|
idTransaction=str(invoice.id),
|
2018-07-03 12:49:13 +00:00
|
|
|
),
|
2019-11-04 16:55:03 +00:00
|
|
|
"amount": invoice.prix_total(),
|
2018-07-03 12:49:13 +00:00
|
|
|
}
|
2019-11-04 16:55:03 +00:00
|
|
|
return render(request, "cotisations/payment.html", r)
|
2018-07-13 18:19:57 +00:00
|
|
|
|
2018-07-13 20:03:22 +00:00
|
|
|
def check_price(self, price, *args, **kwargs):
|
|
|
|
"""Checks that the price meets the requirement to be paid with ComNpay.
|
2018-07-13 18:19:57 +00:00
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
return (
|
|
|
|
(price >= self.minimum_payment),
|
|
|
|
_(
|
|
|
|
"In order to pay your invoice with ComNpay, the price must"
|
|
|
|
" be greater than {} €."
|
|
|
|
).format(self.minimum_payment),
|
|
|
|
)
|