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 | ## |
---|
18 | import grok |
---|
19 | import hashlib |
---|
20 | from datetime import datetime, timedelta |
---|
21 | from zope.component import getUtility |
---|
22 | from zope.security import checkPermission |
---|
23 | from waeup.kofa.interfaces import IKofaUtils |
---|
24 | from waeup.kofa.utils.helpers import to_timezone |
---|
25 | from waeup.kofa.browser.layout import UtilityView, KofaPage |
---|
26 | from waeup.kofa.browser.viewlets import ManageActionButton |
---|
27 | from waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant |
---|
28 | from kofacustom.nigeria.etranzact.helpers import (write_payments_log) |
---|
29 | from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment |
---|
30 | from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment |
---|
31 | from kofacustom.nigeria.interfaces import MessageFactory as _ |
---|
32 | |
---|
33 | from kofacustom.nigeria.etranzact.tests import ( |
---|
34 | TERMINAL_ID, HOST, HTTPS, SECRET_KEY, LOGO_URL) |
---|
35 | |
---|
36 | grok.templatedir('browser_templates') |
---|
37 | |
---|
38 | def 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 | |
---|
47 | class 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 | |
---|
66 | class 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 |
---|