[7929] | 1 | ## $Id: browser.py 10983 2014-01-24 16:24:06Z 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 |
---|
[10070] | 20 | import urllib |
---|
[10069] | 21 | import urllib2 |
---|
[7929] | 22 | from xml.dom.minidom import parseString |
---|
| 23 | import grok |
---|
[8704] | 24 | from zope.component import getUtility |
---|
| 25 | from zope.catalog.interfaces import ICatalog |
---|
[8698] | 26 | from waeup.kofa.interfaces import IUniversity |
---|
[10032] | 27 | from waeup.kofa.payments.interfaces import IPayer |
---|
| 28 | from waeup.kofa.webservices import PaymentDataWebservice |
---|
[7929] | 29 | from waeup.kofa.browser.layout import KofaPage, UtilityView |
---|
[8430] | 30 | from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent |
---|
| 31 | from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant |
---|
[8710] | 32 | from waeup.aaue.interfaces import academic_sessions_vocab |
---|
[9904] | 33 | from kofacustom.nigeria.interswitch.browser import ( |
---|
| 34 | InterswitchActionButtonStudent, |
---|
| 35 | InterswitchRequestWebserviceActionButtonStudent, |
---|
| 36 | InterswitchActionButtonApplicant, |
---|
| 37 | InterswitchRequestWebserviceActionButtonApplicant) |
---|
[8444] | 38 | from waeup.aaue.interfaces import MessageFactory as _ |
---|
| 39 | from waeup.aaue.students.interfaces import ICustomStudentOnlinePayment |
---|
| 40 | from waeup.aaue.applicants.interfaces import ICustomApplicantOnlinePayment |
---|
[7929] | 41 | |
---|
[10978] | 42 | ERROR_PART1 = ( |
---|
| 43 | 'PayeeName=N/A~' |
---|
| 44 | + 'Faculty=N/A~' |
---|
| 45 | + 'Department=N/A~' |
---|
| 46 | + 'Level=N/A~' |
---|
| 47 | + 'ProgrammeType=N/A~' |
---|
| 48 | + 'StudyType=N/A~' |
---|
| 49 | + 'Session=N/A~' |
---|
| 50 | + 'PayeeID=N/A~' |
---|
| 51 | + 'Amount=N/A~' |
---|
| 52 | + 'FeeStatus=') |
---|
| 53 | ERROR_PART2 = ( |
---|
| 54 | '~Semester=N/A~' |
---|
| 55 | + 'PaymentType=N/A~' |
---|
| 56 | + 'MatricNumber=N/A~' |
---|
| 57 | + 'Email=N/A~' |
---|
| 58 | + 'PhoneNumber=N/A') |
---|
| 59 | |
---|
[10032] | 60 | class CustomPaymentDataWebservice(PaymentDataWebservice): |
---|
| 61 | """A simple webservice to publish payment and payer details on request from |
---|
| 62 | accepted IP addresses without authentication. |
---|
[8746] | 63 | |
---|
[10032] | 64 | Etranzact is asking for the PAYEE_ID which is indeed misleading. |
---|
| 65 | These are not the data of the payee but of the payer. And it's |
---|
| 66 | not the id of the payer but of the payment. |
---|
| 67 | """ |
---|
| 68 | grok.name('feerequest') |
---|
[8769] | 69 | |
---|
[10032] | 70 | #ACCEPTED_IP = ('195.219.3.181', '195.219.3.184') |
---|
| 71 | ACCEPTED_IP = None |
---|
[8698] | 72 | |
---|
[10937] | 73 | def update(self, PAYEE_ID=None, PAYMENT_TYPE=None): |
---|
[9508] | 74 | if PAYEE_ID == None: |
---|
[10978] | 75 | self.output = ERROR_PART1 + 'Missing PAYEE_ID' + ERROR_PART2 |
---|
[9508] | 76 | return |
---|
[8746] | 77 | real_ip = self.request.get('HTTP_X_FORWARDED_FOR', None) |
---|
[8769] | 78 | # We can forego the logging once eTranzact payments run smoothly |
---|
| 79 | # and the accepted IP addresses are used. |
---|
| 80 | if real_ip: |
---|
[10032] | 81 | self.context.logger.info('PaymentDataWebservice called: %s' % real_ip) |
---|
| 82 | if real_ip and self.ACCEPTED_IP: |
---|
| 83 | if real_ip not in self.ACCEPTED_IP: |
---|
[10978] | 84 | self.output = ERROR_PART1 + 'Wrong IP address' + ERROR_PART2 |
---|
[8746] | 85 | return |
---|
[10937] | 86 | if PAYMENT_TYPE not in ('SCHOOL-FEE', 'ACCEPTANCE-FEE', 'APPLICATION-FEE'): |
---|
[10978] | 87 | self.output = ERROR_PART1 + 'Invalid PAYMENT_TYPE' + ERROR_PART2 |
---|
[10937] | 88 | return |
---|
[8754] | 89 | |
---|
| 90 | # It seems eTranzact sends a POST request with an empty body but the URL |
---|
| 91 | # contains a query string. So it's actually a GET request pretended |
---|
| 92 | # to be a POST request. Although this does not comply with the |
---|
| 93 | # RFC 2616 HTTP guidelines we may try to fetch the id from the QUERY_STRING |
---|
| 94 | # value of the request. |
---|
| 95 | #if PAYEE_ID is None: |
---|
| 96 | # try: |
---|
| 97 | # PAYEE_ID = self.request['QUERY_STRING'].split('=')[1] |
---|
| 98 | # except: |
---|
[10937] | 99 | # self.output = '-4' |
---|
[8754] | 100 | # return |
---|
| 101 | |
---|
[8704] | 102 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 103 | results = list(cat.searchResults(p_id=(PAYEE_ID, PAYEE_ID))) |
---|
| 104 | if len(results) != 1: |
---|
[10978] | 105 | self.output = ERROR_PART1 + 'Invalid PAYEE_ID' + ERROR_PART2 |
---|
[8746] | 106 | return |
---|
[10937] | 107 | if PAYMENT_TYPE == 'SCHOOL-FEE' \ |
---|
| 108 | and not results[0].p_category.startswith('schoolfee'): |
---|
[10978] | 109 | self.output = ERROR_PART1 + 'Wrong PAYMENT_TYPE' + ERROR_PART2 |
---|
[10937] | 110 | return |
---|
| 111 | if PAYMENT_TYPE == 'ACCEPTANCE-FEE' \ |
---|
| 112 | and not results[0].p_category == 'clearance': |
---|
[10978] | 113 | self.output = ERROR_PART1 + 'Wrong PAYMENT_TYPE' + ERROR_PART2 |
---|
[10937] | 114 | return |
---|
| 115 | if PAYMENT_TYPE == 'APPLICATION-FEE' \ |
---|
| 116 | and not results[0].p_category == 'application': |
---|
[10978] | 117 | self.output = ERROR_PART1 + 'Wrong PAYMENT_TYPE' + ERROR_PART2 |
---|
[10937] | 118 | return |
---|
[8746] | 119 | try: |
---|
[10032] | 120 | owner = IPayer(results[0]) |
---|
[8746] | 121 | full_name = owner.display_fullname |
---|
| 122 | matric_no = owner.id |
---|
| 123 | faculty = owner.faculty |
---|
| 124 | department = owner.department |
---|
[10907] | 125 | study_type = owner.current_mode |
---|
| 126 | email = owner.email |
---|
| 127 | phone = owner.phone |
---|
| 128 | level = owner.current_level |
---|
[8746] | 129 | except (TypeError, AttributeError): |
---|
[10978] | 130 | self.output = ERROR_PART1 + 'Unknown error' + ERROR_PART2 |
---|
[8746] | 131 | return |
---|
| 132 | amount = results[0].amount_auth |
---|
| 133 | payment_type = results[0].category |
---|
| 134 | programme_type = results[0].p_item |
---|
[10907] | 135 | |
---|
[8746] | 136 | academic_session = academic_sessions_vocab.getTerm( |
---|
| 137 | results[0].p_session).title |
---|
| 138 | status = results[0].p_state |
---|
| 139 | self.output = ( |
---|
[10907] | 140 | # Version 1 |
---|
| 141 | #'FULL_NAME=%s&' + |
---|
| 142 | #'FACULTY=%s&' + |
---|
| 143 | #'DEPARTMENT=%s&' + |
---|
| 144 | #'RETURN_TYPE=%s&' + |
---|
| 145 | #'PROGRAMME_TYPE=%s&' + |
---|
| 146 | #'PAYMENT_TYPE=%s&' + |
---|
| 147 | #'ACADEMIC_SESSION=%s&' + |
---|
| 148 | #'MATRIC_NO=%s&' + |
---|
| 149 | #'FEE_AMOUNT=%s&' + |
---|
| 150 | #'TRANSACTION_STATUS=%s' |
---|
| 151 | |
---|
| 152 | # Version 2 |
---|
| 153 | 'PayeeName=%s~' + |
---|
| 154 | 'Faculty=%s~' + |
---|
| 155 | 'Department=%s~' + |
---|
| 156 | 'Level=%s~' + |
---|
| 157 | 'ProgrammeType=%s~' + |
---|
| 158 | 'StudyType=%s~' + |
---|
| 159 | 'Session=%s~' + |
---|
| 160 | 'PayeeID=%s~' + |
---|
| 161 | 'Amount=%s~' + |
---|
| 162 | 'FeeStatus=%s~' + |
---|
[10932] | 163 | 'Semester=N/A~' + |
---|
[10907] | 164 | 'PaymentType=%s~' + |
---|
| 165 | 'MatricNumber=%s~' + |
---|
| 166 | 'Email=%s~' + |
---|
| 167 | 'PhoneNumber=%s' |
---|
| 168 | |
---|
| 169 | ) % (full_name, faculty, |
---|
| 170 | department, level, programme_type, study_type, |
---|
| 171 | academic_session, PAYEE_ID, amount, status, payment_type, |
---|
| 172 | matric_no, email, phone) |
---|
[8698] | 173 | return |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | # Requerying eTranzact payments |
---|
| 177 | |
---|
[10983] | 178 | TERMINAL_ID = '0570000070' |
---|
| 179 | QUERY_URL = 'https://www.etranzact.net/WebConnectPlus/query.jsp' |
---|
[8259] | 180 | |
---|
[8680] | 181 | # Test environment |
---|
[8776] | 182 | #QUERY_URL = 'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp' |
---|
| 183 | #TERMINAL_ID = '5009892289' |
---|
[8680] | 184 | |
---|
[8430] | 185 | def query_etranzact(confirmation_number, payment): |
---|
| 186 | |
---|
[8247] | 187 | postdict = {} |
---|
| 188 | postdict['TERMINAL_ID'] = TERMINAL_ID |
---|
| 189 | #postdict['RESPONSE_URL'] = 'http://dummy' |
---|
| 190 | postdict['CONFIRMATION_NO'] = confirmation_number |
---|
[10070] | 191 | data = urllib.urlencode(postdict) |
---|
[8682] | 192 | payment.conf_number = confirmation_number |
---|
[8247] | 193 | try: |
---|
[10069] | 194 | # eTranzact only accepts HTTP 1.1 requests. Therefore |
---|
| 195 | # the urllib2 package is required here. |
---|
| 196 | f = urllib2.urlopen(url=QUERY_URL, data=data) |
---|
[8247] | 197 | success = f.read() |
---|
[8432] | 198 | success = success.replace('\r\n','') |
---|
[9935] | 199 | if 'CUSTOMER_ID' not in success: |
---|
[8430] | 200 | msg = _('Invalid or unsuccessful callback: ${a}', |
---|
| 201 | mapping = {'a': success}) |
---|
| 202 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 203 | payment.p_state = 'failed' |
---|
[8430] | 204 | return False, msg, log |
---|
[8247] | 205 | success = success.replace('%20',' ').split('&') |
---|
| 206 | # We expect at least two parameters |
---|
| 207 | if len(success) < 2: |
---|
[8430] | 208 | msg = _('Invalid callback: ${a}', mapping = {'a': success}) |
---|
| 209 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 210 | payment.p_state = 'failed' |
---|
[8430] | 211 | return False, msg, log |
---|
[8247] | 212 | try: |
---|
| 213 | success_dict = dict([tuple(i.split('=')) for i in success]) |
---|
| 214 | except ValueError: |
---|
[8430] | 215 | msg = _('Invalid callback: ${a}', mapping = {'a': success}) |
---|
| 216 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 217 | payment.p_state = 'failed' |
---|
[8430] | 218 | return False, msg, log |
---|
[8247] | 219 | except IOError: |
---|
[8430] | 220 | msg = _('eTranzact IOError') |
---|
| 221 | log = 'eTranzact IOError' |
---|
| 222 | return False, msg, log |
---|
[8247] | 223 | payment.r_code = u'ET' |
---|
[9327] | 224 | payment.r_company = u'etranzact' |
---|
[8247] | 225 | payment.r_desc = u'%s' % success_dict.get('TRANS_DESCR') |
---|
| 226 | payment.r_amount_approved = float(success_dict.get('TRANS_AMOUNT',0.0)) |
---|
| 227 | payment.r_card_num = None |
---|
| 228 | payment.r_pay_reference = u'%s' % success_dict.get('RECEIPT_NO') |
---|
| 229 | if payment.r_amount_approved != payment.amount_auth: |
---|
[8430] | 230 | msg = _('Wrong amount') |
---|
| 231 | log = 'wrong callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 232 | payment.p_state = 'failed' |
---|
[8430] | 233 | return False, msg, log |
---|
[9935] | 234 | customer_id = success_dict.get('CUSTOMER_ID') |
---|
| 235 | if payment.p_id != customer_id: |
---|
[8717] | 236 | msg = _('Wrong payment id') |
---|
[8430] | 237 | log = 'wrong callback for payment %s: %s' % (payment.p_id, success) |
---|
[8247] | 238 | payment.p_state = 'failed' |
---|
[8430] | 239 | return False, msg, log |
---|
| 240 | log = 'valid callback for payment %s: %s' % (payment.p_id, success) |
---|
| 241 | msg = _('Successful callback received') |
---|
[8247] | 242 | payment.p_state = 'paid' |
---|
[8433] | 243 | payment.payment_date = datetime.utcnow() |
---|
[8430] | 244 | return True, msg, log |
---|
[8247] | 245 | |
---|
[8430] | 246 | class EtranzactEnterPinActionButtonApplicant(APABApplicant): |
---|
[8253] | 247 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8430] | 248 | grok.require('waeup.payApplicant') |
---|
[8259] | 249 | grok.order(3) |
---|
[7929] | 250 | icon = 'actionicon_call.png' |
---|
| 251 | text = _('Query eTranzact History') |
---|
[7976] | 252 | target = 'enterpin' |
---|
[7929] | 253 | |
---|
[8430] | 254 | class EtranzactEnterPinActionButtonStudent(APABStudent): |
---|
[8253] | 255 | grok.context(ICustomStudentOnlinePayment) |
---|
[8430] | 256 | grok.require('waeup.payStudent') |
---|
[8259] | 257 | grok.order(3) |
---|
[8247] | 258 | icon = 'actionicon_call.png' |
---|
| 259 | text = _('Query eTranzact History') |
---|
| 260 | target = 'enterpin' |
---|
| 261 | |
---|
| 262 | class EtranzactEnterPinPageStudent(KofaPage): |
---|
[7976] | 263 | """ |
---|
| 264 | """ |
---|
[8253] | 265 | grok.context(ICustomStudentOnlinePayment) |
---|
[7976] | 266 | grok.name('enterpin') |
---|
| 267 | grok.template('enterpin') |
---|
[7929] | 268 | grok.require('waeup.payStudent') |
---|
| 269 | |
---|
[7976] | 270 | buttonname = _('Submit to eTranzact') |
---|
| 271 | label = _('Requery eTranzact History') |
---|
| 272 | action = 'query_history' |
---|
[7929] | 273 | |
---|
[8247] | 274 | class EtranzactEnterPinPageApplicant(EtranzactEnterPinPageStudent): |
---|
| 275 | """ |
---|
| 276 | """ |
---|
| 277 | grok.require('waeup.payApplicant') |
---|
[8253] | 278 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8247] | 279 | |
---|
| 280 | class EtranzactQueryHistoryPageStudent(UtilityView, grok.View): |
---|
[7929] | 281 | """ Query history of eTranzact payments |
---|
| 282 | """ |
---|
[8253] | 283 | grok.context(ICustomStudentOnlinePayment) |
---|
[7929] | 284 | grok.name('query_history') |
---|
| 285 | grok.require('waeup.payStudent') |
---|
| 286 | |
---|
| 287 | def update(self, confirmation_number=None): |
---|
| 288 | if self.context.p_state == 'paid': |
---|
| 289 | self.flash(_('This ticket has already been paid.')) |
---|
| 290 | return |
---|
[8763] | 291 | student = self.context.student |
---|
[8430] | 292 | success, msg, log = query_etranzact(confirmation_number,self.context) |
---|
[8764] | 293 | student.writeLogMessage(self, log) |
---|
[8430] | 294 | if not success: |
---|
| 295 | self.flash(msg) |
---|
| 296 | return |
---|
| 297 | success, msg, log = self.context.doAfterStudentPayment() |
---|
| 298 | if log is not None: |
---|
[8764] | 299 | student.writeLogMessage(self, log) |
---|
[8430] | 300 | self.flash(msg) |
---|
[8247] | 301 | return |
---|
[7929] | 302 | |
---|
[8247] | 303 | def render(self): |
---|
| 304 | self.redirect(self.url(self.context, '@@index')) |
---|
| 305 | return |
---|
[7929] | 306 | |
---|
[8247] | 307 | class EtranzactQueryHistoryPageApplicant(UtilityView, grok.View): |
---|
| 308 | """ Query history of eTranzact payments |
---|
| 309 | """ |
---|
[8253] | 310 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8247] | 311 | grok.name('query_history') |
---|
| 312 | grok.require('waeup.payApplicant') |
---|
| 313 | |
---|
| 314 | def update(self, confirmation_number=None): |
---|
[8430] | 315 | ob_class = self.__implemented__.__name__ |
---|
[8247] | 316 | if self.context.p_state == 'paid': |
---|
| 317 | self.flash(_('This ticket has already been paid.')) |
---|
[7929] | 318 | return |
---|
[8247] | 319 | applicant = self.context.__parent__ |
---|
[8430] | 320 | success, msg, log = query_etranzact(confirmation_number,self.context) |
---|
[8769] | 321 | applicant.writeLogMessage(self, log) |
---|
[8430] | 322 | if not success: |
---|
| 323 | self.flash(msg) |
---|
| 324 | return |
---|
| 325 | success, msg, log = self.context.doAfterApplicantPayment() |
---|
| 326 | if log is not None: |
---|
[8769] | 327 | applicant.writeLogMessage(self, log) |
---|
[8430] | 328 | self.flash(msg) |
---|
[7929] | 329 | return |
---|
| 330 | |
---|
| 331 | def render(self): |
---|
| 332 | self.redirect(self.url(self.context, '@@index')) |
---|
[8259] | 333 | return |
---|
[9904] | 334 | |
---|
| 335 | # Disable Interswitch viewlets. This could be avoided by defining the |
---|
| 336 | # action button viewlets of kofacustom.nigeria.interswitch.browser in the |
---|
| 337 | # context of INigeriaStudentOnlinePayment or INigeriaApplicantOnlinePayment |
---|
| 338 | # respectively. But then all interswitch.browser modules have to be extended. |
---|
| 339 | |
---|
| 340 | class InterswitchActionButtonStudent(InterswitchActionButtonStudent): |
---|
| 341 | |
---|
| 342 | @property |
---|
| 343 | def target_url(self): |
---|
| 344 | return '' |
---|
| 345 | |
---|
| 346 | class InterswitchRequestWebserviceActionButtonStudent( |
---|
| 347 | InterswitchRequestWebserviceActionButtonStudent): |
---|
| 348 | |
---|
| 349 | @property |
---|
| 350 | def target_url(self): |
---|
| 351 | return '' |
---|
| 352 | |
---|
| 353 | class InterswitchActionButtonApplicant(InterswitchActionButtonApplicant): |
---|
| 354 | |
---|
| 355 | @property |
---|
| 356 | def target_url(self): |
---|
| 357 | return '' |
---|
| 358 | |
---|
| 359 | class InterswitchRequestWebserviceActionButtonApplicant( |
---|
| 360 | InterswitchRequestWebserviceActionButtonApplicant): |
---|
| 361 | |
---|
| 362 | @property |
---|
| 363 | def target_url(self): |
---|
| 364 | return '' |
---|