[15586] | 1 | ## $Id: applicantsbrowser.py 15796 2019-11-12 08:23:13Z henrik $ |
---|
[15585] | 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 |
---|
[15589] | 27 | from kofacustom.nigeria.etranzact.helpers import ( |
---|
| 28 | write_payments_log, process_response, query_history) |
---|
[15600] | 29 | from kofacustom.nigeria.applicants.browser import NigeriaOnlinePaymentDisplayFormPage as NOPDPApplicant |
---|
[15585] | 30 | from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment |
---|
| 31 | from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment |
---|
| 32 | from kofacustom.nigeria.interfaces import MessageFactory as _ |
---|
[15772] | 33 | from kofacustom.nigeria.etranzact.studentsbrowser import webconnect_module_activated |
---|
[15585] | 34 | from kofacustom.nigeria.etranzact.tests import ( |
---|
[15755] | 35 | TERMINAL_ID, HOST, HTTPS, SECRET_KEY, LOGO_URL, GATEWAY_AMT) |
---|
[15585] | 36 | |
---|
| 37 | grok.templatedir('browser_templates') |
---|
| 38 | |
---|
| 39 | class EtranzactActionButtonApplicant(ManageActionButton): |
---|
| 40 | grok.order(1) |
---|
| 41 | grok.context(INigeriaOnlinePayment) |
---|
[15600] | 42 | grok.view(NOPDPApplicant) |
---|
[15585] | 43 | grok.require('waeup.payApplicant') |
---|
| 44 | icon = 'actionicon_pay.png' |
---|
[15796] | 45 | text = _('Pay via Etranzact WebConnect') |
---|
[15585] | 46 | target = 'goto_etranzact' |
---|
| 47 | |
---|
| 48 | @property |
---|
| 49 | def target_url(self): |
---|
[15772] | 50 | if not webconnect_module_activated( |
---|
[15770] | 51 | self.context.__parent__.__parent__.year, self.context): |
---|
[15585] | 52 | return '' |
---|
| 53 | if self.context.p_state != 'unpaid': |
---|
| 54 | return '' |
---|
| 55 | return self.view.url(self.view.context, self.target) |
---|
| 56 | |
---|
[15596] | 57 | class EtranzactRequeryActionButtonApplicant(ManageActionButton): |
---|
| 58 | grok.order(2) |
---|
| 59 | grok.context(INigeriaOnlinePayment) |
---|
[15600] | 60 | grok.view(NOPDPApplicant) |
---|
[15596] | 61 | grok.require('waeup.payApplicant') |
---|
| 62 | icon = 'actionicon_call.png' |
---|
[15796] | 63 | text = _('Requery Etranzact WebConnect History') |
---|
[15596] | 64 | target = 'requery_history' |
---|
| 65 | |
---|
| 66 | @property |
---|
| 67 | def target_url(self): |
---|
[15772] | 68 | if not webconnect_module_activated( |
---|
[15770] | 69 | self.context.__parent__.__parent__.year, self.context): |
---|
[15596] | 70 | return '' |
---|
[15796] | 71 | if self.context.p_state in ('paid', 'waived', 'scholarship'): |
---|
[15596] | 72 | return '' |
---|
| 73 | return self.view.url(self.view.context, self.target) |
---|
| 74 | |
---|
[15585] | 75 | class EtranzactPageApplicant(KofaPage): |
---|
[15702] | 76 | """ View which sends a POST request to the Etranzact payment gateway. |
---|
[15585] | 77 | """ |
---|
| 78 | grok.context(INigeriaApplicantOnlinePayment) |
---|
| 79 | grok.name('goto_etranzact') |
---|
| 80 | grok.template('goto_etranzact') |
---|
| 81 | grok.require('waeup.payApplicant') |
---|
[15702] | 82 | label = _('Pay via Etranzact') |
---|
[15585] | 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 |
---|
[15755] | 90 | gateway_amt = GATEWAY_AMT |
---|
[15585] | 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': |
---|
[15702] | 100 | return _("Payment ticket can't be re-sent to Etranzact.") |
---|
[15585] | 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.") |
---|
[15596] | 108 | # In contrast to the procedure in the Remita and Interswitch modules, |
---|
| 109 | # we do not call requery_history but receive and evaluate |
---|
[15702] | 110 | # the response form from Etranzact directly. This is possible |
---|
| 111 | # because Etranzact provides the FINAL_CHECKSUM hash value |
---|
[15596] | 112 | # which authenticates the response. |
---|
[15589] | 113 | self.responseurl = self.url(self.context, 'receive_etranzact') |
---|
[15755] | 114 | self.transaction_id = self.context.p_id |
---|
| 115 | hashargs = self.amount + self.terminal_id + self.transaction_id \ |
---|
[15585] | 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): |
---|
[15755] | 122 | # Already now it becomes an Etranzact payment. We set the net amount |
---|
| 123 | # and add the gateway amount. |
---|
[15772] | 124 | if not webconnect_module_activated( |
---|
| 125 | self.context.__parent__.__parent__.year, self.context): |
---|
| 126 | return _("Etranzact payments deactivated.") |
---|
[15755] | 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' |
---|
[15585] | 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 |
---|
[15589] | 139 | |
---|
[15600] | 140 | class EtranzactReceiveResponseApplicant(NOPDPApplicant): |
---|
[15589] | 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() |
---|
[15772] | 150 | if not webconnect_module_activated( |
---|
[15770] | 151 | self.context.__parent__.__parent__.year, self.context): |
---|
[15589] | 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 | |
---|
[15596] | 170 | class EtranzactRequestPaymentStatusPageApplicant(UtilityView, grok.View): |
---|
[15702] | 171 | """ Request webservice view for the Etranzact gateway. |
---|
[15589] | 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): |
---|
[15772] | 184 | if not webconnect_module_activated( |
---|
[15770] | 185 | self.context.__parent__.__parent__.year, self.context): |
---|
[15589] | 186 | return |
---|
[15796] | 187 | if self.context.p_state in ('paid', 'waived', 'scholarship'): |
---|
[15596] | 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) |
---|
[15589] | 204 | return |
---|
[15596] | 205 | |
---|
| 206 | def render(self): |
---|
[15599] | 207 | self.redirect(self.url(self.context)) |
---|
[15596] | 208 | return |
---|