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
Line 
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
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
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):
66        if self.context.p_state == 'waived':
67            return ''
68        if self.context.p_state == 'paid' \
69            and not checkPermission('waeup.manageStudent', self.context):
70            return ''
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
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
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
95    https = True
96    mac = None
97
98    def update(self):
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):
104            self.flash(_('This ticket has already been paid.'), type='danger')
105            return
106        student = self.context.student
107        success, msg, log = query_interswitch(
108            self.context, self.product_id,
109            self.gateway_host, self.gateway_url,
110            self.https, self.mac)
111        student.writeLogMessage(self, log)
112        if not success:
113            self.flash(msg, type='danger')
114            return
115        write_payments_log(student.student_id, self.context)
116        flashtype, msg, log = self.context.doAfterStudentPayment()
117        if log is not None:
118            student.writeLogMessage(self, log)
119        self.flash(msg, type=flashtype)
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
136    https = True
137    mac = None
138
139    def update(self):
140        if self.context.p_state == 'paid' \
141            and not checkPermission('waeup.manageApplication', self.context):
142            self.flash(_('This ticket has already been paid.'), type='danger')
143            return
144        applicant = self.context.__parent__
145        success, msg, log = query_interswitch(
146            self.context, self.product_id,
147            self.gateway_host, self.gateway_url,
148            self.https, self.mac)
149        applicant.writeLogMessage(self, log)
150        if not success:
151            self.flash(msg, type='danger')
152            return
153        write_payments_log(applicant.applicant_id, self.context)
154        flashtype, msg, log = self.context.doAfterApplicantPayment()
155        if log is not None:
156            applicant.writeLogMessage(self, log)
157        self.flash(msg, type=flashtype)
158        return
159
160    def render(self):
161        self.redirect(self.url(self.context, '@@index'))
162        return
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
175    action = None
176    site_name = None
177    currency = None
178    pay_item_id = None
179    product_id = None
180    xml_data = None
181    hashvalue = None
182
183    def init_update(self):
184        if self.context.p_state == 'paid':
185            return _("Payment ticket can't be re-sent to CollegePAY.")
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
191        if time_delta.days > 7:
192            return _("This payment ticket is too old. Please create a new ticket.")
193        student = self.context.student
194        certificate = getattr(student['studycourse'],'certificate',None)
195        if certificate is None:
196            return _("Study course data are incomplete.")
197        kofa_utils = getUtility(IKofaUtils)
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.")
202        self.amount_auth = int(100 * self.context.amount_auth)
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
210        self.category = self.context.category
211        tz = kofa_utils.tzinfo
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')
215        self.student = student
216        self.xmldict = xmldict
217        return
218
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
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')
236    hashvalue = None
237
238    action = None
239    site_name = None
240    currency = None
241    pay_item_id = None
242    product_id = None
243    xml_data = None
244
245    def init_update(self):
246        if self.context.p_state != 'unpaid':
247            return _("Payment ticket can't be re-sent to CollegePAY.")
248        if self.context.__parent__.__parent__.expired \
249            and self.context.__parent__.__parent__.strict_deadline:
250            return _("Payment ticket can't be send to CollegePAY. "
251                     "Application period has expired.")
252        tz = getUtility(IKofaUtils).tzinfo
253        time_delta = datetime.utcnow() - self.context.creation_date
254        if time_delta.days > 7:
255            return _("This payment ticket is too old. Please create a new ticket.")
256        self.applicant = self.context.__parent__
257        self.amount_auth = int(100 * self.context.amount_auth)
258        self.category = self.context.category
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')
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'))
270        return
Note: See TracBrowser for help on using the repository browser.