source: main/kofacustom.nigeria/branches/uli-paypal/src/kofacustom/nigeria/etranzact/studentsbrowser.py @ 17259

Last change on this file since 17259 was 16484, checked in by Henrik Bettermann, 4 years ago

Implement PAYDirect Bank Branch payment.

File size: 9.0 KB
Line 
1## $Id: studentsbrowser.py 15599 2019-09-20 13:24:59Z 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.students.browser import NigeriaOnlinePaymentDisplayFormPage as NOPDPStudent
30from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
31from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment
32from kofacustom.nigeria.interfaces import MessageFactory as _
33
34from kofacustom.nigeria.etranzact.tests import (
35    TERMINAL_ID, HOST, HTTPS, SECRET_KEY, LOGO_URL, GATEWAY_AMT)
36
37grok.templatedir('browser_templates')
38
39def webconnect_module_activated(session, payment):
40    if payment.r_company and payment.r_company != 'etranzact':
41        return False
42    try:
43        return getattr(grok.getSite()['configuration'][str(session)],
44            'etranzact_webconnect_enabled', False)
45    except KeyError:
46        session = datetime.now().year
47        try:
48            return getattr(grok.getSite()['configuration'][str(session)],
49                'etranzact_webconnect_enabled', False)
50        except KeyError:
51            return False
52
53class EtranzactActionButtonStudent(ManageActionButton):
54    grok.order(8)
55    grok.context(INigeriaOnlinePayment)
56    grok.view(NOPDPStudent)
57    grok.require('waeup.payStudent')
58    icon = 'actionicon_pay.png'
59    text = _('Pay via Etranzact WebConnect')
60    target = 'goto_etranzact'
61
62    @property
63    def target_url(self):
64        if not webconnect_module_activated(
65            self.context.student.current_session, self.context):
66            return ''
67        if self.context.p_state != 'unpaid':
68            return ''
69        return self.view.url(self.view.context, self.target)
70
71class EtranzactRequeryActionButtonStudent(ManageActionButton):
72    grok.order(9)
73    grok.context(INigeriaOnlinePayment)
74    grok.view(NOPDPStudent)
75    grok.require('waeup.payStudent')
76    icon = 'actionicon_call.png'
77    text = _('Requery Etranzact WebConnect History')
78    target = 'requery_history'
79
80    @property
81    def target_url(self):
82        if not webconnect_module_activated(
83            self.context.student.current_session, self.context):
84            return ''
85        if self.context.p_state in ('paid', 'waived', 'scholarship', 'failed'):
86            return ''
87        return self.view.url(self.view.context, self.target)
88
89class EtranzactPageStudent(KofaPage):
90    """ View which sends a POST request to the Etranzact payment gateway.
91    """
92    grok.context(INigeriaStudentOnlinePayment)
93    grok.name('goto_etranzact')
94    grok.template('goto_etranzact')
95    grok.require('waeup.payStudent')
96    label = _('Pay via Etranzact')
97    submit_button = _('Pay now')
98
99    host = HOST
100    https = HTTPS
101    secret_key = SECRET_KEY
102    terminal_id = TERMINAL_ID
103    logo_url = LOGO_URL
104    gateway_amt = GATEWAY_AMT
105
106    @property
107    def action(self):
108        if self.https:
109            return 'https://' + self.host + '/webconnect/v3/caller.jsp'
110        return 'http://' + self.host + '/webconnect/v3/caller.jsp'
111
112    def init_update(self):
113        if self.context.p_state == 'paid':
114            return _("Payment ticket can't be re-sent to Etranzact.")
115        now = datetime.utcnow()
116        if self.context.creation_date.tzinfo is not None:
117            # That's bad. Please store timezone-naive datetimes only!
118            now = self.context.creation_date.tzinfo.localize(now)
119        time_delta = now - self.context.creation_date
120        if time_delta.days > 7:
121            return _("This payment ticket is too old. Please create a new ticket.")
122        # In contrast to the procedure in the Remita and Interswitch modules,
123        # we do not call requery_history but receive and evaluate
124        # the response form from Etranzact directly. This is possible
125        # because Etranzact provides the FINAL_CHECKSUM hash value
126        # which authenticates the response.
127        self.responseurl = self.url(self.context, 'receive_etranzact')
128        self.transaction_id = self.context.p_id
129        self.description = "%s (%s)" % (
130            self.context.p_category, self.context.p_item)
131        hashargs =      self.amount + self.terminal_id + self.transaction_id \
132            + self.responseurl + self.secret_key
133        self.hashvalue = hashlib.md5(hashargs).hexdigest()
134        self.customer = self.context.student
135        return
136
137    def update(self):
138        if not webconnect_module_activated(
139            self.context.student.current_session, self.context):
140            self.flash(_('Forbidden'), type='danger')
141            self.redirect(self.url(self.context, '@@index'))
142            return
143        # Already now it becomes an Etranzact payment. We set the net amount
144        # and add the gateway amount.
145        if not self.context.r_company:
146            self.context.net_amt = self.context.amount_auth
147            self.context.amount_auth += self.gateway_amt
148            self.context.gateway_amt = self.gateway_amt
149            self.context.r_company = u'etranzact'
150        self.amount = "%.1f" % self.context.amount_auth
151        error = self.init_update()
152        if error:
153            self.flash(error, type='danger')
154            self.redirect(self.url(self.context, '@@index'))
155            return
156        return
157
158class EtranzactReceiveResponseStudent(NOPDPStudent):
159    """ View that receives the response from eTrantact payment gateway.
160    """
161    grok.name('receive_etranzact')
162
163    secret_key = SECRET_KEY
164    terminal_id = TERMINAL_ID
165
166    def update(self):
167        super(EtranzactReceiveResponseStudent, self).update()
168        if not webconnect_module_activated(
169            self.context.student.current_session, self.context):
170            self.flash(_('Forbidden'), type='danger')
171            self.redirect(self.url(self.context, '@@index'))
172            return
173        student = self.context.student
174        form = self.request.form
175        verify = False
176        if self.context.p_state == 'paid':
177            verify = True
178        success, msg, log = process_response(self.context, form, self, verify)
179        student.writeLogMessage(self, log)
180        if not success:
181            self.flash(msg, type='danger')
182            return
183        write_payments_log(student.student_id, self.context)
184        flashtype, msg, log = self.context.doAfterStudentPayment()
185        if log is not None:
186            student.writeLogMessage(self, log)
187        self.flash(msg, type=flashtype)
188        return
189
190class EtranzactRequestPaymentStatusPageStudent(UtilityView, grok.View):
191    """ Request webservice view for the Etranzact gateway.
192    """
193    grok.context(INigeriaStudentOnlinePayment)
194    grok.name('requery_history')
195    grok.require('waeup.payStudent')
196
197    host = HOST
198    https = HTTPS
199    secret_key = SECRET_KEY
200    terminal_id = TERMINAL_ID
201    logo_url = LOGO_URL
202
203    def update(self):
204        if not webconnect_module_activated(
205            self.context.student.current_session, self.context):
206            self.flash(_('Forbidden'), type='danger')
207            self.redirect(self.url(self.context, '@@index'))
208            return
209        if self.context.p_state in ('paid', 'waived', 'scholarship', 'failed'):
210            self.flash(_('This ticket has already been processed.'), type='danger')
211            return
212        student = self.context.student
213        verify = False
214        raw, form = query_history(self.host, self.terminal_id,
215                                  self.context.p_id, self.https)
216        success, msg, log = process_response(self.context, form, self, verify)
217        student.writeLogMessage(self, log)
218        if not success:
219            self.flash(msg, type='danger')
220            return
221        write_payments_log(student.student_id, self.context)
222        flashtype, msg, log = self.context.doAfterStudentPayment()
223        if log is not None:
224            student.writeLogMessage(self, log)
225        self.flash(msg, type=flashtype)
226        return
227
228    def render(self):
229        self.redirect(self.url(self.context))
230        return
Note: See TracBrowser for help on using the repository browser.