source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/etranzact/payoutletbrowser.py @ 17421

Last change on this file since 17421 was 17248, checked in by Henrik Bettermann, 23 months ago

Enable Paypal only for USD payments.
Enable all other gateway services only for NGN payments.

File size: 9.3 KB
Line 
1## $Id: browser.py 15346 2019-03-06 22:19:56Z 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##
18from datetime import datetime
19import httplib
20import urllib
21import urllib2
22import re
23from xml.dom.minidom import parseString
24import grok
25from zope.component import getUtility
26from zope.catalog.interfaces import ICatalog
27from waeup.kofa.interfaces import IUniversity, CLEARED
28from waeup.kofa.payments.interfaces import IPayer
29from waeup.kofa.webservices import PaymentDataWebservice
30from waeup.kofa.browser.layout import KofaPage, UtilityView
31from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
32from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
33from waeup.kofa.students.viewlets import PaymentReceiptActionButton as PRABStudent
34from waeup.kofa.applicants.viewlets import PaymentReceiptActionButton as PRABApplicant
35from kofacustom.nigeria.interswitch.browser import (
36    InterswitchActionButtonStudent,
37    InterswitchRequestWebserviceActionButtonStudent,
38    InterswitchActionButtonApplicant,
39    InterswitchRequestWebserviceActionButtonApplicant)
40from kofacustom.nigeria.interfaces import MessageFactory as _
41from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment
42from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
43from kofacustom.nigeria.etranzact.tests import HOST, TERMINAL_ID, HTTPS, GATEWAY_AMT
44from kofacustom.nigeria.etranzact.helpers import query_payoutlet
45
46grok.templatedir('browser_templates')
47
48def payoutlet_module_activated(session, payment):
49    if payment.p_currency != 'NGN':
50        return False
51    if payment.r_company and payment.r_company != 'etranzact':
52        return False
53    try:
54        return getattr(grok.getSite()['configuration'][str(session)],
55            'etranzact_payoutlet_enabled', False)
56    except KeyError:
57        session = datetime.now().year
58        try:
59            return getattr(grok.getSite()['configuration'][str(session)],
60                'etranzact_payoutlet_enabled', False)
61        except KeyError:
62            return False
63
64class EtranzactEnterPinActionButtonApplicant(APABApplicant):
65    grok.context(INigeriaApplicantOnlinePayment)
66    grok.require('waeup.payApplicant')
67    grok.order(3)
68    icon = 'actionicon_pay.png'
69    text = _('Pay via Etranzact PayOutlet')
70    target = 'enterpin'
71
72    @property
73    def target_url(self):
74        if not payoutlet_module_activated(
75            self.context.__parent__.__parent__.year, self.context):
76            return ''
77        if self.context.p_state in ('paid', 'waived', 'scholarship'):
78            return ''
79        return self.view.url(self.view.context, self.target)
80
81class EtranzactEnterPinActionButtonStudent(APABStudent):
82    grok.context(INigeriaStudentOnlinePayment)
83    grok.require('waeup.payStudent')
84    grok.order(10)
85    icon = 'actionicon_pay.png'
86    text = _('Pay via Etranzact PayOutlet')
87    target = 'enterpin'
88
89    @property
90    def target_url(self):
91        if not payoutlet_module_activated(
92            self.context.student.current_session, self.context):
93            return ''
94        if self.context.p_state in ('paid', 'waived', 'scholarship'):
95            return ''
96        return self.view.url(self.view.context, self.target)
97
98class EtranzactEnterPinPageStudent(KofaPage):
99    """Enter confirmation PIN and submit to `EtranzactQueryHistoryPageStudent`
100    """
101    grok.context(INigeriaStudentOnlinePayment)
102    grok.name('enterpin')
103    grok.template('enterpin')
104    grok.require('waeup.payStudent')
105
106    buttonname = _('Requery now')
107    label = _('Requery Etranzact PayOutlet History')
108    action = 'query_payoutlet_history'
109    placeholder = _('Confirmation Number (PIN)')
110    gateway_amt = GATEWAY_AMT
111
112    def update(self):
113        if not payoutlet_module_activated(
114            self.context.student.current_session, self.context):
115            self.flash(_('Forbidden'), type='danger')
116            self.redirect(self.url(self.context, '@@index'))
117            return
118        # Already now it becomes an Etranzact payment. We set the net amount
119        # and add the gateway amount.
120        if not self.context.r_company:
121            self.context.net_amt = self.context.amount_auth
122            self.context.amount_auth += self.gateway_amt
123            self.context.gateway_amt = self.gateway_amt
124            self.context.r_company = u'etranzact'
125        return
126
127class EtranzactEnterPinPageApplicant(KofaPage):
128    """Enter confirmation PIN and submit to `EtranzactQueryHistoryPageApplicant`
129    """
130    grok.require('waeup.payApplicant')
131    grok.context(INigeriaApplicantOnlinePayment)
132    grok.name('enterpin')
133    grok.template('enterpin')
134
135    buttonname = _('Requery now')
136    label = _('Requery Etranzact PayOutlet History')
137    action = 'query_payoutlet_history'
138    placeholder = _('Confirmation Number (PIN)')
139    gateway_amt = GATEWAY_AMT
140
141    def update(self):
142        if not payoutlet_module_activated(
143            self.context.__parent__.__parent__.year, self.context):
144            self.flash(_('Forbidden'), type='danger')
145            self.redirect(self.url(self.context, '@@index'))
146            return
147        # Already now it becomes an Etranzact payment. We set the net amount
148        # and add the gateway amount.
149        if not self.context.r_company:
150            self.context.net_amt = self.context.amount_auth
151            self.context.amount_auth += self.gateway_amt
152            self.context.gateway_amt = self.gateway_amt
153            self.context.r_company = u'etranzact'
154        return
155
156class PayoutletPaymentReceiptActionButtonApplicant(PRABApplicant):
157
158    grok.view(EtranzactEnterPinPageApplicant)
159
160    @property
161    def target_url(self):
162        if not self.context.r_company:
163            return ''
164        return self.view.url(self.view.context, self.target)
165
166class PayoutletPaymentReceiptActionButtonStudent(PRABStudent):
167
168    grok.view(EtranzactEnterPinPageStudent)
169
170    @property
171    def target_url(self):
172        if not self.context.r_company:
173            return ''
174        return self.view.url(self.view.context, self.target)
175
176class EtranzactQueryHistoryPageStudent(UtilityView, grok.View):
177    """ Query history of Etranzact payments
178    """
179    grok.context(INigeriaStudentOnlinePayment)
180    grok.name('query_payoutlet_history')
181    grok.require('waeup.payStudent')
182    terminal_id = TERMINAL_ID
183    host = HOST
184    https = HTTPS
185
186    def update(self, confirmation_number=None):
187        if not payoutlet_module_activated(
188            self.context.student.current_session, self.context):
189            self.flash(_('Forbidden'), type='danger')
190            self.redirect(self.url(self.context, '@@index'))
191            return
192        if self.context.p_state == 'paid':
193            self.flash(_('This ticket has already been paid.'))
194            return
195        student = self.context.student
196        success, msg, log = query_payoutlet(
197            self.host, self.terminal_id, confirmation_number,
198            self.context, self.https)
199        student.writeLogMessage(self, log)
200        if not success:
201            self.flash(msg, type="danger")
202            return
203        flashtype, msg, log = self.context.doAfterStudentPayment()
204        if log is not None:
205            student.writeLogMessage(self, log)
206        self.flash(msg, type=flashtype)
207        return
208
209    def render(self):
210        self.redirect(self.url(self.context, '@@index'))
211        return
212
213class EtranzactQueryHistoryPageApplicant(UtilityView, grok.View):
214    """ Query history of Etranzact payments
215    """
216    grok.context(INigeriaApplicantOnlinePayment)
217    grok.name('query_payoutlet_history')
218    grok.require('waeup.payApplicant')
219    terminal_id = TERMINAL_ID
220    host = HOST
221    https = HTTPS
222
223    def update(self, confirmation_number=None):
224        if not payoutlet_module_activated(
225            self.context.__parent__.__parent__.year, self.context):
226            self.flash(_('Forbidden'), type='danger')
227            self.redirect(self.url(self.context, '@@index'))
228            return
229        if self.context.p_state == 'paid':
230            self.flash(_('This ticket has already been paid.'))
231            return
232        applicant = self.context.__parent__
233        success, msg, log = query_payoutlet(
234            self.host, self.terminal_id, confirmation_number,
235            self.context, self.https)
236        applicant.writeLogMessage(self, log)
237        if not success:
238            self.flash(msg)
239            return
240        flashtype, msg, log = self.context.doAfterApplicantPayment()
241        if log is not None:
242            applicant.writeLogMessage(self, log)
243        self.flash(msg, type=flashtype)
244        return
245
246    def render(self):
247        self.redirect(self.url(self.context, '@@index'))
248        return
Note: See TracBrowser for help on using the repository browser.