1 | ## $Id: browser.py 15529 2019-07-30 09:20:21Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2012 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 | import grok |
---|
19 | from datetime import datetime, timedelta |
---|
20 | from zope.component import getUtility |
---|
21 | from zope.security import checkPermission |
---|
22 | from waeup.kofa.interfaces import IKofaUtils |
---|
23 | from waeup.kofa.utils.helpers import to_timezone |
---|
24 | from waeup.kofa.browser.layout import UtilityView, KofaPage |
---|
25 | from waeup.kofa.browser.viewlets import ManageActionButton |
---|
26 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
27 | from waeup.kofa.students.browser import OnlinePaymentDisplayFormPage as OPDPStudent |
---|
28 | from waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant |
---|
29 | from kofacustom.nigeria.interswitch.helpers import ( |
---|
30 | query_interswitch, write_payments_log) |
---|
31 | from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment |
---|
32 | from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment |
---|
33 | from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment |
---|
34 | from kofacustom.nigeria.interfaces import MessageFactory as _ |
---|
35 | |
---|
36 | # Buttons |
---|
37 | |
---|
38 | def module_activated(session): |
---|
39 | try: |
---|
40 | return getattr(grok.getSite()['configuration'][str(session)], |
---|
41 | 'interswitch_enabled', True) |
---|
42 | except KeyError: |
---|
43 | return False |
---|
44 | |
---|
45 | class InterswitchActionButtonStudent(ManageActionButton): |
---|
46 | grok.order(1) |
---|
47 | grok.context(INigeriaOnlinePayment) |
---|
48 | grok.view(OPDPStudent) |
---|
49 | grok.require('waeup.payStudent') |
---|
50 | icon = 'actionicon_pay.png' |
---|
51 | text = _('CollegePAY') |
---|
52 | target = 'goto_interswitch' |
---|
53 | |
---|
54 | @property |
---|
55 | def target_url(self): |
---|
56 | if not module_activated(self.context.student.current_session): |
---|
57 | return '' |
---|
58 | if self.context.p_state != 'unpaid': |
---|
59 | return '' |
---|
60 | return self.view.url(self.view.context, self.target) |
---|
61 | |
---|
62 | class InterswitchActionButtonApplicant(InterswitchActionButtonStudent): |
---|
63 | grok.view(OPDPApplicant) |
---|
64 | grok.require('waeup.payApplicant') |
---|
65 | |
---|
66 | @property |
---|
67 | def target_url(self): |
---|
68 | if not module_activated(self.context.__parent__.__parent__.year): |
---|
69 | return '' |
---|
70 | if self.context.p_state != 'unpaid': |
---|
71 | return '' |
---|
72 | return self.view.url(self.view.context, self.target) |
---|
73 | |
---|
74 | class InterswitchRequestWebserviceActionButtonStudent(ManageActionButton): |
---|
75 | grok.order(2) |
---|
76 | grok.context(INigeriaOnlinePayment) |
---|
77 | grok.view(OPDPStudent) |
---|
78 | grok.require('waeup.payStudent') |
---|
79 | icon = 'actionicon_call.png' |
---|
80 | text = _('Requery CollegePAY') |
---|
81 | target = 'request_webservice' |
---|
82 | |
---|
83 | @property |
---|
84 | def target_url(self): |
---|
85 | if not module_activated(self.context.student.current_session): |
---|
86 | return '' |
---|
87 | if self.context.p_state in ('paid', 'waived'): |
---|
88 | return '' |
---|
89 | return self.view.url(self.view.context, self.target) |
---|
90 | |
---|
91 | class InterswitchRequestWebserviceActionButtonApplicant( |
---|
92 | InterswitchRequestWebserviceActionButtonStudent): |
---|
93 | grok.view(OPDPApplicant) |
---|
94 | grok.require('waeup.payApplicant') |
---|
95 | |
---|
96 | @property |
---|
97 | def target_url(self): |
---|
98 | if not module_activated(self.context.__parent__.__parent__.year): |
---|
99 | return '' |
---|
100 | if self.context.p_state in ('paid', 'waived'): |
---|
101 | return '' |
---|
102 | return self.view.url(self.view.context, self.target) |
---|
103 | |
---|
104 | class InterswitchVerifyWebserviceActionButtonStudent(ManageActionButton): |
---|
105 | grok.order(3) |
---|
106 | grok.context(INigeriaOnlinePayment) |
---|
107 | grok.view(OPDPStudent) |
---|
108 | grok.require('waeup.manageStudent') |
---|
109 | icon = 'actionicon_call.png' |
---|
110 | text = _('Verify Payment') |
---|
111 | target = 'verify_payment' |
---|
112 | |
---|
113 | @property |
---|
114 | def target_url(self): |
---|
115 | if not module_activated(self.context.student.current_session): |
---|
116 | return '' |
---|
117 | if self.context.p_state != 'paid' \ |
---|
118 | or self.context.r_company != u'interswitch': |
---|
119 | return '' |
---|
120 | return self.view.url(self.view.context, self.target) |
---|
121 | |
---|
122 | class InterswitchVerifyWebserviceActionButtonApplicant( |
---|
123 | InterswitchVerifyWebserviceActionButtonStudent): |
---|
124 | grok.view(OPDPApplicant) |
---|
125 | grok.require('waeup.manageApplication') |
---|
126 | |
---|
127 | @property |
---|
128 | def target_url(self): |
---|
129 | if not module_activated(self.context.__parent__.__parent__.year): |
---|
130 | return '' |
---|
131 | if self.context.p_state != 'paid' \ |
---|
132 | or self.context.r_company != u'interswitch': |
---|
133 | return '' |
---|
134 | return self.view.url(self.view.context, self.target) |
---|
135 | |
---|
136 | # Webservice request views |
---|
137 | |
---|
138 | class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View): |
---|
139 | """ Request webservice view for the CollegePAY gateway |
---|
140 | """ |
---|
141 | grok.context(INigeriaStudentOnlinePayment) |
---|
142 | grok.name('request_webservice') |
---|
143 | grok.require('waeup.payStudent') |
---|
144 | |
---|
145 | product_id = None |
---|
146 | gateway_host = None |
---|
147 | gateway_url = None |
---|
148 | https = True |
---|
149 | mac = None |
---|
150 | |
---|
151 | def update(self): |
---|
152 | if not module_activated(self.context.student.current_session): |
---|
153 | return |
---|
154 | if self.context.p_state in ('paid', 'waived'): |
---|
155 | self.flash(_('This ticket has already been paid.'), type='danger') |
---|
156 | return |
---|
157 | student = self.context.student |
---|
158 | success, msg, log = query_interswitch( |
---|
159 | self.context, self.product_id, |
---|
160 | self.gateway_host, self.gateway_url, |
---|
161 | self.https, self.mac, False) |
---|
162 | student.writeLogMessage(self, log) |
---|
163 | if not success: |
---|
164 | self.flash(msg, type='danger') |
---|
165 | return |
---|
166 | write_payments_log(student.student_id, self.context) |
---|
167 | flashtype, msg, log = self.context.doAfterStudentPayment() |
---|
168 | if log is not None: |
---|
169 | student.writeLogMessage(self, log) |
---|
170 | self.flash(msg, type=flashtype) |
---|
171 | return |
---|
172 | |
---|
173 | def render(self): |
---|
174 | self.redirect(self.url(self.context, '@@index')) |
---|
175 | return |
---|
176 | |
---|
177 | class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View): |
---|
178 | """ Request webservice view for the CollegePAY gateway |
---|
179 | """ |
---|
180 | grok.context(INigeriaApplicantOnlinePayment) |
---|
181 | grok.name('request_webservice') |
---|
182 | grok.require('waeup.payApplicant') |
---|
183 | |
---|
184 | product_id = None |
---|
185 | gateway_host = None |
---|
186 | gateway_url = None |
---|
187 | https = True |
---|
188 | mac = None |
---|
189 | |
---|
190 | def update(self): |
---|
191 | if not module_activated(self.context.__parent__.__parent__.year): |
---|
192 | return |
---|
193 | if self.context.p_state == 'paid': |
---|
194 | self.flash(_('This ticket has already been paid.'), type='danger') |
---|
195 | return |
---|
196 | applicant = self.context.__parent__ |
---|
197 | success, msg, log = query_interswitch( |
---|
198 | self.context, self.product_id, |
---|
199 | self.gateway_host, self.gateway_url, |
---|
200 | self.https, self.mac, False) |
---|
201 | applicant.writeLogMessage(self, log) |
---|
202 | if not success: |
---|
203 | self.flash(msg, type='danger') |
---|
204 | return |
---|
205 | write_payments_log(applicant.applicant_id, self.context) |
---|
206 | flashtype, msg, log = self.context.doAfterApplicantPayment() |
---|
207 | if log is not None: |
---|
208 | applicant.writeLogMessage(self, log) |
---|
209 | self.flash(msg, type=flashtype) |
---|
210 | return |
---|
211 | |
---|
212 | def render(self): |
---|
213 | self.redirect(self.url(self.context.__parent__, 'edit')) |
---|
214 | return |
---|
215 | |
---|
216 | class InterswitchPaymentVerifyWebservicePageStudent(UtilityView, grok.View): |
---|
217 | """ Verify payment view for the CollegePAY gateway |
---|
218 | """ |
---|
219 | grok.context(INigeriaStudentOnlinePayment) |
---|
220 | grok.name('verify_payment') |
---|
221 | grok.require('waeup.manageStudent') |
---|
222 | |
---|
223 | product_id = None |
---|
224 | gateway_host = None |
---|
225 | gateway_url = None |
---|
226 | https = True |
---|
227 | mac = None |
---|
228 | |
---|
229 | def update(self): |
---|
230 | if not module_activated(self.context.student.current_session): |
---|
231 | return |
---|
232 | if self.context.p_state != 'paid' \ |
---|
233 | or self.context.r_company != u'interswitch': |
---|
234 | self.flash(_('This ticket has not been paid.'), type='danger') |
---|
235 | return |
---|
236 | student = self.context.student |
---|
237 | success, msg, log = query_interswitch( |
---|
238 | self.context, self.product_id, |
---|
239 | self.gateway_host, self.gateway_url, |
---|
240 | self.https, self.mac, True) |
---|
241 | student.writeLogMessage(self, log) |
---|
242 | if not success: |
---|
243 | self.flash(msg, type='danger') |
---|
244 | return |
---|
245 | self.flash(msg) |
---|
246 | return |
---|
247 | |
---|
248 | def render(self): |
---|
249 | self.redirect(self.url(self.context, '@@index')) |
---|
250 | return |
---|
251 | |
---|
252 | class InterswitchPaymentVerifyWebservicePageApplicant(UtilityView, grok.View): |
---|
253 | """ Verify payment view for the CollegePAY gateway |
---|
254 | """ |
---|
255 | grok.context(INigeriaApplicantOnlinePayment) |
---|
256 | grok.name('verify_payment') |
---|
257 | grok.require('waeup.manageApplication') |
---|
258 | |
---|
259 | product_id = None |
---|
260 | gateway_host = None |
---|
261 | gateway_url = None |
---|
262 | https = True |
---|
263 | mac = None |
---|
264 | |
---|
265 | def update(self): |
---|
266 | if not module_activated(self.context.__parent__.__parent__.year): |
---|
267 | return |
---|
268 | if self.context.p_state != 'paid' \ |
---|
269 | or self.context.r_company != u'interswitch': |
---|
270 | self.flash(_('This ticket has not been paid.'), type='danger') |
---|
271 | return |
---|
272 | applicant = self.context.__parent__ |
---|
273 | success, msg, log = query_interswitch( |
---|
274 | self.context, self.product_id, |
---|
275 | self.gateway_host, self.gateway_url, |
---|
276 | self.https, self.mac, True) |
---|
277 | applicant.writeLogMessage(self, log) |
---|
278 | if not success: |
---|
279 | self.flash(msg, type='danger') |
---|
280 | return |
---|
281 | self.flash(msg) |
---|
282 | return |
---|
283 | |
---|
284 | def render(self): |
---|
285 | self.redirect(self.url(self.context, '@@index')) |
---|
286 | return |
---|
287 | |
---|
288 | # Forwarding pages |
---|
289 | |
---|
290 | class InterswitchPageStudent(KofaPage): |
---|
291 | """ View which sends a POST request to the Interswitch |
---|
292 | CollegePAY payment gateway. |
---|
293 | """ |
---|
294 | grok.context(INigeriaOnlinePayment) |
---|
295 | grok.name('goto_interswitch') |
---|
296 | grok.template('student_goto_interswitch') |
---|
297 | grok.require('waeup.payStudent') |
---|
298 | label = _('Submit data to CollegePAY (Interswitch Payment Gateway)') |
---|
299 | submit_button = _('Submit') |
---|
300 | |
---|
301 | action = None |
---|
302 | site_name = None |
---|
303 | currency = None |
---|
304 | pay_item_id = None |
---|
305 | product_id = None |
---|
306 | xml_data = None |
---|
307 | hashvalue = None |
---|
308 | |
---|
309 | def init_update(self): |
---|
310 | if self.context.p_state == 'paid': |
---|
311 | return _("Payment ticket can't be re-sent to CollegePAY.") |
---|
312 | now = datetime.utcnow() |
---|
313 | if self.context.creation_date.tzinfo is not None: |
---|
314 | # That's bad. Please store timezone-naive datetimes only! |
---|
315 | now = self.context.creation_date.tzinfo.localize(now) |
---|
316 | time_delta = now - self.context.creation_date |
---|
317 | if time_delta.days > 7: |
---|
318 | return _("This payment ticket is too old. Please create a new ticket.") |
---|
319 | student = self.context.student |
---|
320 | certificate = getattr(student['studycourse'],'certificate',None) |
---|
321 | if certificate is None: |
---|
322 | return _("Study course data are incomplete.") |
---|
323 | kofa_utils = getUtility(IKofaUtils) |
---|
324 | student_utils = getUtility(IStudentsUtils) |
---|
325 | if student_utils.samePaymentMade(student, self.context.p_category, |
---|
326 | self.context.p_item, self.context.p_session): |
---|
327 | return _("This type of payment has already been made.") |
---|
328 | self.amount_auth = int(100 * self.context.amount_auth) |
---|
329 | xmldict = {} |
---|
330 | if certificate is not None: |
---|
331 | xmldict['department'] = certificate.__parent__.__parent__.code |
---|
332 | xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code |
---|
333 | else: |
---|
334 | xmldict['department'] = None |
---|
335 | xmldict['faculty'] = None |
---|
336 | self.category = self.context.category |
---|
337 | tz = kofa_utils.tzinfo |
---|
338 | self.local_date_time = to_timezone( |
---|
339 | self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
340 | self.site_redirect_url = self.url(self.context, 'request_webservice') |
---|
341 | self.student = student |
---|
342 | self.xmldict = xmldict |
---|
343 | return |
---|
344 | |
---|
345 | def update(self): |
---|
346 | if not module_activated(self.context.student.current_session): |
---|
347 | return |
---|
348 | error = self.init_update() |
---|
349 | if error: |
---|
350 | self.flash(error, type='danger') |
---|
351 | self.redirect(self.url(self.context, '@@index')) |
---|
352 | return |
---|
353 | |
---|
354 | class InterswitchPageApplicant(KofaPage): |
---|
355 | """ View which sends a POST request to the Interswitch |
---|
356 | CollegePAY payment gateway. |
---|
357 | """ |
---|
358 | grok.context(INigeriaApplicantOnlinePayment) |
---|
359 | grok.require('waeup.payApplicant') |
---|
360 | grok.template('applicant_goto_interswitch') |
---|
361 | grok.name('goto_interswitch') |
---|
362 | label = _('Submit data to CollegePAY (Interswitch Payment Gateway)') |
---|
363 | submit_button = _('Submit') |
---|
364 | hashvalue = None |
---|
365 | |
---|
366 | action = None |
---|
367 | site_name = None |
---|
368 | currency = None |
---|
369 | pay_item_id = None |
---|
370 | product_id = None |
---|
371 | xml_data = None |
---|
372 | |
---|
373 | def init_update(self): |
---|
374 | if self.context.p_state != 'unpaid': |
---|
375 | return _("Payment ticket can't be re-sent to CollegePAY.") |
---|
376 | if self.context.__parent__.__parent__.expired \ |
---|
377 | and self.context.__parent__.__parent__.strict_deadline: |
---|
378 | return _("Payment ticket can't be send to CollegePAY. " |
---|
379 | "Application period has expired.") |
---|
380 | tz = getUtility(IKofaUtils).tzinfo |
---|
381 | time_delta = datetime.utcnow() - self.context.creation_date |
---|
382 | if time_delta.days > 7: |
---|
383 | return _("This payment ticket is too old. Please create a new ticket.") |
---|
384 | self.applicant = self.context.__parent__ |
---|
385 | self.amount_auth = int(100 * self.context.amount_auth) |
---|
386 | self.category = self.context.category |
---|
387 | tz = getUtility(IKofaUtils).tzinfo |
---|
388 | self.local_date_time = to_timezone( |
---|
389 | self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
390 | self.site_redirect_url = self.url(self.context, 'request_webservice') |
---|
391 | return |
---|
392 | |
---|
393 | def update(self): |
---|
394 | if not module_activated(self.context.__parent__.__parent__.year): |
---|
395 | return |
---|
396 | error = self.init_update() |
---|
397 | if error: |
---|
398 | self.flash(error, type='danger') |
---|
399 | self.redirect(self.url(self.context, '@@index')) |
---|
400 | return |
---|