models.py 3.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# 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.
21 22
from django.db import models
from django.shortcuts import render
23 24
from django.urls import reverse
from django.utils.translation import ugettext as _
25
from django.utils.translation import ugettext_lazy as _l
26

27 28
from cotisations.models import Paiement
from cotisations.payment_methods.mixins import PaymentMethodMixin
29

30
from re2o.aes_field import AESEncryptedField
31
from .comnpay import Transaction
32 33


34
class ComnpayPayment(PaymentMethodMixin, models.Model):
35
    """
36
    The model allowing you to pay with COMNPAY.
37
    """
38 39
    payment = models.OneToOneField(
        Paiement,
40
        on_delete=models.CASCADE,
41 42 43
        related_name='payment_method',
        editable=False
    )
44 45 46
    payment_credential = models.CharField(
        max_length=255,
        default='',
47 48
        blank=True,
        verbose_name=_l("ComNpay VAD Number"),
49 50 51 52 53
    )
    payment_pass = AESEncryptedField(
        max_length=255,
        null=True,
        blank=True,
54
        verbose_name=_l("ComNpay Secret Key"),
55
    )
56 57 58 59 60 61 62 63
    minimum_payment = models.DecimalField(
        verbose_name=_l("Minimum payment"),
        help_text=_l("The minimal amount of money you have to use when paying"
                     " with ComNpay"),
        max_digits=5,
        decimal_places=2,
        default=1,
    )
64 65

    def end_payment(self, invoice, request):
66 67 68 69 70
        """
        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.
        """
71 72
        invoice.valid = False
        invoice.save()
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        host = request.get_host()
        p = Transaction(
            str(self.payment_credential),
            str(self.payment_pass),
            'https://' + host + reverse(
                'cotisations:comnpay:accept_payment',
                kwargs={'factureid': invoice.id}
            ),
            'https://' + host + reverse('cotisations:comnpay:refuse_payment'),
            'https://' + host + reverse('cotisations:comnpay:ipn'),
            "",
            "D"
        )
        r = {
            'action': 'https://secure.homologation.comnpay.com',
            'method': 'POST',
            'content': p.buildSecretHTML(
                _("Pay invoice no : ")+str(invoice.id),
                invoice.prix_total(),
                idTransaction=str(invoice.id)
            ),
            'amount': invoice.prix_total(),
        }
        return render(request, 'cotisations/payment.html', r)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    def check_invoice(self, invoice_form):
        """Checks that a invoice meets the requirement to be paid with ComNpay.

        Args:
            invoice_form: The invoice_form which is to be checked.

        Returns:
            True if the form is valid for ComNpay.

        """
        if invoice_form.instance.prix_total() < self.minimum_payment:
            invoice_form.add_error(
                'paiement',
                _('In order to pay your invoice with ComNpay'
                  ', the price must be grater than {} €')
                .format(self.minimum_payment)
            )
            return False
        return True