1 | ## $Id: payments.py 10030 2013-03-17 08:40:59Z 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 | """ |
---|
19 | Student payment components. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from zope.component.interfaces import IFactory |
---|
23 | from zope.interface import implementedBy |
---|
24 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
25 | from waeup.kofa.students.interfaces import ( |
---|
26 | IStudentPaymentsContainer, IStudentNavigation, IStudentOnlinePayment) |
---|
27 | from waeup.kofa.payments import PaymentsContainer, OnlinePayment |
---|
28 | from waeup.kofa.payments.interfaces import IPayer |
---|
29 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
30 | from waeup.kofa.accesscodes import create_accesscode |
---|
31 | |
---|
32 | class StudentPaymentsContainer(PaymentsContainer): |
---|
33 | """This is a container for student payments. |
---|
34 | """ |
---|
35 | grok.implements(IStudentPaymentsContainer, IStudentNavigation) |
---|
36 | grok.provides(IStudentPaymentsContainer) |
---|
37 | |
---|
38 | def __init__(self): |
---|
39 | super(StudentPaymentsContainer, self).__init__() |
---|
40 | return |
---|
41 | |
---|
42 | @property |
---|
43 | def student(self): |
---|
44 | return self.__parent__ |
---|
45 | |
---|
46 | def writeLogMessage(self, view, message): |
---|
47 | return self.__parent__.writeLogMessage(view, message) |
---|
48 | |
---|
49 | StudentPaymentsContainer = attrs_to_fields(StudentPaymentsContainer) |
---|
50 | |
---|
51 | class StudentOnlinePayment(OnlinePayment): |
---|
52 | """This is an online payment. |
---|
53 | """ |
---|
54 | grok.implements(IStudentOnlinePayment, IStudentNavigation) |
---|
55 | grok.provides(IStudentOnlinePayment) |
---|
56 | |
---|
57 | def __init__(self): |
---|
58 | super(StudentOnlinePayment, self).__init__() |
---|
59 | return |
---|
60 | |
---|
61 | @property |
---|
62 | def student(self): |
---|
63 | try: |
---|
64 | return self.__parent__.__parent__ |
---|
65 | except AttributeError: |
---|
66 | return None |
---|
67 | |
---|
68 | @property |
---|
69 | def student_state(self): |
---|
70 | try: |
---|
71 | return self.student.state |
---|
72 | except AttributeError: |
---|
73 | return None |
---|
74 | |
---|
75 | @property |
---|
76 | def current_session(self): |
---|
77 | try: |
---|
78 | return self.student.current_session |
---|
79 | except AttributeError: |
---|
80 | return None |
---|
81 | |
---|
82 | def writeLogMessage(self, view, message): |
---|
83 | return self.__parent__.__parent__.writeLogMessage(view, message) |
---|
84 | |
---|
85 | def _createActivationCodes(self): |
---|
86 | student = self.student |
---|
87 | if self.p_category == 'clearance': |
---|
88 | # Create CLR access code |
---|
89 | pin, error = create_accesscode( |
---|
90 | 'CLR',0,self.amount_auth,student.student_id) |
---|
91 | if error: |
---|
92 | return error |
---|
93 | self.ac = pin |
---|
94 | elif self.p_category in ('schoolfee', 'schoolfee_1'): |
---|
95 | # Create SFE access code |
---|
96 | pin, error = create_accesscode( |
---|
97 | 'SFE',0,self.amount_auth,student.student_id) |
---|
98 | if error: |
---|
99 | return error |
---|
100 | self.ac = pin |
---|
101 | elif self.p_category == 'bed_allocation': |
---|
102 | # Create HOS access code |
---|
103 | pin, error = create_accesscode( |
---|
104 | 'HOS',0,self.amount_auth,student.student_id) |
---|
105 | if error: |
---|
106 | return error |
---|
107 | self.ac = pin |
---|
108 | return None |
---|
109 | |
---|
110 | def doAfterStudentPayment(self): |
---|
111 | """Process student after payment was made. |
---|
112 | """ |
---|
113 | if self.p_current: |
---|
114 | error = self._createActivationCodes() |
---|
115 | if error is not None: |
---|
116 | return False, error, error |
---|
117 | log = 'successful %s payment: %s' % (self.p_category, self.p_id) |
---|
118 | msg = _('Successful payment') |
---|
119 | return True, msg, log |
---|
120 | |
---|
121 | def doAfterStudentPaymentApproval(self): |
---|
122 | """Process student after payment was approved. |
---|
123 | """ |
---|
124 | if self.p_current: |
---|
125 | error = self._createActivationCodes() |
---|
126 | if error is not None: |
---|
127 | return False, error, error |
---|
128 | log = '%s payment approved: %s' % (self.p_category, self.p_id) |
---|
129 | msg = _('Payment approved') |
---|
130 | return True, msg, log |
---|
131 | |
---|
132 | def approveStudentPayment(self): |
---|
133 | """Approve payment and process student. |
---|
134 | """ |
---|
135 | if self.p_state == 'paid': |
---|
136 | return False, _('This ticket has already been paid.'), None |
---|
137 | self.approve() |
---|
138 | return self.doAfterStudentPaymentApproval() |
---|
139 | |
---|
140 | |
---|
141 | StudentOnlinePayment = attrs_to_fields( |
---|
142 | StudentOnlinePayment, omit=['display_item']) |
---|
143 | |
---|
144 | class Payer(grok.Adapter): |
---|
145 | """An adapter to publish student data through a simple webservice. |
---|
146 | """ |
---|
147 | grok.context(IStudentOnlinePayment) |
---|
148 | grok.implements(IPayer) |
---|
149 | |
---|
150 | @property |
---|
151 | def display_fullname(self): |
---|
152 | "Name of payer" |
---|
153 | return self.context.student.display_fullname |
---|
154 | |
---|
155 | @property |
---|
156 | def id(self): |
---|
157 | "Id of payer" |
---|
158 | return self.context.student.student_id |
---|
159 | |
---|
160 | @property |
---|
161 | def matric_number(self): |
---|
162 | "Matric number or reg number of payer" |
---|
163 | return self.context.student.matric_number |
---|
164 | |
---|
165 | @property |
---|
166 | def reg_number(self): |
---|
167 | "Reg number or reg number of payer" |
---|
168 | return self.context.student.reg_number |
---|
169 | |
---|
170 | @property |
---|
171 | def faculty(self): |
---|
172 | "Faculty of payer" |
---|
173 | return self.context.student.faccode |
---|
174 | |
---|
175 | @property |
---|
176 | def department(self): |
---|
177 | "Department of payer" |
---|
178 | return self.context.student.depcode |
---|
179 | |
---|
180 | # Student online payments must be importable. So we might need a factory. |
---|
181 | class StudentOnlinePaymentFactory(grok.GlobalUtility): |
---|
182 | """A factory for student online payments. |
---|
183 | """ |
---|
184 | grok.implements(IFactory) |
---|
185 | grok.name(u'waeup.StudentOnlinePayment') |
---|
186 | title = u"Create a new online payment.", |
---|
187 | description = u"This factory instantiates new online payment instances." |
---|
188 | |
---|
189 | def __call__(self, *args, **kw): |
---|
190 | return StudentOnlinePayment() |
---|
191 | |
---|
192 | def getInterfaces(self): |
---|
193 | return implementedBy(StudentOnlinePayment) |
---|