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

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

Optimize gataway tests. We can't test more.

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