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