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

Last change on this file since 12080 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
RevLine 
[9781]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
[11630]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
[9781]23from waeup.kofa.browser.viewlets import ManageActionButton
[11642]24from waeup.kofa.students.interfaces import IStudentsUtils
[9781]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
[11915]83    https = False
[9781]84
85    def update(self):
86        if self.context.p_state == 'paid':
[11579]87            self.flash(_('This ticket has already been paid.'), type='warning')
[9781]88            return
89        student = self.context.student
90        success, msg, log = query_interswitch(
[11915]91            self.context, self.product_id,
92            self.gateway_host, self.gateway_url,
93            self.https)
[9781]94        student.writeLogMessage(self, log)
95        if not success:
[11579]96            self.flash(msg, type='danger')
[9781]97            return
98        write_payments_log(student.student_id, self.context)
[11581]99        flashtype, msg, log = self.context.doAfterStudentPayment()
[9781]100        if log is not None:
101            student.writeLogMessage(self, log)
[11581]102        self.flash(msg, type=flashtype)
[9781]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
[11915]119    https = False
[9781]120
121    def update(self):
122        if self.context.p_state == 'paid':
[11642]123            self.flash(_('This ticket has already been paid.'), type='danger')
[9781]124            return
125        applicant = self.context.__parent__
126        success, msg, log = query_interswitch(
[11915]127            self.context, self.product_id,
128            self.gateway_host, self.gateway_url,
129            self.https)
[9781]130        applicant.writeLogMessage(self, log)
131        if not success:
[11579]132            self.flash(msg, type='danger')
[9781]133            return
134        write_payments_log(applicant.applicant_id, self.context)
[11581]135        flashtype, msg, log = self.context.doAfterApplicantPayment()
[9781]136        if log is not None:
137            applicant.writeLogMessage(self, log)
[11581]138        self.flash(msg, type=flashtype)
[9781]139        return
140
141    def render(self):
142        self.redirect(self.url(self.context, '@@index'))
143        return
[11630]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
[11639]156    action = None
157    site_name = None
158    currency = None
159    pay_item_id = None
160    product_id = None
161    xml_data = None
162
[11642]163    def init_update(self):
[11630]164        if self.context.p_state == 'paid':
[11642]165            return _("Payment ticket can't be re-sent to CollegePAY.")
166        student = self.context.student
[11630]167        certificate = getattr(student['studycourse'],'certificate',None)
168        if certificate is None:
[11642]169            return _("Study course data are incomplete.")
[11639]170        kofa_utils = getUtility(IKofaUtils)
[11642]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.")
[11767]175        self.amount_auth = int(100 * self.context.amount_auth)
[11630]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
[11639]183        self.category = kofa_utils.PAYMENT_CATEGORIES[self.context.p_category]
184        tz = kofa_utils.tzinfo
[11630]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')
[11642]188        self.student = student
189        self.xmldict = xmldict
190        return
[11630]191
[11642]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
[11630]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
[11639]210    action = None
211    site_name = None
212    currency = None
213    pay_item_id = None
214    product_id = None
215    xml_data = None
216
[11642]217    def init_update(self):
[11630]218        if self.context.p_state != 'unpaid':
[11642]219            return _("Payment ticket can't be re-sent to CollegePAY.")
[11630]220        if self.context.__parent__.__parent__.expired \
221            and self.context.__parent__.__parent__.strict_deadline:
[11642]222            return _("Payment ticket can't be send to CollegePAY. "
223                     "Application period has expired.")
[11630]224        self.applicant = self.context.__parent__
[11767]225        self.amount_auth = int(100 * self.context.amount_auth)
[11630]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')
[11642]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'))
[11630]238        return
Note: See TracBrowser for help on using the repository browser.