[7929] | 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 | ## |
---|
| 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 |
---|
[8247] | 24 | from waeup.kofa.applicants.viewlets import RequestCallbackActionButton as RCABApplicant |
---|
| 25 | from waeup.kofa.students.viewlets import RequestCallbackActionButton as RCABStudent |
---|
[8020] | 26 | from waeup.uniben.interfaces import MessageFactory as _ |
---|
[8247] | 27 | from waeup.uniben.students.utils import actions_after_student_payment |
---|
| 28 | from waeup.uniben.applicants.utils import actions_after_applicant_payment |
---|
[8253] | 29 | from waeup.uniben.students.interfaces import ICustomStudentOnlinePayment |
---|
| 30 | from waeup.uniben.applicants.interfaces import ICustomApplicantOnlinePayment |
---|
[7929] | 31 | |
---|
[8267] | 32 | #TERMINAL_ID = '0500000003' |
---|
| 33 | TERMINAL_ID = '0000000001' |
---|
| 34 | #QUERY_URL = 'https://www.etranzact.net/Query/queryPayoutletTransaction.jsp' |
---|
| 35 | QUERY_URL = 'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp' |
---|
[8259] | 36 | |
---|
[8247] | 37 | def query_etranzact(confirmation_number, user, payment, view): |
---|
[8256] | 38 | ob_class = view.__implemented__.__name__ |
---|
[8247] | 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 | |
---|
| 102 | class EtranzactEnterPinActionButtonApplicant(RCABApplicant): |
---|
[8253] | 103 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8259] | 104 | grok.order(3) |
---|
[7929] | 105 | icon = 'actionicon_call.png' |
---|
| 106 | text = _('Query eTranzact History') |
---|
[7976] | 107 | target = 'enterpin' |
---|
[7929] | 108 | |
---|
[8247] | 109 | class EtranzactEnterPinActionButtonStudent(RCABStudent): |
---|
[8253] | 110 | grok.context(ICustomStudentOnlinePayment) |
---|
[8259] | 111 | grok.order(3) |
---|
[8247] | 112 | icon = 'actionicon_call.png' |
---|
| 113 | text = _('Query eTranzact History') |
---|
| 114 | target = 'enterpin' |
---|
| 115 | |
---|
| 116 | class EtranzactEnterPinPageStudent(KofaPage): |
---|
[7976] | 117 | """ |
---|
| 118 | """ |
---|
[8253] | 119 | grok.context(ICustomStudentOnlinePayment) |
---|
[7976] | 120 | grok.name('enterpin') |
---|
| 121 | grok.template('enterpin') |
---|
[7929] | 122 | grok.require('waeup.payStudent') |
---|
| 123 | |
---|
[7976] | 124 | buttonname = _('Submit to eTranzact') |
---|
| 125 | label = _('Requery eTranzact History') |
---|
| 126 | action = 'query_history' |
---|
[7929] | 127 | |
---|
[8247] | 128 | class EtranzactEnterPinPageApplicant(EtranzactEnterPinPageStudent): |
---|
| 129 | """ |
---|
| 130 | """ |
---|
| 131 | grok.require('waeup.payApplicant') |
---|
[8253] | 132 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8247] | 133 | |
---|
| 134 | class EtranzactQueryHistoryPageStudent(UtilityView, grok.View): |
---|
[7929] | 135 | """ Query history of eTranzact payments |
---|
| 136 | """ |
---|
[8253] | 137 | grok.context(ICustomStudentOnlinePayment) |
---|
[7929] | 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() |
---|
[8247] | 146 | if query_etranzact(confirmation_number, student, self.context, self): |
---|
| 147 | actions_after_student_payment(student, self.context, self) |
---|
| 148 | return |
---|
[7929] | 149 | |
---|
[8247] | 150 | def render(self): |
---|
| 151 | self.redirect(self.url(self.context, '@@index')) |
---|
| 152 | return |
---|
[7929] | 153 | |
---|
[8247] | 154 | class EtranzactQueryHistoryPageApplicant(UtilityView, grok.View): |
---|
| 155 | """ Query history of eTranzact payments |
---|
| 156 | """ |
---|
[8253] | 157 | grok.context(ICustomApplicantOnlinePayment) |
---|
[8247] | 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.')) |
---|
[7929] | 164 | return |
---|
[8247] | 165 | applicant = self.context.__parent__ |
---|
| 166 | if query_etranzact(confirmation_number, applicant, self.context, self): |
---|
| 167 | actions_after_applicant_payment(applicant, self) |
---|
[7929] | 168 | return |
---|
| 169 | |
---|
| 170 | def render(self): |
---|
| 171 | self.redirect(self.url(self.context, '@@index')) |
---|
[8259] | 172 | return |
---|