source: main/waeup.kofa/trunk/src/waeup/kofa/students/payments.py @ 16254

Last change on this file since 16254 was 15325, checked in by Henrik Bettermann, 6 years ago

Harmonize 'Payment successful' flash message.

  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
1## $Id: payments.py 15325 2019-02-08 08:27:51Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""
19Student payment components.
20"""
21import grok
22from zope.component.interfaces import IFactory
23from zope.interface import implementedBy
24from zope.schema.interfaces import ConstraintNotSatisfied
25from hurry.workflow.interfaces import IWorkflowInfo
26from waeup.kofa.interfaces import MessageFactory as _
27from waeup.kofa.students.interfaces import (
28    IStudentPaymentsContainer, IStudentNavigation, IStudentOnlinePayment)
29from waeup.kofa.students.workflow import CLEARED, RETURNING, PAID
30from waeup.kofa.payments import PaymentsContainer, OnlinePayment
31from waeup.kofa.payments.interfaces import IPayer
32from waeup.kofa.utils.helpers import attrs_to_fields
33from waeup.kofa.accesscodes import create_accesscode
34
35class StudentPaymentsContainer(PaymentsContainer):
36    """This is a container for student payments.
37    """
38    grok.implements(IStudentPaymentsContainer, IStudentNavigation)
39    grok.provides(IStudentPaymentsContainer)
40
41    def __init__(self):
42        super(StudentPaymentsContainer, self).__init__()
43        return
44
45    @property
46    def student(self):
47        return self.__parent__
48
49    @property
50    def certificate(self):
51        try:
52            return self.student['studycourse'].certificate
53        except TypeError:
54            return None
55
56    def writeLogMessage(self, view, message):
57        return self.__parent__.writeLogMessage(view, message)
58
59StudentPaymentsContainer = attrs_to_fields(StudentPaymentsContainer)
60
61class StudentOnlinePayment(OnlinePayment):
62    """This is an online payment.
63    """
64    grok.implements(IStudentOnlinePayment, IStudentNavigation)
65    grok.provides(IStudentOnlinePayment)
66
67    def __init__(self):
68        super(StudentOnlinePayment, self).__init__()
69        return
70
71    @property
72    def student(self):
73        try:
74            return self.__parent__.__parent__
75        except AttributeError:
76            return None
77
78    @property
79    def certificate(self):
80        try:
81            return self.student['studycourse'].certificate
82        except TypeError:
83            return None
84
85    def writeLogMessage(self, view, message):
86        return self.__parent__.__parent__.writeLogMessage(view, message)
87
88    def redeemTicket(self):
89        """Either create an appropriate access code or trigger an action
90        directly.
91        """
92        student = self.student
93        if self.p_category == 'clearance':
94            # Create CLR access code
95            pin, error = create_accesscode(
96                'CLR',0,self.amount_auth,student.student_id)
97            if error:
98                return error
99            self.ac = pin
100        elif self.p_category.startswith('schoolfee'):
101            # Bypass activation code creation if next session
102            # can be started directly.
103            if student['studycourse'].next_session_allowed:
104                try:
105                    if student.state == CLEARED:
106                        IWorkflowInfo(student).fireTransition(
107                            'pay_first_school_fee')
108                        return None
109                    elif student.state == RETURNING:
110                        IWorkflowInfo(student).fireTransition(
111                            'pay_school_fee')
112                        return None
113                    elif student.state == PAID:
114                        IWorkflowInfo(student).fireTransition(
115                            'pay_pg_fee')
116                        return None
117                except ConstraintNotSatisfied:
118                    pass
119            # Create SFE access code
120            pin, error = create_accesscode(
121                'SFE',0,self.amount_auth,student.student_id)
122            if error:
123                return error
124            self.ac = pin
125        elif self.p_category == 'bed_allocation':
126            # Create HOS access code
127            pin, error = create_accesscode(
128                'HOS',0,self.amount_auth,student.student_id)
129            if error:
130                return error
131            self.ac = pin
132        elif self.p_category == 'transcript':
133            # Create TSC access code
134            pin, error = create_accesscode(
135                'TSC',0,self.amount_auth,student.student_id)
136            if error:
137                return error
138            self.ac = pin
139        return None
140
141    def doAfterStudentPayment(self):
142        """Process student after payment was made.
143        """
144        if self.p_current:
145            error = self.redeemTicket()
146            if error is not None:
147                return 'danger', error, error
148        log = 'successful %s payment: %s' % (self.p_category, self.p_id)
149        msg = _('Payment successfully completed')
150        flashtype = 'success'
151        return flashtype, msg, log
152
153    def doAfterStudentPaymentApproval(self):
154        """Process student after payment was approved.
155        """
156        if self.p_current:
157            error = self.redeemTicket()
158            if error is not None:
159                return 'danger', error, error
160        log = '%s payment approved: %s' % (self.p_category, self.p_id)
161        msg = _('Payment approved')
162        flashtype = 'success'
163        return flashtype, msg, log
164
165    def approveStudentPayment(self):
166        """Approve payment and process student.
167        """
168        if self.p_state == 'paid':
169            return 'warning', _('This ticket has already been paid.'), None
170        self.approve()
171        return self.doAfterStudentPaymentApproval()
172
173
174StudentOnlinePayment = attrs_to_fields(
175    StudentOnlinePayment, omit=['display_item'])
176
177class Payer(grok.Adapter):
178    """An adapter to publish student data through a simple webservice.
179    """
180    grok.context(IStudentOnlinePayment)
181    grok.implements(IPayer)
182
183    @property
184    def display_fullname(self):
185        "Name of  payer"
186        return self.context.student.display_fullname
187
188    @property
189    def id(self):
190        "Id of payer"
191        return self.context.student.student_id
192
193    @property
194    def matric_number(self):
195        "Matric number or reg number of payer"
196        return self.context.student.matric_number
197
198    @property
199    def reg_number(self):
200        "Reg number or reg number of payer"
201        return self.context.student.reg_number
202
203    @property
204    def faculty(self):
205        "Faculty of payer"
206        return self.context.student.faccode
207
208    @property
209    def department(self):
210        "Department of payer"
211        return self.context.student.depcode
212
213    @property
214    def email(self):
215        "Email of payer"
216        return self.context.student.email
217
218    @property
219    def phone(self):
220        "Phone number of payer"
221        return self.context.student.phone
222
223    @property
224    def current_mode(self):
225        "Current study mode of payer"
226        return self.context.student.current_mode
227
228    @property
229    def current_level(self):
230        "Current level of payer"
231        return self.context.student.current_level
232
233# Student online payments must be importable. So we might need a factory.
234class StudentOnlinePaymentFactory(grok.GlobalUtility):
235    """A factory for student online payments.
236    """
237    grok.implements(IFactory)
238    grok.name(u'waeup.StudentOnlinePayment')
239    title = u"Create a new online payment.",
240    description = u"This factory instantiates new online payment instances."
241
242    def __call__(self, *args, **kw):
243        return StudentOnlinePayment()
244
245    def getInterfaces(self):
246        return implementedBy(StudentOnlinePayment)
Note: See TracBrowser for help on using the repository browser.