1 | ## $Id: browser.py 9442 2012-10-27 06:37:54Z 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 | import hashlib |
---|
22 | from xml.dom.minidom import parseString |
---|
23 | import grok |
---|
24 | from zope.component import getUtility |
---|
25 | from waeup.kofa.browser.layout import KofaPage, UtilityView |
---|
26 | from waeup.kofa.accesscodes import create_accesscode |
---|
27 | from waeup.kofa.interfaces import RETURNING, IKofaUtils |
---|
28 | from waeup.kofa.utils.helpers import to_timezone |
---|
29 | from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent |
---|
30 | from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant |
---|
31 | from waeup.kwarapoly.students.interfaces import ICustomStudentOnlinePayment |
---|
32 | from waeup.kwarapoly.applicants.interfaces import ICustomApplicantOnlinePayment |
---|
33 | from waeup.kwarapoly.interfaces import MessageFactory as _ |
---|
34 | |
---|
35 | PRODUCT_ID = '3986' |
---|
36 | SITE_NAME = 'kwarapoly-kofa.waeup.org' |
---|
37 | PROVIDER_ACCT = '1010764827' |
---|
38 | PROVIDER_BANK_ID = '117' |
---|
39 | PROVIDER_ITEM_NAME = 'BT Education' |
---|
40 | INSTITUTION_NAME = 'KwaraPoly' |
---|
41 | CURRENCY = '566' |
---|
42 | #QUERY_URL = 'https://webpay.interswitchng.com/paydirect/services/TransactionQueryURL.aspx' |
---|
43 | #QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx' |
---|
44 | |
---|
45 | #POST_ACTION = 'https://webpay.interswitchng.com/paydirect/webpay/pay.aspx' |
---|
46 | POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx' |
---|
47 | |
---|
48 | #HOST = 'webpay.interswitchng.com' |
---|
49 | HOST = 'testwebpay.interswitchng.com' |
---|
50 | |
---|
51 | #URL = '/paydirect/services/TransactionQueryWs.asmx' |
---|
52 | URL = '/test_paydirect/services/TransactionQueryWs.asmx' |
---|
53 | httplib.HTTPConnection.debuglevel = 0 |
---|
54 | |
---|
55 | |
---|
56 | def SOAP_post(soap_action,xml): |
---|
57 | """Handles making the SOAP request. |
---|
58 | |
---|
59 | Further reading: |
---|
60 | http://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryWs.asmx?op=getTransactionData |
---|
61 | """ |
---|
62 | h = httplib.HTTPConnection(HOST) |
---|
63 | headers={ |
---|
64 | 'Host':HOST, |
---|
65 | 'Content-Type':'text/xml; charset=utf-8', |
---|
66 | 'Content-Length':len(xml), |
---|
67 | 'SOAPAction':'"%s"' % soap_action, |
---|
68 | } |
---|
69 | h.request('POST', URL, body=xml,headers=headers) |
---|
70 | r = h.getresponse() |
---|
71 | d = r.read() |
---|
72 | if r.status!=200: |
---|
73 | raise ValueError('Error connecting: %s, %s' % (r.status, r.reason)) |
---|
74 | return d |
---|
75 | |
---|
76 | def get_SOAP_response(product_id, transref): |
---|
77 | xml="""\ |
---|
78 | <?xml version="1.0" encoding="utf-8"?> |
---|
79 | <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> |
---|
80 | <soap:Body> |
---|
81 | <getTransactionData xmlns="http://tempuri.org/"> |
---|
82 | <product_id>%s</product_id> |
---|
83 | <trans_ref>%s</trans_ref> |
---|
84 | </getTransactionData> |
---|
85 | </soap:Body> |
---|
86 | </soap:Envelope>""" % (product_id, transref) |
---|
87 | result_xml=SOAP_post("http://tempuri.org/getTransactionData",xml) |
---|
88 | doc=parseString(result_xml) |
---|
89 | response=doc.getElementsByTagName('getTransactionDataResult')[0].firstChild.data |
---|
90 | return response |
---|
91 | |
---|
92 | def query_interswitch(payment): |
---|
93 | sr = get_SOAP_response(PRODUCT_ID, payment.p_id) |
---|
94 | wlist = sr.split(':') |
---|
95 | if len(wlist) != 7: |
---|
96 | msg = _('Invalid callback: ${a}', mapping = {'a': sr}) |
---|
97 | log = 'invalid callback for payment %s: %s' % (payment.p_id, sr) |
---|
98 | return False, msg, log |
---|
99 | payment.r_code = wlist[0] |
---|
100 | payment.r_desc = wlist[1] |
---|
101 | payment.r_amount_approved = float(wlist[2]) / 100 |
---|
102 | payment.r_card_num = wlist[3] |
---|
103 | payment.r_pay_reference = wlist[5] |
---|
104 | payment.r_company = u'interswitch' |
---|
105 | if payment.r_code != '00': |
---|
106 | msg = _('Unsuccessful callback: ${a}', mapping = {'a': sr}) |
---|
107 | log = 'unsuccessful callback for %s payment %s: %s' % ( |
---|
108 | payment.p_category, payment.p_id, sr) |
---|
109 | payment.p_state = 'failed' |
---|
110 | return False, msg, log |
---|
111 | if payment.r_amount_approved != payment.amount_auth: |
---|
112 | msg = _('Callback amount does not match.') |
---|
113 | log = 'wrong callback for %s payment %s: %s' % ( |
---|
114 | payment.p_category, payment.p_id, sr) |
---|
115 | payment.p_state = 'failed' |
---|
116 | return False, msg, log |
---|
117 | if wlist[4] != payment.p_id: |
---|
118 | msg = _('Callback transaction id does not match.') |
---|
119 | log = 'wrong callback for %s payment %s: %s' % ( |
---|
120 | payment.p_category, payment.p_id, sr) |
---|
121 | payment.p_state = 'failed' |
---|
122 | return False, msg, log |
---|
123 | payment.p_state = 'paid' |
---|
124 | payment.payment_date = datetime.utcnow() |
---|
125 | msg = _('Successful callback received') |
---|
126 | log = 'valid callback for %s payment %s: %s' % ( |
---|
127 | payment.p_category, payment.p_id, sr) |
---|
128 | return True, msg, log |
---|
129 | |
---|
130 | class InterswitchActionButtonStudent(APABStudent): |
---|
131 | grok.order(1) |
---|
132 | grok.context(ICustomStudentOnlinePayment) |
---|
133 | grok.require('waeup.payStudent') |
---|
134 | icon = 'actionicon_pay.png' |
---|
135 | text = _('CollegePAY') |
---|
136 | target = 'goto_interswitch' |
---|
137 | |
---|
138 | @property |
---|
139 | def target_url(self): |
---|
140 | if self.context.p_state != 'unpaid': |
---|
141 | return '' |
---|
142 | return self.view.url(self.view.context, self.target) |
---|
143 | |
---|
144 | class InterswitchActionButtonApplicant(APABApplicant): |
---|
145 | grok.order(1) |
---|
146 | grok.context(ICustomApplicantOnlinePayment) |
---|
147 | grok.require('waeup.payApplicant') |
---|
148 | icon = 'actionicon_pay.png' |
---|
149 | text = _('CollegePAY') |
---|
150 | target = 'goto_interswitch' |
---|
151 | |
---|
152 | @property |
---|
153 | def target_url(self): |
---|
154 | if self.context.p_state != 'unpaid': |
---|
155 | return '' |
---|
156 | return self.view.url(self.view.context, self.target) |
---|
157 | |
---|
158 | class InterswitchRequestWebserviceActionButtonStudent(APABStudent): |
---|
159 | grok.order(2) |
---|
160 | grok.context(ICustomStudentOnlinePayment) |
---|
161 | grok.require('waeup.payStudent') |
---|
162 | icon = 'actionicon_call.png' |
---|
163 | text = _('Requery CollegePAY') |
---|
164 | target = 'request_webservice' |
---|
165 | |
---|
166 | class InterswitchRequestWebserviceActionButtonApplicant(APABApplicant): |
---|
167 | grok.order(2) |
---|
168 | grok.context(ICustomApplicantOnlinePayment) |
---|
169 | grok.require('waeup.payApplicant') |
---|
170 | icon = 'actionicon_call.png' |
---|
171 | text = _('Requery CollegePAY') |
---|
172 | target = 'request_webservice' |
---|
173 | |
---|
174 | class InterswitchPageStudent(KofaPage): |
---|
175 | """ View which sends a POST request to the Interswitch |
---|
176 | CollegePAY payment gateway. |
---|
177 | """ |
---|
178 | grok.context(ICustomStudentOnlinePayment) |
---|
179 | grok.name('goto_interswitch') |
---|
180 | grok.template('student_goto_interswitch') |
---|
181 | grok.require('waeup.payStudent') |
---|
182 | label = _('Submit data to CollegePAY (Interswitch Payment Gateway)') |
---|
183 | submit_button = _('Submit') |
---|
184 | action = POST_ACTION |
---|
185 | site_name = SITE_NAME |
---|
186 | currency = CURRENCY |
---|
187 | pay_item_id = '101' |
---|
188 | product_id = PRODUCT_ID |
---|
189 | mac = '737396C271FB8E2BA1A612A942267553E16373658A5F83D90DAFFBB806C16E9E6284006C06CEFFB55768004179D7BC01CD237CCE6314B938A5A5D9F49369DE5A' |
---|
190 | |
---|
191 | def update(self): |
---|
192 | #if self.context.p_state != 'unpaid': |
---|
193 | if self.context.p_state == 'paid': |
---|
194 | self.flash(_("Payment ticket can't be re-send to CollegePAY.")) |
---|
195 | self.redirect(self.url(self.context, '@@index')) |
---|
196 | return |
---|
197 | |
---|
198 | student = self.student = self.context.student |
---|
199 | certificate = getattr(student['studycourse'],'certificate',None) |
---|
200 | self.amount_auth = 100 * self.context.amount_auth |
---|
201 | xmldict = {} |
---|
202 | if certificate is not None: |
---|
203 | xmldict['department'] = certificate.__parent__.__parent__.code |
---|
204 | xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code |
---|
205 | else: |
---|
206 | xmldict['department'] = None |
---|
207 | xmldict['faculty'] = None |
---|
208 | self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category] |
---|
209 | tz = getUtility(IKofaUtils).tzinfo |
---|
210 | self.local_date_time = to_timezone( |
---|
211 | self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
212 | self.site_redirect_url = self.url(self.context, 'request_webservice') |
---|
213 | # Provider data |
---|
214 | xmldict['detail_ref'] = self.context.p_id |
---|
215 | xmldict['provider_acct'] = PROVIDER_ACCT |
---|
216 | xmldict['provider_bank_id'] = PROVIDER_BANK_ID |
---|
217 | xmldict['provider_item_name'] = PROVIDER_ITEM_NAME |
---|
218 | xmldict['provider_amt'] = 100 * 1200 |
---|
219 | # Dalash data |
---|
220 | xmldict['dalash_amt'] = 100 * 1800 |
---|
221 | # Institution data |
---|
222 | if xmldict['faculty'] in ('CPGS',): |
---|
223 | xmldict['institution_acct'] = "1771180233" |
---|
224 | xmldict['institution_bank_id'] = '120' |
---|
225 | elif xmldict['faculty'] in ('IBAS',): |
---|
226 | xmldict['institution_acct'] = "0006772436" |
---|
227 | xmldict['institution_bank_id'] = '121' |
---|
228 | elif xmldict['faculty'] in ('IETS',): |
---|
229 | xmldict['institution_acct'] = "0106259811" |
---|
230 | xmldict['institution_bank_id'] = '10' |
---|
231 | elif xmldict['faculty'] in ('IFMS',): |
---|
232 | xmldict['institution_acct'] = "2013910271" |
---|
233 | xmldict['institution_bank_id'] = '8' |
---|
234 | elif xmldict['faculty'] in ('ITCH',): |
---|
235 | xmldict['institution_acct'] = "1010445144" |
---|
236 | xmldict['institution_bank_id'] = '117' |
---|
237 | else: |
---|
238 | xmldict['institution_acct'] = "0000000000000" |
---|
239 | xmldict['institution_bank_id'] = '0' |
---|
240 | xmldict['institution_amt'] = 100 * ( |
---|
241 | self.context.amount_auth - 1200 - 300 - 1800) |
---|
242 | xmldict['institution_item_name'] = self.context.p_category |
---|
243 | xmldict['institution_name'] = INSTITUTION_NAME |
---|
244 | |
---|
245 | hashargs = ( |
---|
246 | self.context.p_id + |
---|
247 | PRODUCT_ID + |
---|
248 | self.pay_item_id + |
---|
249 | str(int(self.amount_auth)) + |
---|
250 | self.site_redirect_url + |
---|
251 | self.mac) |
---|
252 | self.hashvalue = hashlib.sha512(hashargs).hexdigest() |
---|
253 | |
---|
254 | # Interswitch amount is not part of the xml data |
---|
255 | xmltext = """<payment_item_detail> |
---|
256 | <item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s"> |
---|
257 | <item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" /> |
---|
258 | <item_detail item_id="2" item_name="Dalash" item_amt="%(dalash_amt)s" bank_id="117" acct_num="1013196791" /> |
---|
259 | <item_detail item_id="3" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" /> |
---|
260 | </item_details> |
---|
261 | </payment_item_detail>""" % xmldict |
---|
262 | self.xml_data = """<input type="hidden" name="xml_data" value='%s' />""" % xmltext |
---|
263 | return |
---|
264 | |
---|
265 | class InterswitchPageApplicant(KofaPage): |
---|
266 | """ View which sends a POST request to the Interswitch |
---|
267 | CollegePAY payment gateway. |
---|
268 | """ |
---|
269 | grok.context(ICustomApplicantOnlinePayment) |
---|
270 | grok.require('waeup.payApplicant') |
---|
271 | grok.template('applicant_goto_interswitch') |
---|
272 | grok.name('goto_interswitch') |
---|
273 | label = _('Submit data to CollegePAY (Interswitch Payment Gateway)') |
---|
274 | submit_button = _('Submit') |
---|
275 | action = POST_ACTION |
---|
276 | site_name = SITE_NAME |
---|
277 | currency = CURRENCY |
---|
278 | pay_item_id = '' |
---|
279 | product_id = PRODUCT_ID |
---|
280 | |
---|
281 | def update(self): |
---|
282 | if self.context.p_state != 'unpaid': |
---|
283 | self.flash(_("Payment ticket can't be re-send to CollegePAY.")) |
---|
284 | self.redirect(self.url(self.context, '@@index')) |
---|
285 | return |
---|
286 | if self.context.__parent__.__parent__.expired \ |
---|
287 | and self.context.__parent__.__parent__.strict_deadline: |
---|
288 | self.flash(_("Payment ticket can't be send to CollegePAY. " |
---|
289 | "Application period has expired.")) |
---|
290 | self.redirect(self.url(self.context, '@@index')) |
---|
291 | return |
---|
292 | self.applicant = self.context.__parent__ |
---|
293 | self.amount_auth = 100 * self.context.amount_auth |
---|
294 | xmldict = {} |
---|
295 | self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category] |
---|
296 | tz = getUtility(IKofaUtils).tzinfo |
---|
297 | self.local_date_time = to_timezone( |
---|
298 | self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
299 | self.site_redirect_url = self.url(self.context, 'request_webservice') |
---|
300 | xmldict['detail_ref'] = self.context.p_id |
---|
301 | # Provider data |
---|
302 | xmldict['provider_amt'] = 100 * 500 |
---|
303 | xmldict['provider_acct'] = PROVIDER_ACCT |
---|
304 | xmldict['provider_bank_id'] = PROVIDER_BANK_ID |
---|
305 | xmldict['provider_item_name'] = PROVIDER_ITEM_NAME |
---|
306 | # Institution data |
---|
307 | xmldict['institution_amt'] = 100 * (self.context.amount_auth - 500 - 150) |
---|
308 | xmldict['institution_acct'] = '0' |
---|
309 | xmldict['institution_bank_id'] = '0' |
---|
310 | xmldict['institution_item_name'] = self.context.p_category |
---|
311 | xmldict['institution_name'] = INSTITUTION_NAME |
---|
312 | # Interswitch amount is not part of the xml data |
---|
313 | xmltext = """<payment_item_detail> |
---|
314 | <item_details detail_ref="%(detail_ref)s" college="%(institution_name)s"> |
---|
315 | <item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" /> |
---|
316 | <item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" /> |
---|
317 | </item_details> |
---|
318 | </payment_item_detail>""" % xmldict |
---|
319 | self.xml_data = """<input type="hidden" name="xml_data" value='%s' />""" % xmltext |
---|
320 | return |
---|
321 | |
---|
322 | |
---|
323 | class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View): |
---|
324 | """ Request webservice view for the CollegePAY gateway |
---|
325 | """ |
---|
326 | grok.context(ICustomStudentOnlinePayment) |
---|
327 | grok.name('request_webservice') |
---|
328 | grok.require('waeup.payStudent') |
---|
329 | |
---|
330 | def update(self): |
---|
331 | ob_class = self.__implemented__.__name__ |
---|
332 | if self.context.p_state == 'paid': |
---|
333 | self.flash(_('This ticket has already been paid.')) |
---|
334 | return |
---|
335 | student = self.context.student |
---|
336 | success, msg, log = query_interswitch(self.context) |
---|
337 | student.writeLogMessage(self, log) |
---|
338 | if not success: |
---|
339 | self.flash(msg) |
---|
340 | return |
---|
341 | success, msg, log = self.context.doAfterStudentPayment() |
---|
342 | if log is not None: |
---|
343 | student.writeLogMessage(self, log) |
---|
344 | self.flash(msg) |
---|
345 | return |
---|
346 | |
---|
347 | def render(self): |
---|
348 | self.redirect(self.url(self.context, '@@index')) |
---|
349 | return |
---|
350 | |
---|
351 | class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View): |
---|
352 | """ Request webservice view for the CollegePAY gateway |
---|
353 | """ |
---|
354 | grok.context(ICustomApplicantOnlinePayment) |
---|
355 | grok.name('request_webservice') |
---|
356 | grok.require('waeup.payApplicant') |
---|
357 | |
---|
358 | def update(self): |
---|
359 | if self.context.p_state == 'paid': |
---|
360 | self.flash(_('This ticket has already been paid.')) |
---|
361 | return |
---|
362 | applicant = self.context.__parent__ |
---|
363 | success, msg, log = query_interswitch(self.context) |
---|
364 | applicant.writeLogMessage(self, log) |
---|
365 | if not success: |
---|
366 | self.flash(msg) |
---|
367 | return |
---|
368 | success, msg, log = self.context.doAfterApplicantPayment() |
---|
369 | if log is not None: |
---|
370 | applicant.writeLogMessage(self, log) |
---|
371 | self.flash(msg) |
---|
372 | return |
---|
373 | |
---|
374 | def render(self): |
---|
375 | self.redirect(self.url(self.context, '@@index')) |
---|
376 | return |
---|