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

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

Add necessary components for browser tests (not used in custom packages).

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