[15600] | 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 | ## |
---|
| 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 kofacustom.nigeria.etranzact.helpers import ( |
---|
| 28 | write_payments_log, process_response, query_history) |
---|
| 29 | from kofacustom.nigeria.students.browser import NigeriaOnlinePaymentDisplayFormPage as NOPDPStudent |
---|
| 30 | from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment |
---|
| 31 | from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment |
---|
| 32 | from kofacustom.nigeria.interfaces import MessageFactory as _ |
---|
| 33 | |
---|
| 34 | from kofacustom.nigeria.etranzact.tests import ( |
---|
[15755] | 35 | TERMINAL_ID, HOST, HTTPS, SECRET_KEY, LOGO_URL, GATEWAY_AMT) |
---|
[15600] | 36 | |
---|
| 37 | grok.templatedir('browser_templates') |
---|
| 38 | |
---|
[15772] | 39 | def webconnect_module_activated(session, payment): |
---|
[15770] | 40 | if payment.r_company and payment.r_company != 'etranzact': |
---|
| 41 | return False |
---|
[15600] | 42 | try: |
---|
| 43 | return getattr(grok.getSite()['configuration'][str(session)], |
---|
[15702] | 44 | 'etranzact_webconnect_enabled', False) |
---|
[15600] | 45 | except KeyError: |
---|
[16246] | 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 |
---|
[15600] | 52 | |
---|
| 53 | class EtranzactActionButtonStudent(ManageActionButton): |
---|
[16484] | 54 | grok.order(8) |
---|
[15600] | 55 | grok.context(INigeriaOnlinePayment) |
---|
| 56 | grok.view(NOPDPStudent) |
---|
| 57 | grok.require('waeup.payStudent') |
---|
| 58 | icon = 'actionicon_pay.png' |
---|
[15796] | 59 | text = _('Pay via Etranzact WebConnect') |
---|
[15600] | 60 | target = 'goto_etranzact' |
---|
| 61 | |
---|
| 62 | @property |
---|
| 63 | def target_url(self): |
---|
[15772] | 64 | if not webconnect_module_activated( |
---|
[15770] | 65 | self.context.student.current_session, self.context): |
---|
[15600] | 66 | return '' |
---|
| 67 | if self.context.p_state != 'unpaid': |
---|
| 68 | return '' |
---|
| 69 | return self.view.url(self.view.context, self.target) |
---|
| 70 | |
---|
| 71 | class EtranzactRequeryActionButtonStudent(ManageActionButton): |
---|
[16484] | 72 | grok.order(9) |
---|
[15600] | 73 | grok.context(INigeriaOnlinePayment) |
---|
| 74 | grok.view(NOPDPStudent) |
---|
| 75 | grok.require('waeup.payStudent') |
---|
| 76 | icon = 'actionicon_call.png' |
---|
[15796] | 77 | text = _('Requery Etranzact WebConnect History') |
---|
[15600] | 78 | target = 'requery_history' |
---|
| 79 | |
---|
| 80 | @property |
---|
| 81 | def target_url(self): |
---|
[15772] | 82 | if not webconnect_module_activated( |
---|
[15770] | 83 | self.context.student.current_session, self.context): |
---|
[15600] | 84 | return '' |
---|
[16379] | 85 | if self.context.p_state in ('paid', 'waived', 'scholarship', 'failed'): |
---|
[15600] | 86 | return '' |
---|
| 87 | return self.view.url(self.view.context, self.target) |
---|
| 88 | |
---|
| 89 | class EtranzactPageStudent(KofaPage): |
---|
[15702] | 90 | """ View which sends a POST request to the Etranzact payment gateway. |
---|
[15600] | 91 | """ |
---|
| 92 | grok.context(INigeriaStudentOnlinePayment) |
---|
| 93 | grok.name('goto_etranzact') |
---|
| 94 | grok.template('goto_etranzact') |
---|
| 95 | grok.require('waeup.payStudent') |
---|
[15702] | 96 | label = _('Pay via Etranzact') |
---|
[15600] | 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 |
---|
[15755] | 104 | gateway_amt = GATEWAY_AMT |
---|
[15600] | 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': |
---|
[15702] | 114 | return _("Payment ticket can't be re-sent to Etranzact.") |
---|
[15600] | 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 |
---|
[15702] | 124 | # the response form from Etranzact directly. This is possible |
---|
| 125 | # because Etranzact provides the FINAL_CHECKSUM hash value |
---|
[15600] | 126 | # which authenticates the response. |
---|
| 127 | self.responseurl = self.url(self.context, 'receive_etranzact') |
---|
[15755] | 128 | self.transaction_id = self.context.p_id |
---|
[16271] | 129 | self.description = "%s (%s)" % ( |
---|
| 130 | self.context.p_category, self.context.p_item) |
---|
[15755] | 131 | hashargs = self.amount + self.terminal_id + self.transaction_id \ |
---|
[15600] | 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): |
---|
[15974] | 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 |
---|
[15755] | 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' |
---|
[15600] | 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 | |
---|
| 158 | class 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() |
---|
[15772] | 168 | if not webconnect_module_activated( |
---|
[15770] | 169 | self.context.student.current_session, self.context): |
---|
[15974] | 170 | self.flash(_('Forbidden'), type='danger') |
---|
| 171 | self.redirect(self.url(self.context, '@@index')) |
---|
[15600] | 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 | |
---|
| 190 | class EtranzactRequestPaymentStatusPageStudent(UtilityView, grok.View): |
---|
[15702] | 191 | """ Request webservice view for the Etranzact gateway. |
---|
[15600] | 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): |
---|
[15772] | 204 | if not webconnect_module_activated( |
---|
[15770] | 205 | self.context.student.current_session, self.context): |
---|
[15974] | 206 | self.flash(_('Forbidden'), type='danger') |
---|
| 207 | self.redirect(self.url(self.context, '@@index')) |
---|
[15600] | 208 | return |
---|
[16379] | 209 | if self.context.p_state in ('paid', 'waived', 'scholarship', 'failed'): |
---|
| 210 | self.flash(_('This ticket has already been processed.'), type='danger') |
---|
[15600] | 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 |
---|