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

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

Be prepared for https webservice requests. #2

  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1## $Id: browser.py 11915 2014-10-30 16:54:24Z 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
163    def init_update(self):
164        if self.context.p_state == 'paid':
165            return _("Payment ticket can't be re-sent to CollegePAY.")
166        student = self.context.student
167        certificate = getattr(student['studycourse'],'certificate',None)
168        if certificate is None:
169            return _("Study course data are incomplete.")
170        kofa_utils = getUtility(IKofaUtils)
171        student_utils = getUtility(IStudentsUtils)
172        if student_utils.samePaymentMade(student, self.context.p_category,
173            self.context.p_item, self.context.p_session):
174            return _("This type of payment has already been made.")
175        self.amount_auth = int(100 * self.context.amount_auth)
176        xmldict = {}
177        if certificate is not None:
178            xmldict['department'] = certificate.__parent__.__parent__.code
179            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
180        else:
181            xmldict['department'] = None
182            xmldict['faculty'] = None
183        self.category = kofa_utils.PAYMENT_CATEGORIES[self.context.p_category]
184        tz = kofa_utils.tzinfo
185        self.local_date_time = to_timezone(
186            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
187        self.site_redirect_url = self.url(self.context, 'request_webservice')
188        self.student = student
189        self.xmldict = xmldict
190        return
191
192    def update(self):
193        error = self.init_update()
194        if error:
195            self.flash(error, type='danger')
196            self.redirect(self.url(self.context, '@@index'))
197        return
198
199class InterswitchPageApplicant(KofaPage):
200    """ View which sends a POST request to the Interswitch
201    CollegePAY payment gateway.
202    """
203    grok.context(INigeriaApplicantOnlinePayment)
204    grok.require('waeup.payApplicant')
205    grok.template('applicant_goto_interswitch')
206    grok.name('goto_interswitch')
207    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
208    submit_button = _('Submit')
209
210    action = None
211    site_name = None
212    currency = None
213    pay_item_id = None
214    product_id = None
215    xml_data = None
216
217    def init_update(self):
218        if self.context.p_state != 'unpaid':
219            return _("Payment ticket can't be re-sent to CollegePAY.")
220        if self.context.__parent__.__parent__.expired \
221            and self.context.__parent__.__parent__.strict_deadline:
222            return _("Payment ticket can't be send to CollegePAY. "
223                     "Application period has expired.")
224        self.applicant = self.context.__parent__
225        self.amount_auth = int(100 * self.context.amount_auth)
226        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
227        tz = getUtility(IKofaUtils).tzinfo
228        self.local_date_time = to_timezone(
229            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
230        self.site_redirect_url = self.url(self.context, 'request_webservice')
231        return
232
233    def update(self):
234        error = self.init_update()
235        if error:
236            self.flash(error, type='danger')
237            self.redirect(self.url(self.context, '@@index'))
238        return
Note: See TracBrowser for help on using the repository browser.