[7929] | 1 | ## $Id: browser.py 8776 2012-06-22 21:11:40Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2012 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 | from datetime import datetime |
---|
| 19 | import httplib |
---|
| 20 | import urllib |
---|
| 21 | from xml.dom.minidom import parseString |
---|
| 22 | import grok |
---|
[8704] | 23 | from zope.component import getUtility |
---|
| 24 | from zope.catalog.interfaces import ICatalog |
---|
[8698] | 25 | from waeup.kofa.interfaces import IUniversity |
---|
[8704] | 26 | from waeup.kofa.payments.interfaces import IPaymentWebservice |
---|
[7929] | 27 | from waeup.kofa.browser.layout import KofaPage, UtilityView |
---|
[8430] | 28 | from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent |
---|
| 29 | from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant |
---|
[8710] | 30 | from waeup.aaue.interfaces import academic_sessions_vocab |
---|
[8444] | 31 | from waeup.aaue.interfaces import MessageFactory as _ |
---|
| 32 | from waeup.aaue.students.interfaces import ICustomStudentOnlinePayment |
---|
| 33 | from waeup.aaue.applicants.interfaces import ICustomApplicantOnlinePayment |
---|
[7929] | 34 | |
---|
[8769] | 35 | #ACCEPTED_IP = ('195.219.3.181', '195.219.3.184') |
---|
[8746] | 36 | ACCEPTED_IP = None |
---|
| 37 | |
---|
[8769] | 38 | |
---|
[8698] | 39 | # Kofa's webservice |
---|
| 40 | |
---|
| 41 | class KofaFeeRequest(grok.View): |
---|
| 42 | grok.context(IUniversity) |
---|
| 43 | grok.name('feerequest') |
---|
| 44 | grok.require('waeup.Public') |
---|
| 45 | |
---|
| 46 | def update(self, PAYEE_ID=None): |
---|
[8746] | 47 | real_ip = self.request.get('HTTP_X_FORWARDED_FOR', None) |
---|
[8769] | 48 | # We can forego the logging once eTranzact payments run smoothly |
---|
| 49 | # and the accepted IP addresses are used. |
---|
| 50 | if real_ip: |
---|
| 51 | self.context.logger.info('KofaFeeRequest called: %s' % real_ip) |
---|
[8746] | 52 | if real_ip and ACCEPTED_IP: |
---|
[8769] | 53 | if real_ip not in ACCEPTED_IP: |
---|
[8746] | 54 | self.output = '-4' |
---|
| 55 | return |
---|
[8754] | 56 | |
---|
| 57 | # It seems eTranzact sends a POST request with an empty body but the URL |
---|
| 58 | # contains a query string. So it's actually a GET request pretended |
---|
| 59 | # to be a POST request. Although this does not comply with the |
---|
| 60 | # RFC 2616 HTTP guidelines we may try to fetch the id from the QUERY_STRING |
---|
| 61 | # value of the request. |
---|
| 62 | |
---|
| 63 | #if PAYEE_ID is None: |
---|
| 64 | # try: |
---|
| 65 | # PAYEE_ID = self.request['QUERY_STRING'].split('=')[1] |
---|
| 66 | # except: |
---|
| 67 | # self.output = '-2' |
---|
| 68 | # return |
---|
| 69 | |
---|
[8704] | 70 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 71 | results = list(cat.searchResults(p_id=(PAYEE_ID, PAYEE_ID))) |
---|
| 72 | if len(results) != 1: |
---|
| 73 | self.output = '-1' |
---|
[8746] | 74 | return |
---|
| 75 | try: |
---|
| 76 | owner = IPaymentWebservice(results[0]) |
---|
| 77 | full_name = owner.display_fullname |
---|
| 78 | matric_no = owner.id |
---|
| 79 | faculty = owner.faculty |
---|
| 80 | department = owner.department |
---|
| 81 | except (TypeError, AttributeError): |
---|
| 82 | self.output = '-3' |
---|
| 83 | return |
---|
| 84 | amount = results[0].amount_auth |
---|
| 85 | payment_type = results[0].category |
---|
| 86 | programme_type = results[0].p_item |
---|
| 87 | academic_session = academic_sessions_vocab.getTerm( |
---|
| 88 | results[0].p_session).title |
---|
| 89 | status = results[0].p_state |
---|
| 90 | self.output = ( |
---|
| 91 | 'FULL_NAME=%s&' + |
---|
| 92 | 'FACULTY=%s&' + |
---|
| 93 | 'DEPARTMENT=%s&' + |
---|
| 94 | 'RETURN_TYPE=%s&' + |
---|
| 95 | 'PROGRAMME_TYPE=%s&' + |
---|
| 96 | 'PAYMENT_TYPE=%s&' + |
---|
| 97 | 'ACADEMIC_SESSION=%s&' + |
---|
| 98 | 'MATRIC_NO=%s&' + |
---|
| 99 | 'FEE_AMOUNT=%s&' + |
---|
| 100 | 'TRANSACTION_STATUS=%s') % (full_name, faculty, |
---|
| 101 | department, PAYEE_ID, programme_type, payment_type, |
---|
| 102 | academic_session, matric_no, amount, status) |
---|
[8698] | 103 | return |
---|
| 104 | |
---|
| 105 | def render(self): |
---|
| 106 | return self.output |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | # Requerying eTranzact payments |
---|
| 110 | |
---|
[8776] | 111 | TERMINAL_ID = '0330000046' |
---|
| 112 | QUERY_URL = 'https://www.etranzact.net/Query/queryPayoutletTransaction.jsp' |
---|
[8259] | 113 | |
---|
[8680] | 114 | # Test environment |
---|
[8776] | 115 | #QUERY_URL = 'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp' |
---|
| 116 | #TERMINAL_ID = '5009892289' |
---|
[8680] | 117 | |
---|
[8430] | 118 | def query_etranzact(confirmation_number, payment): |
---|
| 119 | |
---|
[8247] | 120 | postdict = {} |
---|
| 121 | postdict['TERMINAL_ID'] = TERMINAL_ID |
---|
| 122 | #postdict['RESPONSE_URL'] = 'http://dummy' |
---|
| 123 | postdict['CONFIRMATION_NO'] = confirmation_number |
---|
| 124 | data = urllib.urlencode(postdict) |
---|
[8682] | 125 | payment.conf_number = confirmation_number |
---|
[8247] | 126 | try: |
---|
| 127 | f = urllib.urlopen(url=QUERY_URL, data=data) |
---|
| 128 | success = f.read() |
---|
[8432] | 129 | success = success.replace('\r\n','') |
---|
[8769] | 130 | if 'COL6' not in success: |
---|
[8430] | 131 | msg = _('Invalid or unsuccessful callback: ${a}', |
---|
| 132 | mapping = {'a': success}) |
---|
| 133 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 134 | payment.p_state = 'failed' |
---|
[8430] | 135 | return False, msg, log |
---|
[8247] | 136 | success = success.replace('%20',' ').split('&') |
---|
| 137 | # We expect at least two parameters |
---|
| 138 | if len(success) < 2: |
---|
[8430] | 139 | msg = _('Invalid callback: ${a}', mapping = {'a': success}) |
---|
| 140 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 141 | payment.p_state = 'failed' |
---|
[8430] | 142 | return False, msg, log |
---|
[8247] | 143 | try: |
---|
| 144 | success_dict = dict([tuple(i.split('=')) for i in success]) |
---|
| 145 | except ValueError: |
---|
[8430] | 146 | msg = _('Invalid callback: ${a}', mapping = {'a': success}) |
---|
| 147 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 148 | payment.p_state = 'failed' |
---|
[8430] | 149 | return False, msg, log |
---|
[8247] | 150 | except IOError: |
---|
[8430] | 151 | msg = _('eTranzact IOError') |
---|
| 152 | log = 'eTranzact IOError' |
---|
| 153 | return False, msg, log |
---|
[8247] | 154 | payment.r_code = u'ET' |
---|
| 155 | payment.r_desc = u'%s' % success_dict.get('TRANS_DESCR') |
---|
| 156 | payment.r_amount_approved = float(success_dict.get('TRANS_AMOUNT',0.0)) |
---|
| 157 | payment.r_card_num = None |
---|
| 158 | payment.r_pay_reference = u'%s' % success_dict.get('RECEIPT_NO') |
---|
| 159 | if payment.r_amount_approved != payment.amount_auth: |
---|
[8430] | 160 | msg = _('Wrong amount') |
---|
| 161 | log = 'wrong callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 162 | payment.p_state = 'failed' |
---|
[8430] | 163 | return False, msg, log |
---|
[8717] | 164 | #tcode = payment.p_id |
---|
| 165 | #tcode = tcode[len(tcode)-8:len(tcode)] |
---|
[8247] | 166 | col1 = success_dict.get('COL1') |
---|
[8717] | 167 | #col1 = col1[len(col1)-8:len(col1)] |
---|
| 168 | #if tcode != col1: |
---|
| 169 | if payment.p_id != col1: |
---|
| 170 | #msg = _('Wrong transaction code') |
---|
| 171 | msg = _('Wrong payment id') |
---|
[8430] | 172 | log = 'wrong callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 173 | payment.p_state = 'failed' |
---|
[8430] | 174 | return False, msg, log |
---|
| 175 | log = 'valid callback for payment %s: %s' % (payment.p_id, success) |
---|
| 176 | msg = _('Successful callback received') |
---|
[8247] | 177 | payment.p_state = 'paid' |
---|
[8433] | 178 | payment.payment_date = datetime.utcnow() |
---|
[8430] | 179 | return True, msg, log |
---|
[8247] | 180 | |
---|
[8430] | 181 | class EtranzactEnterPinActionButtonApplicant(APABApplicant): |
---|
[8253] | 182 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8430] | 183 | grok.require('waeup.payApplicant') |
---|
[8259] | 184 | grok.order(3) |
---|
[7929] | 185 | icon = 'actionicon_call.png' |
---|
| 186 | text = _('Query eTranzact History') |
---|
[7976] | 187 | target = 'enterpin' |
---|
[7929] | 188 | |
---|
[8430] | 189 | class EtranzactEnterPinActionButtonStudent(APABStudent): |
---|
[8253] | 190 | grok.context(ICustomStudentOnlinePayment) |
---|
[8430] | 191 | grok.require('waeup.payStudent') |
---|
[8259] | 192 | grok.order(3) |
---|
[8247] | 193 | icon = 'actionicon_call.png' |
---|
| 194 | text = _('Query eTranzact History') |
---|
| 195 | target = 'enterpin' |
---|
| 196 | |
---|
| 197 | class EtranzactEnterPinPageStudent(KofaPage): |
---|
[7976] | 198 | """ |
---|
| 199 | """ |
---|
[8253] | 200 | grok.context(ICustomStudentOnlinePayment) |
---|
[7976] | 201 | grok.name('enterpin') |
---|
| 202 | grok.template('enterpin') |
---|
[7929] | 203 | grok.require('waeup.payStudent') |
---|
| 204 | |
---|
[7976] | 205 | buttonname = _('Submit to eTranzact') |
---|
| 206 | label = _('Requery eTranzact History') |
---|
| 207 | action = 'query_history' |
---|
[7929] | 208 | |
---|
[8247] | 209 | class EtranzactEnterPinPageApplicant(EtranzactEnterPinPageStudent): |
---|
| 210 | """ |
---|
| 211 | """ |
---|
| 212 | grok.require('waeup.payApplicant') |
---|
[8253] | 213 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8247] | 214 | |
---|
| 215 | class EtranzactQueryHistoryPageStudent(UtilityView, grok.View): |
---|
[7929] | 216 | """ Query history of eTranzact payments |
---|
| 217 | """ |
---|
[8253] | 218 | grok.context(ICustomStudentOnlinePayment) |
---|
[7929] | 219 | grok.name('query_history') |
---|
| 220 | grok.require('waeup.payStudent') |
---|
| 221 | |
---|
| 222 | def update(self, confirmation_number=None): |
---|
| 223 | if self.context.p_state == 'paid': |
---|
| 224 | self.flash(_('This ticket has already been paid.')) |
---|
| 225 | return |
---|
[8763] | 226 | student = self.context.student |
---|
[8430] | 227 | success, msg, log = query_etranzact(confirmation_number,self.context) |
---|
[8764] | 228 | student.writeLogMessage(self, log) |
---|
[8430] | 229 | if not success: |
---|
| 230 | self.flash(msg) |
---|
| 231 | return |
---|
| 232 | success, msg, log = self.context.doAfterStudentPayment() |
---|
| 233 | if log is not None: |
---|
[8764] | 234 | student.writeLogMessage(self, log) |
---|
[8430] | 235 | self.flash(msg) |
---|
[8247] | 236 | return |
---|
[7929] | 237 | |
---|
[8247] | 238 | def render(self): |
---|
| 239 | self.redirect(self.url(self.context, '@@index')) |
---|
| 240 | return |
---|
[7929] | 241 | |
---|
[8247] | 242 | class EtranzactQueryHistoryPageApplicant(UtilityView, grok.View): |
---|
| 243 | """ Query history of eTranzact payments |
---|
| 244 | """ |
---|
[8253] | 245 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8247] | 246 | grok.name('query_history') |
---|
| 247 | grok.require('waeup.payApplicant') |
---|
| 248 | |
---|
| 249 | def update(self, confirmation_number=None): |
---|
[8430] | 250 | ob_class = self.__implemented__.__name__ |
---|
[8247] | 251 | if self.context.p_state == 'paid': |
---|
| 252 | self.flash(_('This ticket has already been paid.')) |
---|
[7929] | 253 | return |
---|
[8247] | 254 | applicant = self.context.__parent__ |
---|
[8430] | 255 | success, msg, log = query_etranzact(confirmation_number,self.context) |
---|
[8769] | 256 | applicant.writeLogMessage(self, log) |
---|
[8430] | 257 | if not success: |
---|
| 258 | self.flash(msg) |
---|
| 259 | return |
---|
| 260 | success, msg, log = self.context.doAfterApplicantPayment() |
---|
| 261 | if log is not None: |
---|
[8769] | 262 | applicant.writeLogMessage(self, log) |
---|
[8430] | 263 | self.flash(msg) |
---|
[7929] | 264 | return |
---|
| 265 | |
---|
| 266 | def render(self): |
---|
| 267 | self.redirect(self.url(self.context, '@@index')) |
---|
[8259] | 268 | return |
---|