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

Last change on this file since 13007 was 13007, checked in by Henrik Bettermann, 10 years ago

Non-imported payment tickets must not be submitted to Interswitch if they
are older than 1 day

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