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

Last change on this file since 8652 was 8652, checked in by Henrik Bettermann, 12 years ago

Go live with correct terminal id.

  • Property svn:keywords set to Id
File size: 6.9 KB
RevLine 
[7929]1## $Id: browser.py 8652 2012-06-08 14:23:44Z 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
21from xml.dom.minidom import parseString
22import grok
23from waeup.kofa.browser.layout import KofaPage, UtilityView
[8430]24from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
25from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
[8444]26from waeup.aaue.interfaces import MessageFactory as _
27from waeup.aaue.students.interfaces import ICustomStudentOnlinePayment
28from waeup.aaue.applicants.interfaces import ICustomApplicantOnlinePayment
[7929]29
[8652]30TERMINAL_ID = '0690000120'
31QUERY_URL =   'https://www.etranzact.net/Query/queryPayoutletTransaction.jsp'
32#QUERY_URL =   'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp'
[8259]33
[8430]34def query_etranzact(confirmation_number, payment):
35   
[8247]36    postdict = {}
37    postdict['TERMINAL_ID'] = TERMINAL_ID
38    #postdict['RESPONSE_URL'] = 'http://dummy'
39    postdict['CONFIRMATION_NO'] = confirmation_number
40    data = urllib.urlencode(postdict)
41    try:
42        f = urllib.urlopen(url=QUERY_URL, data=data)
43        success = f.read()
[8432]44        success = success.replace('\r\n','')
[8247]45        if 'COL1' not in success:
[8430]46            msg = _('Invalid or unsuccessful callback: ${a}',
47                mapping = {'a': success})
48            log = 'invalid callback for payment %s: %s' % (payment.p_id, success)
[8247]49            payment.p_state = 'failed'
[8430]50            return False, msg, log
[8247]51        success = success.replace('%20',' ').split('&')
52        # We expect at least two parameters
53        if len(success) < 2:
[8430]54            msg = _('Invalid callback: ${a}', mapping = {'a': success})
55            log = 'invalid callback for payment %s: %s' % (payment.p_id, success)
[8247]56            payment.p_state = 'failed'
[8430]57            return False, msg, log
[8247]58        try:
59            success_dict = dict([tuple(i.split('=')) for i in success])
60        except ValueError:
[8430]61            msg = _('Invalid callback: ${a}', mapping = {'a': success})
62            log = 'invalid callback for payment %s: %s' % (payment.p_id, success)
[8247]63            payment.p_state = 'failed'
[8430]64            return False, msg, log
[8247]65    except IOError:
[8430]66        msg = _('eTranzact IOError')
67        log = 'eTranzact IOError'
68        return False, msg, log
[8247]69    payment.r_code = u'ET'
70    payment.r_desc = u'%s' % success_dict.get('TRANS_DESCR')
71    payment.r_amount_approved = float(success_dict.get('TRANS_AMOUNT',0.0))
72    payment.r_card_num = None
73    payment.r_pay_reference = u'%s' % success_dict.get('RECEIPT_NO')
74    if payment.r_amount_approved != payment.amount_auth:
[8430]75        msg = _('Wrong amount')
76        log = 'wrong callback for payment %s: %s' % (payment.p_id, success)
[8247]77        payment.p_state = 'failed'
[8430]78        return False, msg, log
[8247]79    tcode = payment.p_id
80    tcode = tcode[len(tcode)-8:len(tcode)]
81    col1 = success_dict.get('COL1')
82    col1 = col1[len(col1)-8:len(col1)]
83    if tcode != col1:
[8430]84        msg = _('Wrong transaction code')
85        log = 'wrong callback for payment %s: %s' % (payment.p_id, success)
[8247]86        payment.p_state = 'failed'
[8430]87        return False, msg, log
88    log = 'valid callback for payment %s: %s' % (payment.p_id, success)
89    msg = _('Successful callback received')
[8247]90    payment.p_state = 'paid'
[8433]91    payment.payment_date = datetime.utcnow()
[8430]92    return True, msg, log
[8247]93
[8430]94class EtranzactEnterPinActionButtonApplicant(APABApplicant):
[8253]95    grok.context(ICustomApplicantOnlinePayment)
[8430]96    grok.require('waeup.payApplicant')
[8259]97    grok.order(3)
[7929]98    icon = 'actionicon_call.png'
99    text = _('Query eTranzact History')
[7976]100    target = 'enterpin'
[7929]101
[8430]102class EtranzactEnterPinActionButtonStudent(APABStudent):
[8253]103    grok.context(ICustomStudentOnlinePayment)
[8430]104    grok.require('waeup.payStudent')
[8259]105    grok.order(3)
[8247]106    icon = 'actionicon_call.png'
107    text = _('Query eTranzact History')
108    target = 'enterpin'
109
110class EtranzactEnterPinPageStudent(KofaPage):
[7976]111    """
112    """
[8253]113    grok.context(ICustomStudentOnlinePayment)
[7976]114    grok.name('enterpin')
115    grok.template('enterpin')
[7929]116    grok.require('waeup.payStudent')
117
[7976]118    buttonname = _('Submit to eTranzact')
119    label = _('Requery eTranzact History')
120    action = 'query_history'
[7929]121
[8247]122class EtranzactEnterPinPageApplicant(EtranzactEnterPinPageStudent):
123    """
124    """
125    grok.require('waeup.payApplicant')
[8253]126    grok.context(ICustomApplicantOnlinePayment)
[8247]127
128class EtranzactQueryHistoryPageStudent(UtilityView, grok.View):
[7929]129    """ Query history of eTranzact payments
130    """
[8253]131    grok.context(ICustomStudentOnlinePayment)
[7929]132    grok.name('query_history')
133    grok.require('waeup.payStudent')
134
135    def update(self, confirmation_number=None):
[8430]136        ob_class = self.__implemented__.__name__
[7929]137        if self.context.p_state == 'paid':
138            self.flash(_('This ticket has already been paid.'))
139            return
140        student = self.context.getStudent()
[8430]141        success, msg, log = query_etranzact(confirmation_number,self.context)
142        student.loggerInfo(ob_class, log)
143        if not success:
144            self.flash(msg)
145            return
146        success, msg, log = self.context.doAfterStudentPayment()
147        if log is not None:
148            student.loggerInfo(ob_class, log)
149        self.flash(msg)
[8247]150        return
[7929]151
[8247]152    def render(self):
153        self.redirect(self.url(self.context, '@@index'))
154        return
[7929]155
[8247]156class EtranzactQueryHistoryPageApplicant(UtilityView, grok.View):
157    """ Query history of eTranzact payments
158    """
[8253]159    grok.context(ICustomApplicantOnlinePayment)
[8247]160    grok.name('query_history')
161    grok.require('waeup.payApplicant')
162
163    def update(self, confirmation_number=None):
[8430]164        ob_class = self.__implemented__.__name__
[8247]165        if self.context.p_state == 'paid':
166            self.flash(_('This ticket has already been paid.'))
[7929]167            return
[8247]168        applicant = self.context.__parent__
[8430]169        success, msg, log = query_etranzact(confirmation_number,self.context)
170        applicant.loggerInfo(ob_class, log)
171        if not success:
172            self.flash(msg)
173            return
174        success, msg, log = self.context.doAfterApplicantPayment()
175        if log is not None:
176            applicant.loggerInfo(ob_class, log)
177        self.flash(msg)
[7929]178        return
179
180    def render(self):
181        self.redirect(self.url(self.context, '@@index'))
[8259]182        return
Note: See TracBrowser for help on using the repository browser.