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