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

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

Rename module_activated.

  • Property svn:keywords set to Id
File size: 8.1 KB
Line 
1## $Id: applicantsbrowser.py 15772 2019-11-07 17:00:51Z henrik $
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
27from kofacustom.nigeria.etranzact.helpers import (
28    write_payments_log, process_response, query_history)
29from kofacustom.nigeria.applicants.browser import NigeriaOnlinePaymentDisplayFormPage as NOPDPApplicant
30from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
31from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
32from kofacustom.nigeria.interfaces import MessageFactory as _
33from kofacustom.nigeria.etranzact.studentsbrowser import webconnect_module_activated
34from kofacustom.nigeria.etranzact.tests import (
35    TERMINAL_ID, HOST, HTTPS, SECRET_KEY, LOGO_URL, GATEWAY_AMT)
36
37grok.templatedir('browser_templates')
38
39class EtranzactActionButtonApplicant(ManageActionButton):
40    grok.order(1)
41    grok.context(INigeriaOnlinePayment)
42    grok.view(NOPDPApplicant)
43    grok.require('waeup.payApplicant')
44    icon = 'actionicon_pay.png'
45    text = _('Pay via Etranzact')
46    target = 'goto_etranzact'
47
48    @property
49    def target_url(self):
50        if not webconnect_module_activated(
51            self.context.__parent__.__parent__.year, self.context):
52            return ''
53        if self.context.p_state != 'unpaid':
54            return ''
55        return self.view.url(self.view.context, self.target)
56
57class EtranzactRequeryActionButtonApplicant(ManageActionButton):
58    grok.order(2)
59    grok.context(INigeriaOnlinePayment)
60    grok.view(NOPDPApplicant)
61    grok.require('waeup.payApplicant')
62    icon = 'actionicon_call.png'
63    text = _('Requery Etranzact History')
64    target = 'requery_history'
65
66    @property
67    def target_url(self):
68        if not webconnect_module_activated(
69            self.context.__parent__.__parent__.year, self.context):
70            return ''
71        if self.context.p_state in ('paid', 'waived'):
72            return ''
73        return self.view.url(self.view.context, self.target)
74
75class EtranzactPageApplicant(KofaPage):
76    """ View which sends a POST request to the Etranzact payment gateway.
77    """
78    grok.context(INigeriaApplicantOnlinePayment)
79    grok.name('goto_etranzact')
80    grok.template('goto_etranzact')
81    grok.require('waeup.payApplicant')
82    label = _('Pay via Etranzact')
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
90    gateway_amt = GATEWAY_AMT
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':
100            return _("Payment ticket can't be re-sent to Etranzact.")
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.")
108        # In contrast to the procedure in the Remita and Interswitch modules,
109        # we do not call requery_history but receive and evaluate
110        # the response form from Etranzact directly. This is possible
111        # because Etranzact provides the FINAL_CHECKSUM hash value
112        # which authenticates the response.
113        self.responseurl = self.url(self.context, 'receive_etranzact')
114        self.transaction_id = self.context.p_id
115        hashargs =      self.amount + self.terminal_id + self.transaction_id \
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):
122        # Already now it becomes an Etranzact payment. We set the net amount
123        # and add the gateway amount.
124        if not webconnect_module_activated(
125            self.context.__parent__.__parent__.year, self.context):
126            return _("Etranzact payments deactivated.")
127        if not self.context.r_company:
128            self.context.net_amt = self.context.amount_auth
129            self.context.amount_auth += self.gateway_amt
130            self.context.gateway_amt = self.gateway_amt
131            self.context.r_company = u'etranzact'
132        self.amount = "%.1f" % self.context.amount_auth
133        error = self.init_update()
134        if error:
135            self.flash(error, type='danger')
136            self.redirect(self.url(self.context, '@@index'))
137            return
138        return
139
140class EtranzactReceiveResponseApplicant(NOPDPApplicant):
141    """ View that receives the response from eTrantact payment gateway.
142    """
143    grok.name('receive_etranzact')
144
145    secret_key = SECRET_KEY
146    terminal_id = TERMINAL_ID
147
148    def update(self):
149        super(EtranzactReceiveResponseApplicant, self).update()
150        if not webconnect_module_activated(
151            self.context.__parent__.__parent__.year, self.context):
152            return
153        applicant = self.context.__parent__
154        form = self.request.form
155        verify = False
156        if self.context.p_state == 'paid':
157            verify = True
158        success, msg, log = process_response(self.context, form, self, verify)
159        applicant.writeLogMessage(self, log)
160        if not success:
161            self.flash(msg, type='danger')
162            return
163        write_payments_log(applicant.applicant_id, self.context)
164        flashtype, msg, log = self.context.doAfterApplicantPayment()
165        if log is not None:
166            applicant.writeLogMessage(self, log)
167        self.flash(msg, type=flashtype)
168        return
169
170class EtranzactRequestPaymentStatusPageApplicant(UtilityView, grok.View):
171    """ Request webservice view for the Etranzact gateway.
172    """
173    grok.context(INigeriaApplicantOnlinePayment)
174    grok.name('requery_history')
175    grok.require('waeup.payApplicant')
176
177    host = HOST
178    https = HTTPS
179    secret_key = SECRET_KEY
180    terminal_id = TERMINAL_ID
181    logo_url = LOGO_URL
182
183    def update(self):
184        if not webconnect_module_activated(
185            self.context.__parent__.__parent__.year, self.context):
186            return
187        if self.context.p_state in ('paid', 'waived'):
188            self.flash(_('This ticket has already been paid.'), type='danger')
189            return
190        applicant = self.context.__parent__
191        verify = False
192        raw, form = query_history(self.host, self.terminal_id,
193                                  self.context.p_id, self.https)
194        success, msg, log = process_response(self.context, form, self, verify)
195        applicant.writeLogMessage(self, log)
196        if not success:
197            self.flash(msg, type='danger')
198            return
199        write_payments_log(applicant.applicant_id, self.context)
200        flashtype, msg, log = self.context.doAfterApplicantPayment()
201        if log is not None:
202            applicant.writeLogMessage(self, log)
203        self.flash(msg, type=flashtype)
204        return
205
206    def render(self):
207        self.redirect(self.url(self.context))
208        return
Note: See TracBrowser for help on using the repository browser.