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

Last change on this file since 15402 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
RevLine 
[9781]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
[12973]19from datetime import datetime, timedelta
[11630]20from zope.component import getUtility
[13578]21from zope.security import checkPermission
[11630]22from waeup.kofa.interfaces import IKofaUtils
23from waeup.kofa.utils.helpers import to_timezone
24from waeup.kofa.browser.layout import UtilityView, KofaPage
[9781]25from waeup.kofa.browser.viewlets import ManageActionButton
[11642]26from waeup.kofa.students.interfaces import IStudentsUtils
[9781]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
[13581]36# Buttons
37
[14755]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
[9781]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):
[14755]56        if not module_activated(self.context.student.current_session):
57            return ''
[9781]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
[14755]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
[9781]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):
[14755]85        if not module_activated(self.context.student.current_session):
86            return ''
[13580]87        if self.context.p_state in ('paid', 'waived'):
[9781]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
[14755]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
[13581]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):
[14755]115        if not module_activated(self.context.student.current_session):
116            return ''
[13585]117        if self.context.p_state != 'paid' \
118            or self.context.r_company != u'interswitch':
[13581]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
[14755]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)
[13581]135
136# Webservice request views
137
[9781]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
[13044]148    https = True
[13387]149    mac = None
[9781]150
151    def update(self):
[14755]152        if not module_activated(self.context.student.current_session):
153            return
[13580]154        if self.context.p_state in ('paid', 'waived'):
[13579]155            self.flash(_('This ticket has already been paid.'), type='danger')
[9781]156            return
157        student = self.context.student
158        success, msg, log = query_interswitch(
[11915]159            self.context, self.product_id,
160            self.gateway_host, self.gateway_url,
[13584]161            self.https, self.mac, False)
[9781]162        student.writeLogMessage(self, log)
163        if not success:
[11579]164            self.flash(msg, type='danger')
[9781]165            return
166        write_payments_log(student.student_id, self.context)
[11581]167        flashtype, msg, log = self.context.doAfterStudentPayment()
[9781]168        if log is not None:
169            student.writeLogMessage(self, log)
[11581]170        self.flash(msg, type=flashtype)
[9781]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
[13044]187    https = True
[13387]188    mac = None
[9781]189
190    def update(self):
[14755]191        if not module_activated(self.context.__parent__.__parent__.year):
192            return
[13580]193        if self.context.p_state == 'paid':
[11642]194            self.flash(_('This ticket has already been paid.'), type='danger')
[9781]195            return
196        applicant = self.context.__parent__
197        success, msg, log = query_interswitch(
[11915]198            self.context, self.product_id,
199            self.gateway_host, self.gateway_url,
[13584]200            self.https, self.mac, False)
[9781]201        applicant.writeLogMessage(self, log)
202        if not success:
[11579]203            self.flash(msg, type='danger')
[9781]204            return
205        write_payments_log(applicant.applicant_id, self.context)
[11581]206        flashtype, msg, log = self.context.doAfterApplicantPayment()
[9781]207        if log is not None:
208            applicant.writeLogMessage(self, log)
[11581]209        self.flash(msg, type=flashtype)
[9781]210        return
211
212    def render(self):
[15394]213        if self.context.p_state == 'paid':
214            self.redirect(self.url(self.context.__parent__))
215            return
216        self.redirect(self.url(self.context))
[9781]217        return
[11630]218
[13581]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):
[14755]233        if not module_activated(self.context.student.current_session):
234            return
[13585]235        if self.context.p_state  != 'paid' \
236            or self.context.r_company != u'interswitch':
[13581]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,
[13584]243            self.https, self.mac, True)
[13581]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):
[14755]269        if not module_activated(self.context.__parent__.__parent__.year):
270            return
[13585]271        if self.context.p_state != 'paid' \
272            or self.context.r_company != u'interswitch':
[13581]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,
[13584]279            self.https, self.mac, True)
[13581]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
[11630]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
[11639]304    action = None
305    site_name = None
306    currency = None
307    pay_item_id = None
308    product_id = None
309    xml_data = None
[12477]310    hashvalue = None
[11639]311
[11642]312    def init_update(self):
[11630]313        if self.context.p_state == 'paid':
[11642]314            return _("Payment ticket can't be re-sent to CollegePAY.")
[13478]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
[13015]320        if time_delta.days > 7:
[12973]321            return _("This payment ticket is too old. Please create a new ticket.")
[11642]322        student = self.context.student
[11630]323        certificate = getattr(student['studycourse'],'certificate',None)
324        if certificate is None:
[11642]325            return _("Study course data are incomplete.")
[11639]326        kofa_utils = getUtility(IKofaUtils)
[11642]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.")
[11767]331        self.amount_auth = int(100 * self.context.amount_auth)
[11630]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
[12509]339        self.category = self.context.category
[11639]340        tz = kofa_utils.tzinfo
[11630]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')
[11642]344        self.student = student
345        self.xmldict = xmldict
346        return
[11630]347
[11642]348    def update(self):
[14755]349        if not module_activated(self.context.student.current_session):
350            return
[11642]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
[11630]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')
[12477]367    hashvalue = None
[11630]368
[11639]369    action = None
370    site_name = None
371    currency = None
372    pay_item_id = None
373    product_id = None
374    xml_data = None
375
[11642]376    def init_update(self):
[11630]377        if self.context.p_state != 'unpaid':
[11642]378            return _("Payment ticket can't be re-sent to CollegePAY.")
[11630]379        if self.context.__parent__.__parent__.expired \
380            and self.context.__parent__.__parent__.strict_deadline:
[11642]381            return _("Payment ticket can't be send to CollegePAY. "
382                     "Application period has expired.")
[12973]383        tz = getUtility(IKofaUtils).tzinfo
384        time_delta = datetime.utcnow() - self.context.creation_date
[13015]385        if time_delta.days > 7:
[12973]386            return _("This payment ticket is too old. Please create a new ticket.")
[11630]387        self.applicant = self.context.__parent__
[11767]388        self.amount_auth = int(100 * self.context.amount_auth)
[12509]389        self.category = self.context.category
[11630]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')
[11642]394        return
395
396    def update(self):
[14755]397        if not module_activated(self.context.__parent__.__parent__.year):
398            return
[11642]399        error = self.init_update()
400        if error:
401            self.flash(error, type='danger')
402            self.redirect(self.url(self.context, '@@index'))
[13478]403        return
Note: See TracBrowser for help on using the repository browser.