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 | ## |
---|
18 | from datetime import datetime |
---|
19 | import httplib |
---|
20 | import urllib |
---|
21 | from xml.dom.minidom import parseString |
---|
22 | import grok |
---|
23 | from waeup.kofa.browser.layout import KofaPage, UtilityView |
---|
24 | from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent |
---|
25 | from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant |
---|
26 | from waeup.aaue.interfaces import MessageFactory as _ |
---|
27 | from waeup.aaue.students.interfaces import ICustomStudentOnlinePayment |
---|
28 | from waeup.aaue.applicants.interfaces import ICustomApplicantOnlinePayment |
---|
29 | |
---|
30 | TERMINAL_ID = '0690000120' |
---|
31 | QUERY_URL = 'https://www.etranzact.net/Query/queryPayoutletTransaction.jsp' |
---|
32 | #QUERY_URL = 'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp' |
---|
33 | |
---|
34 | def query_etranzact(confirmation_number, payment): |
---|
35 | |
---|
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() |
---|
44 | success = success.replace('\r\n','') |
---|
45 | if 'COL1' not in success: |
---|
46 | msg = _('Invalid or unsuccessful callback: ${a}', |
---|
47 | mapping = {'a': success}) |
---|
48 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
49 | payment.p_state = 'failed' |
---|
50 | return False, msg, log |
---|
51 | success = success.replace('%20',' ').split('&') |
---|
52 | # We expect at least two parameters |
---|
53 | if len(success) < 2: |
---|
54 | msg = _('Invalid callback: ${a}', mapping = {'a': success}) |
---|
55 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
56 | payment.p_state = 'failed' |
---|
57 | return False, msg, log |
---|
58 | try: |
---|
59 | success_dict = dict([tuple(i.split('=')) for i in success]) |
---|
60 | except ValueError: |
---|
61 | msg = _('Invalid callback: ${a}', mapping = {'a': success}) |
---|
62 | log = 'invalid callback for payment %s: %s' % (payment.p_id, success) |
---|
63 | payment.p_state = 'failed' |
---|
64 | return False, msg, log |
---|
65 | except IOError: |
---|
66 | msg = _('eTranzact IOError') |
---|
67 | log = 'eTranzact IOError' |
---|
68 | return False, msg, log |
---|
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: |
---|
75 | msg = _('Wrong amount') |
---|
76 | log = 'wrong callback for payment %s: %s' % (payment.p_id, success) |
---|
77 | payment.p_state = 'failed' |
---|
78 | return False, msg, log |
---|
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: |
---|
84 | msg = _('Wrong transaction code') |
---|
85 | log = 'wrong callback for payment %s: %s' % (payment.p_id, success) |
---|
86 | payment.p_state = 'failed' |
---|
87 | return False, msg, log |
---|
88 | log = 'valid callback for payment %s: %s' % (payment.p_id, success) |
---|
89 | msg = _('Successful callback received') |
---|
90 | payment.p_state = 'paid' |
---|
91 | payment.payment_date = datetime.utcnow() |
---|
92 | return True, msg, log |
---|
93 | |
---|
94 | class EtranzactEnterPinActionButtonApplicant(APABApplicant): |
---|
95 | grok.context(ICustomApplicantOnlinePayment) |
---|
96 | grok.require('waeup.payApplicant') |
---|
97 | grok.order(3) |
---|
98 | icon = 'actionicon_call.png' |
---|
99 | text = _('Query eTranzact History') |
---|
100 | target = 'enterpin' |
---|
101 | |
---|
102 | class EtranzactEnterPinActionButtonStudent(APABStudent): |
---|
103 | grok.context(ICustomStudentOnlinePayment) |
---|
104 | grok.require('waeup.payStudent') |
---|
105 | grok.order(3) |
---|
106 | icon = 'actionicon_call.png' |
---|
107 | text = _('Query eTranzact History') |
---|
108 | target = 'enterpin' |
---|
109 | |
---|
110 | class EtranzactEnterPinPageStudent(KofaPage): |
---|
111 | """ |
---|
112 | """ |
---|
113 | grok.context(ICustomStudentOnlinePayment) |
---|
114 | grok.name('enterpin') |
---|
115 | grok.template('enterpin') |
---|
116 | grok.require('waeup.payStudent') |
---|
117 | |
---|
118 | buttonname = _('Submit to eTranzact') |
---|
119 | label = _('Requery eTranzact History') |
---|
120 | action = 'query_history' |
---|
121 | |
---|
122 | class EtranzactEnterPinPageApplicant(EtranzactEnterPinPageStudent): |
---|
123 | """ |
---|
124 | """ |
---|
125 | grok.require('waeup.payApplicant') |
---|
126 | grok.context(ICustomApplicantOnlinePayment) |
---|
127 | |
---|
128 | class EtranzactQueryHistoryPageStudent(UtilityView, grok.View): |
---|
129 | """ Query history of eTranzact payments |
---|
130 | """ |
---|
131 | grok.context(ICustomStudentOnlinePayment) |
---|
132 | grok.name('query_history') |
---|
133 | grok.require('waeup.payStudent') |
---|
134 | |
---|
135 | def update(self, confirmation_number=None): |
---|
136 | ob_class = self.__implemented__.__name__ |
---|
137 | if self.context.p_state == 'paid': |
---|
138 | self.flash(_('This ticket has already been paid.')) |
---|
139 | return |
---|
140 | student = self.context.getStudent() |
---|
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) |
---|
150 | return |
---|
151 | |
---|
152 | def render(self): |
---|
153 | self.redirect(self.url(self.context, '@@index')) |
---|
154 | return |
---|
155 | |
---|
156 | class EtranzactQueryHistoryPageApplicant(UtilityView, grok.View): |
---|
157 | """ Query history of eTranzact payments |
---|
158 | """ |
---|
159 | grok.context(ICustomApplicantOnlinePayment) |
---|
160 | grok.name('query_history') |
---|
161 | grok.require('waeup.payApplicant') |
---|
162 | |
---|
163 | def update(self, confirmation_number=None): |
---|
164 | ob_class = self.__implemented__.__name__ |
---|
165 | if self.context.p_state == 'paid': |
---|
166 | self.flash(_('This ticket has already been paid.')) |
---|
167 | return |
---|
168 | applicant = self.context.__parent__ |
---|
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) |
---|
178 | return |
---|
179 | |
---|
180 | def render(self): |
---|
181 | self.redirect(self.url(self.context, '@@index')) |
---|
182 | return |
---|