1 | ## $Id: payments.py 16526 2021-07-05 15:42:14Z 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 zope.schema.interfaces import ConstraintNotSatisfied |
---|
25 | from hurry.workflow.interfaces import IWorkflowInfo |
---|
26 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
27 | from waeup.kofa.students.interfaces import ( |
---|
28 | IStudentPaymentsContainer, IStudentNavigation, IStudentOnlinePayment) |
---|
29 | from waeup.kofa.students.workflow import CLEARED, RETURNING, PAID |
---|
30 | from waeup.kofa.payments import PaymentsContainer, OnlinePayment |
---|
31 | from waeup.kofa.payments.interfaces import IPayer |
---|
32 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
33 | from waeup.kofa.accesscodes import create_accesscode |
---|
34 | |
---|
35 | class 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 | |
---|
59 | StudentPaymentsContainer = attrs_to_fields(StudentPaymentsContainer) |
---|
60 | |
---|
61 | class 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 | |
---|
174 | StudentOnlinePayment = attrs_to_fields( |
---|
175 | StudentOnlinePayment, omit=['display_item']) |
---|
176 | |
---|
177 | class 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 payer(self): |
---|
185 | "The payer object" |
---|
186 | return self.context.student |
---|
187 | |
---|
188 | @property |
---|
189 | def display_fullname(self): |
---|
190 | "Name of payer" |
---|
191 | return self.context.student.display_fullname |
---|
192 | |
---|
193 | @property |
---|
194 | def id(self): |
---|
195 | "Id of payer" |
---|
196 | return self.context.student.student_id |
---|
197 | |
---|
198 | @property |
---|
199 | def matric_number(self): |
---|
200 | "Matric number or reg number of payer" |
---|
201 | return self.context.student.matric_number |
---|
202 | |
---|
203 | @property |
---|
204 | def reg_number(self): |
---|
205 | "Reg number or reg number of payer" |
---|
206 | return self.context.student.reg_number |
---|
207 | |
---|
208 | @property |
---|
209 | def faculty(self): |
---|
210 | "Faculty of payer" |
---|
211 | return self.context.student.faccode |
---|
212 | |
---|
213 | @property |
---|
214 | def department(self): |
---|
215 | "Department of payer" |
---|
216 | return self.context.student.depcode |
---|
217 | |
---|
218 | @property |
---|
219 | def email(self): |
---|
220 | "Email of payer" |
---|
221 | return self.context.student.email |
---|
222 | |
---|
223 | @property |
---|
224 | def phone(self): |
---|
225 | "Phone number of payer" |
---|
226 | return self.context.student.phone |
---|
227 | |
---|
228 | @property |
---|
229 | def current_mode(self): |
---|
230 | "Current study mode of payer" |
---|
231 | return self.context.student.current_mode |
---|
232 | |
---|
233 | @property |
---|
234 | def current_level(self): |
---|
235 | "Current level of payer" |
---|
236 | return self.context.student.current_level |
---|
237 | |
---|
238 | def doAfterPayment(self): |
---|
239 | "Do after payment was made." |
---|
240 | return self.context.student.doAfterStudentPayment() |
---|
241 | |
---|
242 | # Student online payments must be importable. So we might need a factory. |
---|
243 | class StudentOnlinePaymentFactory(grok.GlobalUtility): |
---|
244 | """A factory for student online payments. |
---|
245 | """ |
---|
246 | grok.implements(IFactory) |
---|
247 | grok.name(u'waeup.StudentOnlinePayment') |
---|
248 | title = u"Create a new online payment.", |
---|
249 | description = u"This factory instantiates new online payment instances." |
---|
250 | |
---|
251 | def __call__(self, *args, **kw): |
---|
252 | return StudentOnlinePayment() |
---|
253 | |
---|
254 | def getInterfaces(self): |
---|
255 | return implementedBy(StudentOnlinePayment) |
---|