source: main/waeup.uniben/trunk/src/waeup/uniben/etranzact/browser.py @ 8067

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

Rename custom to uniben.

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