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

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

Provide self.category for backwards compatibility.

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