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

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

Catch TypeError?.

  • Property svn:keywords set to Id
File size: 9.6 KB
RevLine 
[9781]1## $Id: browser.py 13472 2015-11-18 07:34:56Z 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
21from waeup.kofa.interfaces import IKofaUtils
22from waeup.kofa.utils.helpers import to_timezone
23from waeup.kofa.browser.layout import UtilityView, KofaPage
[9781]24from waeup.kofa.browser.viewlets import ManageActionButton
[11642]25from waeup.kofa.students.interfaces import IStudentsUtils
[9781]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):
[13197]65        if self.context.p_state in ('paid', 'waived'):
[9781]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
[13044]84    https = True
[13387]85    mac = None
[9781]86
87    def update(self):
[13197]88        if self.context.p_state in ('paid', 'waived'):
[11579]89            self.flash(_('This ticket has already been paid.'), type='warning')
[9781]90            return
91        student = self.context.student
92        success, msg, log = query_interswitch(
[11915]93            self.context, self.product_id,
94            self.gateway_host, self.gateway_url,
[13387]95            self.https, self.mac)
[9781]96        student.writeLogMessage(self, log)
97        if not success:
[11579]98            self.flash(msg, type='danger')
[9781]99            return
100        write_payments_log(student.student_id, self.context)
[11581]101        flashtype, msg, log = self.context.doAfterStudentPayment()
[9781]102        if log is not None:
103            student.writeLogMessage(self, log)
[11581]104        self.flash(msg, type=flashtype)
[9781]105        return
106
107    def render(self):
108        self.redirect(self.url(self.context, '@@index'))
109        return
110
111class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View):
112    """ Request webservice view for the CollegePAY gateway
113    """
114    grok.context(INigeriaApplicantOnlinePayment)
115    grok.name('request_webservice')
116    grok.require('waeup.payApplicant')
117
118    product_id = None
119    gateway_host = None
120    gateway_url = None
[13044]121    https = True
[13387]122    mac = None
[9781]123
124    def update(self):
125        if self.context.p_state == 'paid':
[11642]126            self.flash(_('This ticket has already been paid.'), type='danger')
[9781]127            return
128        applicant = self.context.__parent__
129        success, msg, log = query_interswitch(
[11915]130            self.context, self.product_id,
131            self.gateway_host, self.gateway_url,
[13387]132            self.https, self.mac)
[9781]133        applicant.writeLogMessage(self, log)
134        if not success:
[11579]135            self.flash(msg, type='danger')
[9781]136            return
137        write_payments_log(applicant.applicant_id, self.context)
[11581]138        flashtype, msg, log = self.context.doAfterApplicantPayment()
[9781]139        if log is not None:
140            applicant.writeLogMessage(self, log)
[11581]141        self.flash(msg, type=flashtype)
[9781]142        return
143
144    def render(self):
145        self.redirect(self.url(self.context, '@@index'))
146        return
[11630]147
148class InterswitchPageStudent(KofaPage):
149    """ View which sends a POST request to the Interswitch
150    CollegePAY payment gateway.
151    """
152    grok.context(INigeriaOnlinePayment)
153    grok.name('goto_interswitch')
154    grok.template('student_goto_interswitch')
155    grok.require('waeup.payStudent')
156    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
157    submit_button = _('Submit')
158
[11639]159    action = None
160    site_name = None
161    currency = None
162    pay_item_id = None
163    product_id = None
164    xml_data = None
[12477]165    hashvalue = None
[11639]166
[11642]167    def init_update(self):
[11630]168        if self.context.p_state == 'paid':
[11642]169            return _("Payment ticket can't be re-sent to CollegePAY.")
[12973]170        tz = getUtility(IKofaUtils).tzinfo
[13472]171        try:
172            time_delta = datetime.utcnow() - self.context.creation_date
173        except TypeError:
174            # when importing datetimes we are storing offset-aware datetimes
175            # which causes a TypeError
176            now = datetime.now(tz)
177            time_delta = datetime.now(tz) - self.context.creation_date
[13015]178        if time_delta.days > 7:
[12973]179            return _("This payment ticket is too old. Please create a new ticket.")
[11642]180        student = self.context.student
[11630]181        certificate = getattr(student['studycourse'],'certificate',None)
182        if certificate is None:
[11642]183            return _("Study course data are incomplete.")
[11639]184        kofa_utils = getUtility(IKofaUtils)
[11642]185        student_utils = getUtility(IStudentsUtils)
186        if student_utils.samePaymentMade(student, self.context.p_category,
187            self.context.p_item, self.context.p_session):
188            return _("This type of payment has already been made.")
[11767]189        self.amount_auth = int(100 * self.context.amount_auth)
[11630]190        xmldict = {}
191        if certificate is not None:
192            xmldict['department'] = certificate.__parent__.__parent__.code
193            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
194        else:
195            xmldict['department'] = None
196            xmldict['faculty'] = None
[12509]197        self.category = self.context.category
[11639]198        tz = kofa_utils.tzinfo
[11630]199        self.local_date_time = to_timezone(
200            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
201        self.site_redirect_url = self.url(self.context, 'request_webservice')
[11642]202        self.student = student
203        self.xmldict = xmldict
204        return
[11630]205
[11642]206    def update(self):
207        error = self.init_update()
208        if error:
209            self.flash(error, type='danger')
210            self.redirect(self.url(self.context, '@@index'))
211        return
212
[11630]213class InterswitchPageApplicant(KofaPage):
214    """ View which sends a POST request to the Interswitch
215    CollegePAY payment gateway.
216    """
217    grok.context(INigeriaApplicantOnlinePayment)
218    grok.require('waeup.payApplicant')
219    grok.template('applicant_goto_interswitch')
220    grok.name('goto_interswitch')
221    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
222    submit_button = _('Submit')
[12477]223    hashvalue = None
[11630]224
[11639]225    action = None
226    site_name = None
227    currency = None
228    pay_item_id = None
229    product_id = None
230    xml_data = None
231
[11642]232    def init_update(self):
[11630]233        if self.context.p_state != 'unpaid':
[11642]234            return _("Payment ticket can't be re-sent to CollegePAY.")
[11630]235        if self.context.__parent__.__parent__.expired \
236            and self.context.__parent__.__parent__.strict_deadline:
[11642]237            return _("Payment ticket can't be send to CollegePAY. "
238                     "Application period has expired.")
[12973]239        tz = getUtility(IKofaUtils).tzinfo
240        time_delta = datetime.utcnow() - self.context.creation_date
[13015]241        if time_delta.days > 7:
[12973]242            return _("This payment ticket is too old. Please create a new ticket.")
[11630]243        self.applicant = self.context.__parent__
[11767]244        self.amount_auth = int(100 * self.context.amount_auth)
[12509]245        self.category = self.context.category
[11630]246        tz = getUtility(IKofaUtils).tzinfo
247        self.local_date_time = to_timezone(
248            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
249        self.site_redirect_url = self.url(self.context, 'request_webservice')
[11642]250        return
251
252    def update(self):
253        error = self.init_update()
254        if error:
255            self.flash(error, type='danger')
256            self.redirect(self.url(self.context, '@@index'))
[11630]257        return
Note: See TracBrowser for help on using the repository browser.