source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/etranzact/applicantsbrowser.py @ 16153

Last change on this file since 16153 was 15974, checked in by Henrik Bettermann, 5 years ago

Do not allow to submit forms to other companies than payment.r_company.

  • Property svn:keywords set to Id
File size: 8.4 KB
RevLine 
[15586]1## $Id: applicantsbrowser.py 15974 2020-01-31 21:50:08Z henrik $
[15585]2##
3## Copyright (C) 2017 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
19import hashlib
20from datetime import datetime, timedelta
21from zope.component import getUtility
22from zope.security import checkPermission
23from waeup.kofa.interfaces import IKofaUtils
24from waeup.kofa.utils.helpers import to_timezone
25from waeup.kofa.browser.layout import UtilityView, KofaPage
26from waeup.kofa.browser.viewlets import ManageActionButton
[15589]27from kofacustom.nigeria.etranzact.helpers import (
28    write_payments_log, process_response, query_history)
[15600]29from kofacustom.nigeria.applicants.browser import NigeriaOnlinePaymentDisplayFormPage as NOPDPApplicant
[15585]30from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
31from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
32from kofacustom.nigeria.interfaces import MessageFactory as _
[15772]33from kofacustom.nigeria.etranzact.studentsbrowser import webconnect_module_activated
[15585]34from kofacustom.nigeria.etranzact.tests import (
[15755]35    TERMINAL_ID, HOST, HTTPS, SECRET_KEY, LOGO_URL, GATEWAY_AMT)
[15585]36
37grok.templatedir('browser_templates')
38
39class EtranzactActionButtonApplicant(ManageActionButton):
40    grok.order(1)
41    grok.context(INigeriaOnlinePayment)
[15600]42    grok.view(NOPDPApplicant)
[15585]43    grok.require('waeup.payApplicant')
44    icon = 'actionicon_pay.png'
[15796]45    text = _('Pay via Etranzact WebConnect')
[15585]46    target = 'goto_etranzact'
47
48    @property
49    def target_url(self):
[15772]50        if not webconnect_module_activated(
[15770]51            self.context.__parent__.__parent__.year, self.context):
[15585]52            return ''
53        if self.context.p_state != 'unpaid':
54            return ''
55        return self.view.url(self.view.context, self.target)
56
[15596]57class EtranzactRequeryActionButtonApplicant(ManageActionButton):
58    grok.order(2)
59    grok.context(INigeriaOnlinePayment)
[15600]60    grok.view(NOPDPApplicant)
[15596]61    grok.require('waeup.payApplicant')
62    icon = 'actionicon_call.png'
[15796]63    text = _('Requery Etranzact WebConnect History')
[15596]64    target = 'requery_history'
65
66    @property
67    def target_url(self):
[15772]68        if not webconnect_module_activated(
[15770]69            self.context.__parent__.__parent__.year, self.context):
[15596]70            return ''
[15796]71        if self.context.p_state in ('paid', 'waived', 'scholarship'):
[15596]72            return ''
73        return self.view.url(self.view.context, self.target)
74
[15585]75class EtranzactPageApplicant(KofaPage):
[15702]76    """ View which sends a POST request to the Etranzact payment gateway.
[15585]77    """
78    grok.context(INigeriaApplicantOnlinePayment)
79    grok.name('goto_etranzact')
80    grok.template('goto_etranzact')
81    grok.require('waeup.payApplicant')
[15702]82    label = _('Pay via Etranzact')
[15585]83    submit_button = _('Pay now')
84
85    host = HOST
86    https = HTTPS
87    secret_key = SECRET_KEY
88    terminal_id = TERMINAL_ID
89    logo_url = LOGO_URL
[15755]90    gateway_amt = GATEWAY_AMT
[15585]91
92    @property
93    def action(self):
94        if self.https:
95            return 'https://' + self.host + '/webconnect/v3/caller.jsp'
96        return 'http://' + self.host + '/webconnect/v3/caller.jsp'
97
98    def init_update(self):
99        if self.context.p_state == 'paid':
[15702]100            return _("Payment ticket can't be re-sent to Etranzact.")
[15585]101        now = datetime.utcnow()
102        if self.context.creation_date.tzinfo is not None:
103            # That's bad. Please store timezone-naive datetimes only!
104            now = self.context.creation_date.tzinfo.localize(now)
105        time_delta = now - self.context.creation_date
106        if time_delta.days > 7:
107            return _("This payment ticket is too old. Please create a new ticket.")
[15596]108        # In contrast to the procedure in the Remita and Interswitch modules,
109        # we do not call requery_history but receive and evaluate
[15702]110        # the response form from Etranzact directly. This is possible
111        # because Etranzact provides the FINAL_CHECKSUM hash value
[15596]112        # which authenticates the response.
[15589]113        self.responseurl = self.url(self.context, 'receive_etranzact')
[15755]114        self.transaction_id = self.context.p_id
115        hashargs =      self.amount + self.terminal_id + self.transaction_id \
[15585]116            + self.responseurl + self.secret_key
117        self.hashvalue = hashlib.md5(hashargs).hexdigest()
118        self.customer = self.context.__parent__
119        return
120
121    def update(self):
[15974]122        if not webconnect_module_activated(
123            self.context.__parent__.__parent__.year, self.context):
124            self.flash(_('Forbidden'), type='danger')
125            self.redirect(self.url(self.context, '@@index'))
126            return
[15755]127        # Already now it becomes an Etranzact payment. We set the net amount
128        # and add the gateway amount.
129        if not self.context.r_company:
130            self.context.net_amt = self.context.amount_auth
131            self.context.amount_auth += self.gateway_amt
132            self.context.gateway_amt = self.gateway_amt
133            self.context.r_company = u'etranzact'
[15585]134        self.amount = "%.1f" % self.context.amount_auth
135        error = self.init_update()
136        if error:
137            self.flash(error, type='danger')
138            self.redirect(self.url(self.context, '@@index'))
139            return
140        return
[15589]141
[15600]142class EtranzactReceiveResponseApplicant(NOPDPApplicant):
[15589]143    """ View that receives the response from eTrantact payment gateway.
144    """
145    grok.name('receive_etranzact')
146
147    secret_key = SECRET_KEY
148    terminal_id = TERMINAL_ID
149
150    def update(self):
151        super(EtranzactReceiveResponseApplicant, self).update()
[15772]152        if not webconnect_module_activated(
[15770]153            self.context.__parent__.__parent__.year, self.context):
[15974]154            self.flash(_('Forbidden'), type='danger')
155            self.redirect(self.url(self.context, '@@index'))
[15589]156            return
157        applicant = self.context.__parent__
158        form = self.request.form
159        verify = False
160        if self.context.p_state == 'paid':
161            verify = True
162        success, msg, log = process_response(self.context, form, self, verify)
163        applicant.writeLogMessage(self, log)
164        if not success:
165            self.flash(msg, type='danger')
166            return
167        write_payments_log(applicant.applicant_id, self.context)
168        flashtype, msg, log = self.context.doAfterApplicantPayment()
169        if log is not None:
170            applicant.writeLogMessage(self, log)
171        self.flash(msg, type=flashtype)
172        return
173
[15596]174class EtranzactRequestPaymentStatusPageApplicant(UtilityView, grok.View):
[15702]175    """ Request webservice view for the Etranzact gateway.
[15589]176    """
177    grok.context(INigeriaApplicantOnlinePayment)
178    grok.name('requery_history')
179    grok.require('waeup.payApplicant')
180
181    host = HOST
182    https = HTTPS
183    secret_key = SECRET_KEY
184    terminal_id = TERMINAL_ID
185    logo_url = LOGO_URL
186
187    def update(self):
[15772]188        if not webconnect_module_activated(
[15770]189            self.context.__parent__.__parent__.year, self.context):
[15974]190            self.flash(_('Forbidden'), type='danger')
191            self.redirect(self.url(self.context, '@@index'))
[15589]192            return
[15796]193        if self.context.p_state in ('paid', 'waived', 'scholarship'):
[15596]194            self.flash(_('This ticket has already been paid.'), type='danger')
195            return
196        applicant = self.context.__parent__
197        verify = False
198        raw, form = query_history(self.host, self.terminal_id,
199                                  self.context.p_id, self.https)
200        success, msg, log = process_response(self.context, form, self, verify)
201        applicant.writeLogMessage(self, log)
202        if not success:
203            self.flash(msg, type='danger')
204            return
205        write_payments_log(applicant.applicant_id, self.context)
206        flashtype, msg, log = self.context.doAfterApplicantPayment()
207        if log is not None:
208            applicant.writeLogMessage(self, log)
209        self.flash(msg, type=flashtype)
[15589]210        return
[15596]211
212    def render(self):
[15599]213        self.redirect(self.url(self.context))
[15596]214        return
Note: See TracBrowser for help on using the repository browser.