[7929] | 1 | ## $Id: browser.py 7976 2012-03-23 08:49:56Z 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.interfaces import IStudentOnlinePayment |
---|
| 25 | from waeup.kofa.students.browser import write_log_message |
---|
| 26 | from waeup.kofa.students.viewlets import RequestCallbackActionButton |
---|
| 27 | from waeup.custom.interfaces import MessageFactory as _ |
---|
| 28 | from waeup.custom.utils.utils import actions_after_payment |
---|
| 29 | |
---|
| 30 | TERMINAL_ID = '0000000001' |
---|
| 31 | QUERY_URL = 'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp' |
---|
| 32 | |
---|
| 33 | class EtranzactEnterPinActionButton(RequestCallbackActionButton): |
---|
| 34 | grok.order(5) |
---|
| 35 | icon = 'actionicon_call.png' |
---|
| 36 | text = _('Query eTranzact History') |
---|
[7976] | 37 | target = 'enterpin' |
---|
[7929] | 38 | |
---|
| 39 | class EtranzactEnterPinPage(KofaPage): |
---|
[7976] | 40 | """ |
---|
| 41 | """ |
---|
[7929] | 42 | grok.context(IStudentOnlinePayment) |
---|
[7976] | 43 | grok.name('enterpin') |
---|
| 44 | grok.template('enterpin') |
---|
[7929] | 45 | grok.require('waeup.payStudent') |
---|
| 46 | |
---|
[7976] | 47 | buttonname = _('Submit to eTranzact') |
---|
| 48 | label = _('Requery eTranzact History') |
---|
| 49 | action = 'query_history' |
---|
[7929] | 50 | |
---|
| 51 | class 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 |
---|