source: main/waeup.aaue/trunk/src/waeup/aaue/etranzact/browser.py @ 14463

Last change on this file since 14463 was 14376, checked in by Henrik Bettermann, 8 years ago

Configure Interswitch split payment and eTrabzact payment type.

  • Property svn:keywords set to Id
File size: 18.1 KB
Line 
1## $Id: browser.py 14376 2017-01-07 09:03:33Z 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##
18from datetime import datetime
19import httplib
20import urllib
21import urllib2
22import re
23from xml.dom.minidom import parseString
24import grok
25from zope.component import getUtility
26from zope.catalog.interfaces import ICatalog
27from waeup.kofa.interfaces import IUniversity, CLEARED
28from waeup.kofa.payments.interfaces import IPayer
29from waeup.kofa.webservices import PaymentDataWebservice
30from waeup.kofa.browser.layout import KofaPage, UtilityView
31from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
32from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
33from waeup.aaue.interfaces import academic_sessions_vocab
34from kofacustom.nigeria.interswitch.browser import (
35    InterswitchActionButtonStudent,
36    InterswitchRequestWebserviceActionButtonStudent,
37    InterswitchActionButtonApplicant,
38    InterswitchRequestWebserviceActionButtonApplicant)
39from waeup.aaue.interfaces import MessageFactory as _
40from waeup.aaue.students.interfaces import ICustomStudentOnlinePayment
41from waeup.aaue.applicants.interfaces import ICustomApplicantOnlinePayment
42
43ERROR_PART1 = (
44        'PayeeName=N/A~'
45        + 'Faculty=N/A~'
46        + 'Department=N/A~'
47        + 'Level=N/A~'
48        + 'ProgrammeType=N/A~'
49        + 'StudyType=N/A~'
50        + 'Session=N/A~'
51        + 'PayeeID=N/A~'
52        + 'Amount=N/A~'
53        + 'FeeStatus=')
54ERROR_PART2 = (
55        '~Semester=N/A~'
56        + 'PaymentType=N/A~'
57        + 'MatricNumber=N/A~'
58        + 'Email=N/A~'
59        + 'PhoneNumber=N/A')
60
61class CustomPaymentDataWebservice(PaymentDataWebservice):
62    """A simple webservice to publish payment and payer details on request from
63    accepted IP addresses without authentication.
64
65    Etranzact is asking for the PAYEE_ID which is indeed misleading.
66    These are not the data of the payee but of the payer. And it's
67    not the id of the payer but of the payment.
68    """
69    grok.name('feerequest')
70
71    #ACCEPTED_IP = ('195.219.3.181', '195.219.3.184')
72    ACCEPTED_IP = None
73
74    def update(self, PAYEE_ID=None, PAYMENT_TYPE=None):
75        if PAYEE_ID == None:
76            self.output = ERROR_PART1 + 'Missing PAYEE_ID' + ERROR_PART2
77            return
78        real_ip = self.request.get('HTTP_X_FORWARDED_FOR', None)
79        # We can forego the logging once eTranzact payments run smoothly
80        # and the accepted IP addresses are used.
81        if real_ip:
82            self.context.logger.info('PaymentDataWebservice called: %s' % real_ip)
83        if real_ip  and self.ACCEPTED_IP:
84            if real_ip not in  self.ACCEPTED_IP:
85                self.output = ERROR_PART1 + 'Wrong IP address' + ERROR_PART2
86                return
87        category_mapping = {
88            'SCHOOL-FEE-NEW': ('schoolfee',),
89            'SCHOOL-FEE-RETURNING': ('schoolfee',),
90            'SCHOOL-FEE-PLUS-NEW': ('schoolfee_incl',),
91            'SCHOOL-FEE-PLUS-RETURNING': ('schoolfee_incl',),
92            'SCHOOL-FEE-PG-NEW': ('schoolfee',),
93            'SCHOOL-FEE-PG-RETURNING': ('schoolfee',),
94            'SCHOOL-FEE-FIRST-INSTALMENT-PLUS': ('schoolfee_1',),
95            'SCHOOL-FEE-SECOND-INSTALMENT': ('schoolfee_2',),
96            'SCHOOL-FEE-BALANCE': ('schoolfee','schoolfee_incl',
97                                   'schoolfee_1','schoolfee_2'),
98            'SCHOOL-FEE-PT-NEW': ('schoolfee',),
99            'SCHOOL-FEE-PT-RETURNING': ('schoolfee',),
100            'SCHOOL-FEE-PT-PLUS-NEW': ('schoolfee_incl',),
101            'SCHOOL-FEE-PT-PLUS-RETURNING': ('schoolfee_incl',),
102            'SCHOOL-FEE-PT-PG-NEW': ('schoolfee',),
103            'SCHOOL-FEE-PT-PG-RETURNING': ('schoolfee',),
104            'SCHOOL-FEE-PT-FIRST-INSTALMENT-PLUS': ('schoolfee_1',),
105            'SCHOOL-FEE-PT-SECOND-INSTALMENT': ('schoolfee_2',),
106            'SCHOOL-FEE-PT-BALANCE': ('schoolfee','schoolfee_incl',
107                                      'schoolfee_1','schoolfee_2'),
108            'SCHOOL-FEE-FP-NEW': ('schoolfee',),
109
110            'ACCEPTANCE-FEE': ('clearance',),
111            'ACCEPTANCE-FEE-PLUS': ('clearance_incl',),
112            'ACCEPTANCE-FEE-PG': ('clearance',),
113            'ACCEPTANCE-FEE-PT': ('clearance',),
114            'ACCEPTANCE-FEE-PT-PLUS': ('clearance_incl',),
115            'ACCEPTANCE-FEE-PT-PG': ('clearance',),
116            'ACCEPTANCE-FEE-FP': ('clearance',),
117
118            'APPLICATION-FEE': ('application',),
119            'APPLICATION-FEE-PT': ('application',),
120            'APPLICATION-FEE-FP': ('application',),
121
122            'LATE-REGISTRATION': ('late_registration',),
123            'LATE-REGISTRATION-PT': ('late_registration',),
124
125            'AAU-STUDENT-WELFARE-ASSURANCE': ('welfare',),
126            'AAU-STUDENT-WELFARE-ASSURANCE-PT': ('welfare',),
127
128            'AAU-STUDENT-ID_CARD': ('id_card',),
129
130            'HOSTEL-ACCOMMODATION-FEE': ('hostel_maintenance',),
131            'HOSTEL-ACCOMMODATION-FEE-PT': ('hostel_maintenance',),
132
133            'AAU-LAPEL-FILE-FEE': ('lapel',),
134            'AAU-LAPEL-FILE-FEE-PT': ('lapel',),
135
136            'MATRICULATION-GOWN-FEE': ('matric_gown',),
137            'MATRICULATION-GOWN-FEE-PT': ('matric_gown',),
138
139            'CONCESSIONAL-FEE': ('concessional',),
140            'CONCESSIONAL-FEE-PT': ('concessional',),
141
142            'STUDENTS-UNION-DUES': ('union',),
143            'STUDENTS-UNION-DUES-PT': ('union',),
144
145            'RESTITUTION-FEE': ('restitution',),
146            }
147
148        if PAYMENT_TYPE not in category_mapping.keys():
149            self.output = ERROR_PART1 + 'Invalid PAYMENT_TYPE' + ERROR_PART2
150            return
151
152        # It seems eTranzact sends a POST request with an empty body but the URL
153        # contains a query string. So it's actually a GET request pretended
154        # to be a POST request. Although this does not comply with the
155        # RFC 2616 HTTP guidelines we may try to fetch the id from the QUERY_STRING
156        # value of the request.
157        #if PAYEE_ID is None:
158        #    try:
159        #        PAYEE_ID = self.request['QUERY_STRING'].split('=')[1]
160        #    except:
161        #        self.output = '-4'
162        #        return
163
164        cat = getUtility(ICatalog, name='payments_catalog')
165        results = list(cat.searchResults(p_id=(PAYEE_ID, PAYEE_ID)))
166        if len(results) != 1:
167            self.output = ERROR_PART1 + 'Invalid PAYEE_ID' + ERROR_PART2
168            return
169        student = getattr(results[0], 'student', None)
170        amount = results[0].amount_auth
171        payment_type = results[0].category
172        p_category = results[0].p_category
173        programme_type = results[0].p_item
174        if not programme_type:
175            programme_type = 'N/A'
176        academic_session = academic_sessions_vocab.getTerm(
177            results[0].p_session).title
178        status = results[0].p_state
179
180        if status == 'paid':
181            self.output = ERROR_PART1 + 'PAYEE_ID already used' + ERROR_PART2
182            return
183        if p_category not in category_mapping[PAYMENT_TYPE]:
184            self.output = ERROR_PART1 + 'Wrong PAYMENT_TYPE' + ERROR_PART2
185            return
186        if student and PAYMENT_TYPE.endswith('-RETURNING') \
187            and student.state == CLEARED:
188            self.output = ERROR_PART1 + 'Not a returning student' + ERROR_PART2
189            return
190        if student and PAYMENT_TYPE.endswith('-NEW') \
191            and student.state != CLEARED:
192            self.output = ERROR_PART1 + 'Not a new student' + ERROR_PART2
193            return
194        if student and '-PG' in PAYMENT_TYPE and not student.is_postgrad:
195            self.output = ERROR_PART1 + 'Not a postgrad student' + ERROR_PART2
196            return
197        if student and '-PG' not in PAYMENT_TYPE and student.is_postgrad \
198            and results[0].p_item != 'Balance':
199            self.output = ERROR_PART1 + 'Postgrad student' + ERROR_PART2
200            return
201        if student and '-PT' in PAYMENT_TYPE \
202            and not student.current_mode.endswith('_pt'):
203            self.output = ERROR_PART1 + 'Not a part-time student' + ERROR_PART2
204            return
205        if student and '-PT' not in PAYMENT_TYPE \
206            and student.current_mode.endswith('_pt'):
207            self.output = ERROR_PART1 + 'Part-time student' + ERROR_PART2
208            return
209        if student and '-FP' in PAYMENT_TYPE and student.current_mode != 'found':
210            self.output = ERROR_PART1 + 'Not a foundation programme student' + ERROR_PART2
211            return
212        if student and '-FP' not in PAYMENT_TYPE and student.current_mode == 'found':
213            self.output = ERROR_PART1 + 'Foundation programme student' + ERROR_PART2
214            return
215        if '-BALANCE' in PAYMENT_TYPE and results[0].p_item != 'Balance':
216            self.output = ERROR_PART1 + 'Not a balance payment' + ERROR_PART2
217            return
218        if not '-BALANCE' in PAYMENT_TYPE and results[0].p_item == 'Balance':
219            self.output = ERROR_PART1 + 'Balance payment' + ERROR_PART2
220            return
221
222        try:
223            owner = IPayer(results[0])
224            full_name = owner.display_fullname
225            matric_no = owner.id
226            faculty = owner.faculty
227            department = owner.department
228            study_type = owner.current_mode
229            email = owner.email
230            phone = owner.phone
231            level = owner.current_level
232        except (TypeError, AttributeError):
233            self.output = ERROR_PART1 +  'Unknown error' + ERROR_PART2
234            return
235        self.output = (
236            'PayeeName=%s~' +
237            'Faculty=%s~' +
238            'Department=%s~' +
239            'Level=%s~' +
240            'ProgrammeType=%s~' +
241            'StudyType=%s~' +
242            'Session=%s~' +
243            'PayeeID=%s~' +
244            'Amount=%s~' +
245            'FeeStatus=%s~' +
246            'Semester=N/A~' +
247            'PaymentType=%s~' +
248            'MatricNumber=%s~' +
249            'Email=%s~' +
250            'PhoneNumber=%s'
251
252            ) % (full_name, faculty,
253            department, level, programme_type, study_type,
254            academic_session, PAYEE_ID, amount, status, payment_type,
255            matric_no, email, phone)
256        return
257
258
259# Requerying eTranzact payments
260
261TERMINAL_ID = '0570000070'
262QUERY_URL =   'https://www.etranzact.net/WebConnectPlus/query.jsp'
263
264# Test environment
265#QUERY_URL =   'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp'
266#TERMINAL_ID = '5009892289'
267
268def query_etranzact(confirmation_number, payment):
269   
270    postdict = {}
271    postdict['TERMINAL_ID'] = TERMINAL_ID
272    #postdict['RESPONSE_URL'] = 'http://dummy'
273    postdict['CONFIRMATION_NO'] = confirmation_number
274    data = urllib.urlencode(postdict)
275    payment.conf_number = confirmation_number
276    try:
277        # eTranzact only accepts HTTP 1.1 requests. Therefore
278        # the urllib2 package is required here.
279        f = urllib2.urlopen(url=QUERY_URL, data=data)
280        success = f.read()
281        success = success.replace('\r\n','')
282        # eTranzact sends strange HTML tags which must be removed.
283        success = re.sub("<.*?>", "", success)
284        if 'CUSTOMER_ID' not in success:
285            msg = _('Invalid or unsuccessful callback: ${a}',
286                mapping = {'a': success})
287            log = 'invalid callback for payment %s: %s' % (payment.p_id, success)
288            payment.p_state = 'failed'
289            return False, msg, log
290        success = success.replace('%20',' ').split('&')
291        # We expect at least two parameters
292        if len(success) < 2:
293            msg = _('Invalid callback: ${a}', mapping = {'a': success})
294            log = 'invalid callback for payment %s: %s' % (payment.p_id, success)
295            payment.p_state = 'failed'
296            return False, msg, log
297        try:
298            success_dict = dict([tuple(i.split('=')) for i in success])
299        except ValueError:
300            msg = _('Invalid callback: ${a}', mapping = {'a': success})
301            log = 'invalid callback for payment %s: %s' % (payment.p_id, success)
302            payment.p_state = 'failed'
303            return False, msg, log
304    except IOError:
305        msg = _('eTranzact IOError')
306        log = 'eTranzact IOError'
307        return False, msg, log
308    payment.r_code = u'ET'
309    payment.r_company = u'etranzact'
310    payment.r_desc = u'%s' % success_dict.get('TRANS_DESCR')
311    payment.r_amount_approved = float(success_dict.get('TRANS_AMOUNT',0.0))
312    payment.r_card_num = None
313    payment.r_pay_reference = u'%s' % success_dict.get('RECEIPT_NO')
314    if payment.r_amount_approved != payment.amount_auth:
315        msg = _('Wrong amount')
316        log = 'wrong callback for payment %s: %s' % (payment.p_id, success)
317        payment.p_state = 'failed'
318        return False, msg, log
319    customer_id = success_dict.get('CUSTOMER_ID')
320    if payment.p_id != customer_id:
321        msg = _('Wrong payment id')
322        log = 'wrong callback for payment %s: %s' % (payment.p_id, success)
323        payment.p_state = 'failed'
324        return False, msg, log
325    log = 'valid callback for payment %s: %s' % (payment.p_id, success)
326    msg = _('Successful callback received')
327    payment.p_state = 'paid'
328    payment.payment_date = datetime.utcnow()
329    return True, msg, log
330
331class EtranzactEnterPinActionButtonApplicant(APABApplicant):
332    grok.context(ICustomApplicantOnlinePayment)
333    grok.require('waeup.payApplicant')
334    grok.order(3)
335    icon = 'actionicon_call.png'
336    text = _('Query eTranzact History')
337    target = 'enterpin'
338
339class EtranzactEnterPinActionButtonStudent(APABStudent):
340    grok.context(ICustomStudentOnlinePayment)
341    grok.require('waeup.payStudent')
342    grok.order(3)
343    icon = 'actionicon_call.png'
344    text = _('Query eTranzact History')
345    target = 'enterpin'
346
347class EtranzactEnterPinPageStudent(KofaPage):
348    """
349    """
350    grok.context(ICustomStudentOnlinePayment)
351    grok.name('enterpin')
352    grok.template('enterpin')
353    grok.require('waeup.payStudent')
354
355    buttonname = _('Submit to eTranzact')
356    label = _('Requery eTranzact History')
357    action = 'query_history'
358    placeholder = _('Confirmation Number (PIN)')
359
360    def update(self):
361        super(EtranzactEnterPinPageStudent, self).update()
362        if not self.context.p_category.startswith('schoolfee'):
363            return
364        student = self.context.student
365        if student.state != CLEARED:
366            return
367        if student.entry_session < 2013:
368            return
369        for ticket in student['payments'].values():
370            if ticket.p_state == 'paid' and \
371                ticket.p_category.startswith('clearance'):
372                return
373        self.flash(_('Please pay acceptance fee first.'), type="danger")
374        self.redirect(self.url(self.context, '@@index'))
375        return
376
377class EtranzactEnterPinPageApplicant(EtranzactEnterPinPageStudent):
378    """
379    """
380    grok.require('waeup.payApplicant')
381    grok.context(ICustomApplicantOnlinePayment)
382
383class EtranzactQueryHistoryPageStudent(UtilityView, grok.View):
384    """ Query history of eTranzact payments
385    """
386    grok.context(ICustomStudentOnlinePayment)
387    grok.name('query_history')
388    grok.require('waeup.payStudent')
389
390    def update(self, confirmation_number=None):
391        if self.context.p_state == 'paid':
392            self.flash(_('This ticket has already been paid.'))
393            return
394        student = self.context.student
395        success, msg, log = query_etranzact(confirmation_number,self.context)
396        student.writeLogMessage(self, log)
397        if not success:
398            self.flash(msg)
399            return
400        flashtype, msg, log = self.context.doAfterStudentPayment()
401        if log is not None:
402            student.writeLogMessage(self, log)
403        self.flash(msg, type=flashtype)
404        return
405
406    def render(self):
407        self.redirect(self.url(self.context, '@@index'))
408        return
409
410class EtranzactQueryHistoryPageApplicant(UtilityView, grok.View):
411    """ Query history of eTranzact payments
412    """
413    grok.context(ICustomApplicantOnlinePayment)
414    grok.name('query_history')
415    grok.require('waeup.payApplicant')
416
417    def update(self, confirmation_number=None):
418        ob_class = self.__implemented__.__name__
419        if self.context.p_state == 'paid':
420            self.flash(_('This ticket has already been paid.'))
421            return
422        applicant = self.context.__parent__
423        success, msg, log = query_etranzact(confirmation_number,self.context)
424        applicant.writeLogMessage(self, log)
425        if not success:
426            self.flash(msg)
427            return
428        flashtype, msg, log = self.context.doAfterApplicantPayment()
429        if log is not None:
430            applicant.writeLogMessage(self, log)
431        self.flash(msg, type=flashtype)
432        return
433
434    def render(self):
435        self.redirect(self.url(self.context, '@@index'))
436        return
437
438# Disable Interswitch viewlets. This could be avoided by defining the
439# action button viewlets of kofacustom.nigeria.interswitch.browser in the
440# context of INigeriaStudentOnlinePayment or INigeriaApplicantOnlinePayment
441# respectively. But then all interswitch.browser modules have to be extended.
442
443#class InterswitchActionButtonStudent(InterswitchActionButtonStudent):
444
445#    @property
446#    def target_url(self):
447#        return ''
448
449#class InterswitchRequestWebserviceActionButtonStudent(
450#    InterswitchRequestWebserviceActionButtonStudent):
451
452#    @property
453#    def target_url(self):
454#        return ''
455
456#class InterswitchActionButtonApplicant(InterswitchActionButtonApplicant):
457
458#    @property
459#    def target_url(self):
460#        return ''
461
462#class InterswitchRequestWebserviceActionButtonApplicant(
463#    InterswitchRequestWebserviceActionButtonApplicant):
464
465#    @property
466#    def target_url(self):
467#        return ''
Note: See TracBrowser for help on using the repository browser.