views.py 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
# -*- 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  Gabriel Detraz
# Copyright © 2018  Pierre-Antoine Comby 
#
# 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.
"""Payment

Here are the views needed by comnpay
"""

from collections import OrderedDict

from django.urls import reverse
from django.shortcuts import redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.views.decorators.csrf import csrf_exempt
from django.utils.datastructures import MultiValueDictKeyError
from django.utils.translation import ugettext as _
from django.http import HttpResponse, HttpResponseBadRequest

from cotisations.models import Facture
from cotisations.utils import find_payment_method
from .models import NotePayment
from re2o.views import form
from re2o.acl import (
    can_create,
    can_edit
)
from .note import login, don
from .forms import NoteCredentialForm

@login_required
@can_edit(Facture)
def note_payment(request, facture, factureid):
    """
    Build a request to start the negociation with NoteKfet by using
    a facture id, the price and the login/password data stored in
    the preferences.
    """
    user = facture.user
    payment_method = find_payment_method(facture.paiement)
    if not payment_method or not isinstance(payment_method, NotePayment):
60
        messages.error(request, _("Unknown error."))
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        return redirect(reverse(
            'users:profil',
            kwargs={'userid': user.id}
         ))
    noteform = NoteCredentialForm(request.POST or None)
    if noteform.is_valid():
        pseudo = noteform.cleaned_data['login']
        password = noteform.cleaned_data['password']
        result, sock, err = login(payment_method.server, payment_method.port, pseudo, password)
        if not result:
            messages.error(request, err)
            return form(
                {'form': noteform, 'amount': facture.prix_total()},
                "cotisations/payment.html",
                request
            )
        else:
            result, err = don(sock, facture.prix_total(), payment_method.id_note, facture)
            if not result:
                messages.error(request, err)
                return form(
                    {'form': noteform, 'amount': facture.prix_total()},
                    "cotisations/payment.html",
                    request
                )
        facture.valid = True
        facture.save()
88
        messages.success(request, _("The payment with note was done."))
89 90 91 92 93 94 95 96 97
        return redirect(reverse(
            'users:profil',
            kwargs={'userid': user.id}
         ))
    return form(
            {'form': noteform, 'amount': facture.prix_total()},
            "cotisations/payment.html",
            request
        )