source: main/waeup.custom/trunk/src/waeup/custom/etranzact/browser.py @ 7930

Last change on this file since 7930 was 7929, checked in by Henrik Bettermann, 13 years ago

Add package for eTranzact payments (query history only).

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1## $Id: browser.py 7929 2012-03-20 15:21:01Z 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
24from waeup.kofa.students.interfaces import IStudentOnlinePayment
25from waeup.kofa.students.browser import write_log_message
26from waeup.kofa.students.viewlets import RequestCallbackActionButton
27from waeup.custom.interfaces import MessageFactory as _
28from waeup.custom.utils.utils import actions_after_payment
29
30TERMINAL_ID = '0000000001'
31QUERY_URL =   'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp'
32
33class EtranzactEnterPinActionButton(RequestCallbackActionButton):
34    grok.order(5)
35    icon = 'actionicon_call.png'
36    text = _('Query eTranzact History')
37    target = 'enter_pin'
38
39class EtranzactEnterPinPage(KofaPage):
40    grok.context(IStudentOnlinePayment)
41    grok.name('enter_pin')
42    grok.template('enter_pin')
43    grok.require('waeup.payStudent')
44
45
46class EtranzactQueryHistoryPage(UtilityView, grok.View):
47    """ Query history of eTranzact payments
48    """
49    grok.context(IStudentOnlinePayment)
50    grok.name('query_history')
51    grok.require('waeup.payStudent')
52
53    def update(self, confirmation_number=None):
54        if self.context.p_state == 'paid':
55            self.flash(_('This ticket has already been paid.'))
56            return
57        student = self.context.getStudent()
58        postdict = {}
59        postdict['TERMINAL_ID'] = TERMINAL_ID
60        #postdict['RESPONSE_URL'] = 'http://dummy'
61        postdict['CONFIRMATION_NO'] = confirmation_number
62        data = urllib.urlencode(postdict)
63        try:
64            f = urllib.urlopen(url=QUERY_URL, data=data)
65            success = f.read()
66            if '-1' in success:
67                self.flash(_('Unsuccessful callback'))
68                write_log_message(self,'unsuccessful callback: %s' % self.context.p_id)
69                self.context.p_state = 'failed'
70                return
71            success = success.replace('%20',' ').split('&')
72            # We expect at least two parameters
73            if len(success) < 2:
74                self.flash(_('Invalid callback: ${a}',
75                    mapping = {'a': success}))
76                write_log_message(self,'invalid callback: %s' % self.context.p_id)
77                self.context.p_state = 'failed'
78                return
79            try:
80                success_dict = dict([tuple(i.split('=')) for i in success])
81            except ValueError:
82                self.flash(_('Invalid callback: ${a}',
83                    mapping = {'a': success}))
84                write_log_message(self,'invalid callback: %s' % self.context.p_id)
85                self.context.p_state = 'failed'
86                return
87        except IOError:
88            self.flash(_('eTranzact IOError'))
89            return
90        write_log_message(self,'callback received: %s' % success)
91
92        self.context.r_code = u'ET'
93        self.context.r_desc = u'%s' % success_dict.get('TRANS_DESCR')
94        self.context.r_amount_approved = float(success_dict.get('TRANS_AMOUNT',0.0))
95        self.context.r_card_num = None
96        self.context.r_pay_reference = u'%s' % success_dict.get('RECEIPT_NO')
97       
98        if self.context.r_amount_approved != self.context.amount_auth:
99            self.flash(_('Wrong amount'))
100            write_log_message(
101                self,'successful callback but wrong amount: %s'
102                % self.context.p_id)
103            self.context.p_state = 'failed'
104            return
105
106        if success_dict.get('PAYMENT_CODE') != self.context.p_id:
107            self.flash(_('Wrong transaction id'))
108            write_log_message(
109                self,'successful callback but wrong transaction id: %s'
110                % self.context.p_id)
111            self.context.p_state = 'failed'
112            return
113
114        write_log_message(self,'successful callback: %s' % self.context.p_id)
115
116        self.context.p_state = 'paid'
117        self.context.payment_date = datetime.now()
118
119        actions_after_payment(student, self.context, self)
120
121        return
122
123    def render(self):
124        self.redirect(self.url(self.context, '@@index'))
125        return
Note: See TracBrowser for help on using the repository browser.