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

Last change on this file since 11659 was 11642, checked in by Henrik Bettermann, 11 years ago

Define method which initiates update method.

  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1## $Id: browser.py 11642 2014-05-13 21:23:59Z 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
84    def update(self):
85        if self.context.p_state == 'paid':
86            self.flash(_('This ticket has already been paid.'), type='warning')
87            return
88        student = self.context.student
89        success, msg, log = query_interswitch(
90            self.context, self.product_id, self.gateway_host, self.gateway_url)
91        student.writeLogMessage(self, log)
92        if not success:
93            self.flash(msg, type='danger')
94            return
95        write_payments_log(student.student_id, self.context)
96        flashtype, msg, log = self.context.doAfterStudentPayment()
97        if log is not None:
98            student.writeLogMessage(self, log)
99        self.flash(msg, type=flashtype)
100        return
101
102    def render(self):
103        self.redirect(self.url(self.context, '@@index'))
104        return
105
106class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View):
107    """ Request webservice view for the CollegePAY gateway
108    """
109    grok.context(INigeriaApplicantOnlinePayment)
110    grok.name('request_webservice')
111    grok.require('waeup.payApplicant')
112
113    product_id = None
114    gateway_host = None
115    gateway_url = None
116
117    def update(self):
118        if self.context.p_state == 'paid':
119            self.flash(_('This ticket has already been paid.'), type='danger')
120            return
121        applicant = self.context.__parent__
122        success, msg, log = query_interswitch(
123            self.context, self.product_id, self.gateway_host, self.gateway_url)
124        applicant.writeLogMessage(self, log)
125        if not success:
126            self.flash(msg, type='danger')
127            return
128        write_payments_log(applicant.applicant_id, self.context)
129        flashtype, msg, log = self.context.doAfterApplicantPayment()
130        if log is not None:
131            applicant.writeLogMessage(self, log)
132        self.flash(msg, type=flashtype)
133        return
134
135    def render(self):
136        self.redirect(self.url(self.context, '@@index'))
137        return
138
139class InterswitchPageStudent(KofaPage):
140    """ View which sends a POST request to the Interswitch
141    CollegePAY payment gateway.
142    """
143    grok.context(INigeriaOnlinePayment)
144    grok.name('goto_interswitch')
145    grok.template('student_goto_interswitch')
146    grok.require('waeup.payStudent')
147    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
148    submit_button = _('Submit')
149
150    action = None
151    site_name = None
152    currency = None
153    pay_item_id = None
154    product_id = None
155    xml_data = None
156
157    def init_update(self):
158        if self.context.p_state == 'paid':
159            return _("Payment ticket can't be re-sent to CollegePAY.")
160        student = self.context.student
161        certificate = getattr(student['studycourse'],'certificate',None)
162        if certificate is None:
163            return _("Study course data are incomplete.")
164        kofa_utils = getUtility(IKofaUtils)
165        student_utils = getUtility(IStudentsUtils)
166        if student_utils.samePaymentMade(student, self.context.p_category,
167            self.context.p_item, self.context.p_session):
168            return _("This type of payment has already been made.")
169        self.amount_auth = 100 * self.context.amount_auth
170        xmldict = {}
171        if certificate is not None:
172            xmldict['department'] = certificate.__parent__.__parent__.code
173            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
174        else:
175            xmldict['department'] = None
176            xmldict['faculty'] = None
177        self.category = kofa_utils.PAYMENT_CATEGORIES[self.context.p_category]
178        tz = kofa_utils.tzinfo
179        self.local_date_time = to_timezone(
180            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
181        self.site_redirect_url = self.url(self.context, 'request_webservice')
182        self.student = student
183        self.xmldict = xmldict
184        return
185
186    def update(self):
187        error = self.init_update()
188        if error:
189            self.flash(error, type='danger')
190            self.redirect(self.url(self.context, '@@index'))
191        return
192
193class InterswitchPageApplicant(KofaPage):
194    """ View which sends a POST request to the Interswitch
195    CollegePAY payment gateway.
196    """
197    grok.context(INigeriaApplicantOnlinePayment)
198    grok.require('waeup.payApplicant')
199    grok.template('applicant_goto_interswitch')
200    grok.name('goto_interswitch')
201    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
202    submit_button = _('Submit')
203
204    action = None
205    site_name = None
206    currency = None
207    pay_item_id = None
208    product_id = None
209    xml_data = None
210
211    def init_update(self):
212        if self.context.p_state != 'unpaid':
213            return _("Payment ticket can't be re-sent to CollegePAY.")
214        if self.context.__parent__.__parent__.expired \
215            and self.context.__parent__.__parent__.strict_deadline:
216            return _("Payment ticket can't be send to CollegePAY. "
217                     "Application period has expired.")
218        self.applicant = self.context.__parent__
219        self.amount_auth = 100 * self.context.amount_auth
220        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
221        tz = getUtility(IKofaUtils).tzinfo
222        self.local_date_time = to_timezone(
223            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
224        self.site_redirect_url = self.url(self.context, 'request_webservice')
225        return
226
227    def update(self):
228        error = self.init_update()
229        if error:
230            self.flash(error, type='danger')
231            self.redirect(self.url(self.context, '@@index'))
232        return
Note: See TracBrowser for help on using the repository browser.