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

Last change on this file since 13579 was 13579, checked in by Henrik Bettermann, 9 years ago

Applicants and students must be treated differently.

  • Property svn:keywords set to Id
File size: 10.2 KB
RevLine 
[9781]1## $Id: browser.py 13579 2016-01-10 17:21:58Z 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
36class InterswitchActionButtonStudent(ManageActionButton):
37    grok.order(1)
38    grok.context(INigeriaOnlinePayment)
39    grok.view(OPDPStudent)
40    grok.require('waeup.payStudent')
41    icon = 'actionicon_pay.png'
42    text = _('CollegePAY')
43    target = 'goto_interswitch'
44
45    @property
46    def target_url(self):
47        if self.context.p_state != 'unpaid':
48            return ''
49        return self.view.url(self.view.context, self.target)
50
51class InterswitchActionButtonApplicant(InterswitchActionButtonStudent):
52    grok.view(OPDPApplicant)
53    grok.require('waeup.payApplicant')
54
55class InterswitchRequestWebserviceActionButtonStudent(ManageActionButton):
56    grok.order(2)
57    grok.context(INigeriaOnlinePayment)
58    grok.view(OPDPStudent)
59    grok.require('waeup.payStudent')
60    icon = 'actionicon_call.png'
61    text = _('Requery CollegePAY')
62    target = 'request_webservice'
63
64    @property
65    def target_url(self):
[13578]66        if self.context.p_state == 'waived':
[9781]67            return ''
[13578]68        if self.context.p_state == 'paid' \
69            and not checkPermission('waeup.manageStudent', self.context):
70            return ''
[9781]71        return self.view.url(self.view.context, self.target)
72
73class InterswitchRequestWebserviceActionButtonApplicant(
74    InterswitchRequestWebserviceActionButtonStudent):
75    grok.view(OPDPApplicant)
76    grok.require('waeup.payApplicant')
77
[13579]78    @property
79    def target_url(self):
80        if self.context.p_state == 'paid' \
81            and not checkPermission('waeup.manageApplication', self.context):
82            return ''
83        return self.view.url(self.view.context, self.target)
84
[9781]85class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View):
86    """ Request webservice view for the CollegePAY gateway
87    """
88    grok.context(INigeriaStudentOnlinePayment)
89    grok.name('request_webservice')
90    grok.require('waeup.payStudent')
91
92    product_id = None
93    gateway_host = None
94    gateway_url = None
[13044]95    https = True
[13387]96    mac = None
[9781]97
98    def update(self):
[13578]99        if self.context.p_state == 'waived':
100            self.flash(_('This ticket cannot be requeried.'), type='warning')
101            return
102        if self.context.p_state == 'paid' \
103            and not checkPermission('waeup.manageStudent', self.context):
[13579]104            self.flash(_('This ticket has already been paid.'), type='danger')
[9781]105            return
106        student = self.context.student
107        success, msg, log = query_interswitch(
[11915]108            self.context, self.product_id,
109            self.gateway_host, self.gateway_url,
[13387]110            self.https, self.mac)
[9781]111        student.writeLogMessage(self, log)
112        if not success:
[11579]113            self.flash(msg, type='danger')
[9781]114            return
115        write_payments_log(student.student_id, self.context)
[11581]116        flashtype, msg, log = self.context.doAfterStudentPayment()
[9781]117        if log is not None:
118            student.writeLogMessage(self, log)
[11581]119        self.flash(msg, type=flashtype)
[9781]120        return
121
122    def render(self):
123        self.redirect(self.url(self.context, '@@index'))
124        return
125
126class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View):
127    """ Request webservice view for the CollegePAY gateway
128    """
129    grok.context(INigeriaApplicantOnlinePayment)
130    grok.name('request_webservice')
131    grok.require('waeup.payApplicant')
132
133    product_id = None
134    gateway_host = None
135    gateway_url = None
[13044]136    https = True
[13387]137    mac = None
[9781]138
139    def update(self):
[13579]140        if self.context.p_state == 'paid' \
141            and not checkPermission('waeup.manageApplication', self.context):
[11642]142            self.flash(_('This ticket has already been paid.'), type='danger')
[9781]143            return
144        applicant = self.context.__parent__
145        success, msg, log = query_interswitch(
[11915]146            self.context, self.product_id,
147            self.gateway_host, self.gateway_url,
[13387]148            self.https, self.mac)
[9781]149        applicant.writeLogMessage(self, log)
150        if not success:
[11579]151            self.flash(msg, type='danger')
[9781]152            return
153        write_payments_log(applicant.applicant_id, self.context)
[11581]154        flashtype, msg, log = self.context.doAfterApplicantPayment()
[9781]155        if log is not None:
156            applicant.writeLogMessage(self, log)
[11581]157        self.flash(msg, type=flashtype)
[9781]158        return
159
160    def render(self):
161        self.redirect(self.url(self.context, '@@index'))
162        return
[11630]163
164class InterswitchPageStudent(KofaPage):
165    """ View which sends a POST request to the Interswitch
166    CollegePAY payment gateway.
167    """
168    grok.context(INigeriaOnlinePayment)
169    grok.name('goto_interswitch')
170    grok.template('student_goto_interswitch')
171    grok.require('waeup.payStudent')
172    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
173    submit_button = _('Submit')
174
[11639]175    action = None
176    site_name = None
177    currency = None
178    pay_item_id = None
179    product_id = None
180    xml_data = None
[12477]181    hashvalue = None
[11639]182
[11642]183    def init_update(self):
[11630]184        if self.context.p_state == 'paid':
[11642]185            return _("Payment ticket can't be re-sent to CollegePAY.")
[13478]186        now = datetime.utcnow()
187        if self.context.creation_date.tzinfo is not None:
188            # That's bad. Please store timezone-naive datetimes only!
189            now = self.context.creation_date.tzinfo.localize(now)
190        time_delta = now - self.context.creation_date
[13015]191        if time_delta.days > 7:
[12973]192            return _("This payment ticket is too old. Please create a new ticket.")
[11642]193        student = self.context.student
[11630]194        certificate = getattr(student['studycourse'],'certificate',None)
195        if certificate is None:
[11642]196            return _("Study course data are incomplete.")
[11639]197        kofa_utils = getUtility(IKofaUtils)
[11642]198        student_utils = getUtility(IStudentsUtils)
199        if student_utils.samePaymentMade(student, self.context.p_category,
200            self.context.p_item, self.context.p_session):
201            return _("This type of payment has already been made.")
[11767]202        self.amount_auth = int(100 * self.context.amount_auth)
[11630]203        xmldict = {}
204        if certificate is not None:
205            xmldict['department'] = certificate.__parent__.__parent__.code
206            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
207        else:
208            xmldict['department'] = None
209            xmldict['faculty'] = None
[12509]210        self.category = self.context.category
[11639]211        tz = kofa_utils.tzinfo
[11630]212        self.local_date_time = to_timezone(
213            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
214        self.site_redirect_url = self.url(self.context, 'request_webservice')
[11642]215        self.student = student
216        self.xmldict = xmldict
217        return
[11630]218
[11642]219    def update(self):
220        error = self.init_update()
221        if error:
222            self.flash(error, type='danger')
223            self.redirect(self.url(self.context, '@@index'))
224        return
225
[11630]226class InterswitchPageApplicant(KofaPage):
227    """ View which sends a POST request to the Interswitch
228    CollegePAY payment gateway.
229    """
230    grok.context(INigeriaApplicantOnlinePayment)
231    grok.require('waeup.payApplicant')
232    grok.template('applicant_goto_interswitch')
233    grok.name('goto_interswitch')
234    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
235    submit_button = _('Submit')
[12477]236    hashvalue = None
[11630]237
[11639]238    action = None
239    site_name = None
240    currency = None
241    pay_item_id = None
242    product_id = None
243    xml_data = None
244
[11642]245    def init_update(self):
[11630]246        if self.context.p_state != 'unpaid':
[11642]247            return _("Payment ticket can't be re-sent to CollegePAY.")
[11630]248        if self.context.__parent__.__parent__.expired \
249            and self.context.__parent__.__parent__.strict_deadline:
[11642]250            return _("Payment ticket can't be send to CollegePAY. "
251                     "Application period has expired.")
[12973]252        tz = getUtility(IKofaUtils).tzinfo
253        time_delta = datetime.utcnow() - self.context.creation_date
[13015]254        if time_delta.days > 7:
[12973]255            return _("This payment ticket is too old. Please create a new ticket.")
[11630]256        self.applicant = self.context.__parent__
[11767]257        self.amount_auth = int(100 * self.context.amount_auth)
[12509]258        self.category = self.context.category
[11630]259        tz = getUtility(IKofaUtils).tzinfo
260        self.local_date_time = to_timezone(
261            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
262        self.site_redirect_url = self.url(self.context, 'request_webservice')
[11642]263        return
264
265    def update(self):
266        error = self.init_update()
267        if error:
268            self.flash(error, type='danger')
269            self.redirect(self.url(self.context, '@@index'))
[13478]270        return
Note: See TracBrowser for help on using the repository browser.