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

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

Add property.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1## $Id: applicantsbrowser.py 15586 2019-09-12 05:27:02Z 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 waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant
28from kofacustom.nigeria.etranzact.helpers import (write_payments_log)
29from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
30from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
31from kofacustom.nigeria.interfaces import MessageFactory as _
32
33from kofacustom.nigeria.etranzact.tests import (
34    TERMINAL_ID, HOST, HTTPS, SECRET_KEY, LOGO_URL)
35
36grok.templatedir('browser_templates')
37
38def module_activated(session):
39    try:
40        return getattr(grok.getSite()['configuration'][str(session)],
41            'etranzact_enabled', False)
42    except KeyError:
43        return False
44
45# Buttons
46
47class EtranzactActionButtonApplicant(ManageActionButton):
48    grok.order(1)
49    grok.context(INigeriaOnlinePayment)
50    grok.view(OPDPApplicant)
51    grok.require('waeup.payApplicant')
52    icon = 'actionicon_pay.png'
53    text = _('Pay via eTranzact')
54    target = 'goto_etranzact'
55
56    @property
57    def target_url(self):
58        if not module_activated(self.context.__parent__.__parent__.year):
59            return ''
60        if self.context.p_state != 'unpaid':
61            return ''
62        return self.view.url(self.view.context, self.target)
63
64# Forwarding pages
65
66class EtranzactPageApplicant(KofaPage):
67    """ View which sends a POST request to the eTranzact payment gateway.
68    """
69    grok.context(INigeriaApplicantOnlinePayment)
70    grok.name('goto_etranzact')
71    grok.template('goto_etranzact')
72    grok.require('waeup.payApplicant')
73    label = _('Pay via eTranzact')
74    submit_button = _('Pay now')
75
76    host = HOST
77    https = HTTPS
78    secret_key = SECRET_KEY
79    terminal_id = TERMINAL_ID
80    logo_url = LOGO_URL
81
82    @property
83    def action(self):
84        if self.https:
85            return 'https://' + self.host + '/webconnect/v3/caller.jsp'
86        return 'http://' + self.host + '/webconnect/v3/caller.jsp'
87
88    def init_update(self):
89        if self.context.p_state == 'paid':
90            return _("Payment ticket can't be re-sent to eTranzact.")
91        now = datetime.utcnow()
92        if self.context.creation_date.tzinfo is not None:
93            # That's bad. Please store timezone-naive datetimes only!
94            now = self.context.creation_date.tzinfo.localize(now)
95        time_delta = now - self.context.creation_date
96        if time_delta.days > 7:
97            return _("This payment ticket is too old. Please create a new ticket.")
98        self.responseurl = self.url(self.context, 'query_etranzact')
99        # Already now it becomes a eTranzact payment
100        self.context.r_company = u'etranzact'
101        hashargs =      self.amount + self.terminal_id+self.transaction_id \
102            + self.responseurl + self.secret_key
103        self.hashvalue = hashlib.md5(hashargs).hexdigest()
104        self.customer = self.context.__parent__
105        return
106
107    def update(self):
108        if not module_activated(self.context.__parent__.__parent__.year):
109            return
110        self.transaction_id = self.context.p_id
111        self.amount = "%.1f" % self.context.amount_auth
112        error = self.init_update()
113        if error:
114            self.flash(error, type='danger')
115            self.redirect(self.url(self.context, '@@index'))
116            return
117        return
Note: See TracBrowser for help on using the repository browser.