source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/browser.py @ 15412

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

Redirect to application form after successful payment.

  • Property svn:keywords set to Id
File size: 14.3 KB
Line 
1## $Id: browser.py 15394 2019-04-23 15:40:22Z 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##
18import grok
19from datetime import datetime, timedelta
20from zope.component import getUtility
21from zope.security import checkPermission
22from waeup.kofa.interfaces import IKofaUtils
23from waeup.kofa.utils.helpers import to_timezone
24from waeup.kofa.browser.layout import UtilityView, KofaPage
25from waeup.kofa.browser.viewlets import ManageActionButton
26from waeup.kofa.students.interfaces import IStudentsUtils
27from waeup.kofa.students.browser import OnlinePaymentDisplayFormPage as OPDPStudent
28from waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant
29from kofacustom.nigeria.interswitch.helpers import (
30    query_interswitch, write_payments_log)
31from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
32from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment
33from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
34from kofacustom.nigeria.interfaces import MessageFactory as _
35
36# Buttons
37
38def module_activated(session):
39    try:
40        return getattr(grok.getSite()['configuration'][str(session)],
41            'interswitch_enabled', True)
42    except KeyError:
43        return False
44
45class 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
62class 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
74class 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
91class 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
104class 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
122class 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
138class 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
177class 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        if self.context.p_state == 'paid':
214            self.redirect(self.url(self.context.__parent__))
215            return
216        self.redirect(self.url(self.context))
217        return
218
219class InterswitchPaymentVerifyWebservicePageStudent(UtilityView, grok.View):
220    """ Verify payment view for the CollegePAY gateway
221    """
222    grok.context(INigeriaStudentOnlinePayment)
223    grok.name('verify_payment')
224    grok.require('waeup.manageStudent')
225
226    product_id = None
227    gateway_host = None
228    gateway_url = None
229    https = True
230    mac = None
231
232    def update(self):
233        if not module_activated(self.context.student.current_session):
234            return
235        if self.context.p_state  != 'paid' \
236            or self.context.r_company != u'interswitch':
237            self.flash(_('This ticket has not been paid.'), type='danger')
238            return
239        student = self.context.student
240        success, msg, log = query_interswitch(
241            self.context, self.product_id,
242            self.gateway_host, self.gateway_url,
243            self.https, self.mac, True)
244        student.writeLogMessage(self, log)
245        if not success:
246            self.flash(msg, type='danger')
247            return
248        self.flash(msg)
249        return
250
251    def render(self):
252        self.redirect(self.url(self.context, '@@index'))
253        return
254
255class InterswitchPaymentVerifyWebservicePageApplicant(UtilityView, grok.View):
256    """ Verify payment view for the CollegePAY gateway
257    """
258    grok.context(INigeriaApplicantOnlinePayment)
259    grok.name('verify_payment')
260    grok.require('waeup.manageApplication')
261
262    product_id = None
263    gateway_host = None
264    gateway_url = None
265    https = True
266    mac = None
267
268    def update(self):
269        if not module_activated(self.context.__parent__.__parent__.year):
270            return
271        if self.context.p_state != 'paid' \
272            or self.context.r_company != u'interswitch':
273            self.flash(_('This ticket has not been paid.'), type='danger')
274            return
275        applicant = self.context.__parent__
276        success, msg, log = query_interswitch(
277            self.context, self.product_id,
278            self.gateway_host, self.gateway_url,
279            self.https, self.mac, True)
280        applicant.writeLogMessage(self, log)
281        if not success:
282            self.flash(msg, type='danger')
283            return
284        self.flash(msg)
285        return
286
287    def render(self):
288        self.redirect(self.url(self.context, '@@index'))
289        return
290
291# Forwarding pages
292
293class InterswitchPageStudent(KofaPage):
294    """ View which sends a POST request to the Interswitch
295    CollegePAY payment gateway.
296    """
297    grok.context(INigeriaOnlinePayment)
298    grok.name('goto_interswitch')
299    grok.template('student_goto_interswitch')
300    grok.require('waeup.payStudent')
301    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
302    submit_button = _('Submit')
303
304    action = None
305    site_name = None
306    currency = None
307    pay_item_id = None
308    product_id = None
309    xml_data = None
310    hashvalue = None
311
312    def init_update(self):
313        if self.context.p_state == 'paid':
314            return _("Payment ticket can't be re-sent to CollegePAY.")
315        now = datetime.utcnow()
316        if self.context.creation_date.tzinfo is not None:
317            # That's bad. Please store timezone-naive datetimes only!
318            now = self.context.creation_date.tzinfo.localize(now)
319        time_delta = now - self.context.creation_date
320        if time_delta.days > 7:
321            return _("This payment ticket is too old. Please create a new ticket.")
322        student = self.context.student
323        certificate = getattr(student['studycourse'],'certificate',None)
324        if certificate is None:
325            return _("Study course data are incomplete.")
326        kofa_utils = getUtility(IKofaUtils)
327        student_utils = getUtility(IStudentsUtils)
328        if student_utils.samePaymentMade(student, self.context.p_category,
329            self.context.p_item, self.context.p_session):
330            return _("This type of payment has already been made.")
331        self.amount_auth = int(100 * self.context.amount_auth)
332        xmldict = {}
333        if certificate is not None:
334            xmldict['department'] = certificate.__parent__.__parent__.code
335            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
336        else:
337            xmldict['department'] = None
338            xmldict['faculty'] = None
339        self.category = self.context.category
340        tz = kofa_utils.tzinfo
341        self.local_date_time = to_timezone(
342            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
343        self.site_redirect_url = self.url(self.context, 'request_webservice')
344        self.student = student
345        self.xmldict = xmldict
346        return
347
348    def update(self):
349        if not module_activated(self.context.student.current_session):
350            return
351        error = self.init_update()
352        if error:
353            self.flash(error, type='danger')
354            self.redirect(self.url(self.context, '@@index'))
355        return
356
357class InterswitchPageApplicant(KofaPage):
358    """ View which sends a POST request to the Interswitch
359    CollegePAY payment gateway.
360    """
361    grok.context(INigeriaApplicantOnlinePayment)
362    grok.require('waeup.payApplicant')
363    grok.template('applicant_goto_interswitch')
364    grok.name('goto_interswitch')
365    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
366    submit_button = _('Submit')
367    hashvalue = None
368
369    action = None
370    site_name = None
371    currency = None
372    pay_item_id = None
373    product_id = None
374    xml_data = None
375
376    def init_update(self):
377        if self.context.p_state != 'unpaid':
378            return _("Payment ticket can't be re-sent to CollegePAY.")
379        if self.context.__parent__.__parent__.expired \
380            and self.context.__parent__.__parent__.strict_deadline:
381            return _("Payment ticket can't be send to CollegePAY. "
382                     "Application period has expired.")
383        tz = getUtility(IKofaUtils).tzinfo
384        time_delta = datetime.utcnow() - self.context.creation_date
385        if time_delta.days > 7:
386            return _("This payment ticket is too old. Please create a new ticket.")
387        self.applicant = self.context.__parent__
388        self.amount_auth = int(100 * self.context.amount_auth)
389        self.category = self.context.category
390        tz = getUtility(IKofaUtils).tzinfo
391        self.local_date_time = to_timezone(
392            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
393        self.site_redirect_url = self.url(self.context, 'request_webservice')
394        return
395
396    def update(self):
397        if not module_activated(self.context.__parent__.__parent__.year):
398            return
399        error = self.init_update()
400        if error:
401            self.flash(error, type='danger')
402            self.redirect(self.url(self.context, '@@index'))
403        return
Note: See TracBrowser for help on using the repository browser.