[7894] | 1 | ## $Id: browser.py 7898 2012-03-16 10:06:59Z 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 |
---|
[7898] | 19 | import httplib |
---|
| 20 | import urllib |
---|
| 21 | from xml.dom.minidom import parseString |
---|
[7894] | 22 | import grok |
---|
| 23 | from waeup.kofa.browser.layout import KofaPage, UtilityView |
---|
| 24 | from waeup.kofa.accesscodes import create_accesscode |
---|
| 25 | from waeup.kofa.students.interfaces import IStudentOnlinePayment |
---|
| 26 | from waeup.kofa.students.browser import write_log_message |
---|
| 27 | from waeup.kofa.students.viewlets import RequestCallbackActionButton |
---|
| 28 | from waeup.custom.interfaces import MessageFactory as _ |
---|
| 29 | |
---|
| 30 | PRODUCT_ID = '57' |
---|
| 31 | SITE_NAME = 'xyz.waeup.org' |
---|
| 32 | PROVIDER_ACCT = '2345' |
---|
| 33 | PROVIDER_BANK_ID = '8' |
---|
| 34 | PROVIDER_ITEM_NAME = 'Kofa Provider Fee' |
---|
| 35 | INSTITUTION_ACCT = '1234' |
---|
| 36 | INSTITUTION_BANK_ID = '9' |
---|
| 37 | INSTITUTION_NAME = 'Sample University' |
---|
| 38 | CURRENCY = '566' |
---|
| 39 | PAY_ITEM_ID = '5700' |
---|
| 40 | QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx' |
---|
| 41 | POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx' |
---|
| 42 | |
---|
[7898] | 43 | HOST = 'testwebpay.interswitchng.com' |
---|
| 44 | URL = '/test_paydirect/services/TransactionQueryWs.asmx' |
---|
| 45 | httplib.HTTPConnection.debuglevel = 0 |
---|
| 46 | |
---|
| 47 | def SOAP_post(soap_action,xml): |
---|
| 48 | """Handles making the SOAP request. |
---|
| 49 | |
---|
| 50 | Further reading: |
---|
| 51 | http://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryWs.asmx?op=getTransactionData |
---|
| 52 | """ |
---|
| 53 | h = httplib.HTTPConnection(HOST) |
---|
| 54 | headers={ |
---|
| 55 | 'Host':HOST, |
---|
| 56 | 'Content-Type':'text/xml; charset=utf-8', |
---|
| 57 | 'Content-Length':len(xml), |
---|
| 58 | 'SOAPAction':'"%s"' % soap_action, |
---|
| 59 | } |
---|
| 60 | h.request('POST', URL, body=xml,headers=headers) |
---|
| 61 | r = h.getresponse() |
---|
| 62 | d = r.read() |
---|
| 63 | if r.status!=200: |
---|
| 64 | raise ValueError('Error connecting: %s, %s' % (r.status, r.reason)) |
---|
| 65 | return d |
---|
| 66 | |
---|
| 67 | def get_SOAP_response(product_id, transref): |
---|
| 68 | xml="""\ |
---|
| 69 | <?xml version="1.0" encoding="utf-8"?> |
---|
| 70 | <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/"> |
---|
| 71 | <soap:Body> |
---|
| 72 | <getTransactionData xmlns="http://tempuri.org/"> |
---|
| 73 | <product_id>%s</product_id> |
---|
| 74 | <trans_ref>%s</trans_ref> |
---|
| 75 | </getTransactionData> |
---|
| 76 | </soap:Body> |
---|
| 77 | </soap:Envelope>""" % (product_id, transref) |
---|
| 78 | result_xml=SOAP_post("http://tempuri.org/getTransactionData",xml) |
---|
| 79 | doc=parseString(result_xml) |
---|
| 80 | response=doc.getElementsByTagName('getTransactionDataResult')[0].firstChild.data |
---|
| 81 | return response |
---|
| 82 | |
---|
[7894] | 83 | class InterswitchActionButton(RequestCallbackActionButton): |
---|
| 84 | grok.order(2) |
---|
| 85 | icon = 'actionicon_pay.png' |
---|
| 86 | text = _('CollegePAY') |
---|
| 87 | target = 'goto_interswitch' |
---|
| 88 | |
---|
| 89 | @property |
---|
| 90 | def target_url(self): |
---|
| 91 | if self.context.p_state != 'unpaid': |
---|
| 92 | return '' |
---|
| 93 | return self.view.url(self.view.context, self.target) |
---|
| 94 | |
---|
| 95 | class InterswitchRequestCallbackActionButton(RequestCallbackActionButton): |
---|
| 96 | grok.order(3) |
---|
| 97 | icon = 'actionicon_call.png' |
---|
| 98 | text = _('Request CollegePAY callback') |
---|
| 99 | |
---|
| 100 | def target_url(self): |
---|
| 101 | if self.context.p_state == 'paid': |
---|
| 102 | return '' |
---|
| 103 | site_redirect_url = self.view.url(self.view.context, 'callback') |
---|
| 104 | args = { |
---|
| 105 | 'transRef':self.context.p_id, |
---|
| 106 | 'prodID':PRODUCT_ID, |
---|
| 107 | 'redirectURL':site_redirect_url} |
---|
[7898] | 108 | return QUERY_URL + '?%s' % urllib.urlencode(args) |
---|
[7894] | 109 | |
---|
| 110 | class InterswitchPage(KofaPage): |
---|
| 111 | """ View which sends a POST request to the Interswitch |
---|
| 112 | CollegePAY payment gateway. |
---|
| 113 | """ |
---|
| 114 | grok.context(IStudentOnlinePayment) |
---|
| 115 | grok.name('goto_interswitch') |
---|
| 116 | grok.template('goto_interswitch') |
---|
| 117 | grok.require('waeup.payStudent') |
---|
| 118 | label = _('Submit data to CollegePAY (Interswitch Payment Gateway)') |
---|
| 119 | submit_button = _('Submit') |
---|
| 120 | action = POST_ACTION |
---|
| 121 | site_name = SITE_NAME |
---|
| 122 | currency = CURRENCY |
---|
| 123 | pay_item_id = PAY_ITEM_ID |
---|
| 124 | product_id = PRODUCT_ID |
---|
| 125 | |
---|
| 126 | def update(self): |
---|
| 127 | if self.context.p_state != 'unpaid': |
---|
| 128 | self.flash(_("Payment ticket can't be re-send to CollegePAY.")) |
---|
| 129 | self.redirect(self.url(self.context, '@@index')) |
---|
| 130 | return |
---|
| 131 | self.student = self.context.getStudent() |
---|
| 132 | self.amount = (self.context.amount_auth + self.context.surcharge_1 + |
---|
| 133 | self.context.surcharge_2 + self.context.surcharge_3) |
---|
| 134 | self.amount_100 = 100 * self.amount |
---|
| 135 | self.local_date_time = str(self.context.creation_date) |
---|
| 136 | self.site_redirect_url = self.url(self.context, 'callback') |
---|
| 137 | certificate = getattr(self.student['studycourse'],'certificate',None) |
---|
| 138 | xmldict = {} |
---|
| 139 | if certificate is not None: |
---|
| 140 | xmldict['department'] = certificate.__parent__.__parent__.code |
---|
| 141 | xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code |
---|
| 142 | else: |
---|
| 143 | xmldict['department'] = None |
---|
| 144 | xmldict['faculty'] = None |
---|
| 145 | xmldict['detail_ref'] = self.context.p_id |
---|
| 146 | xmldict['provider_amt'] = 100 * self.context.surcharge_1 |
---|
| 147 | xmldict['provider_acct'] = PROVIDER_ACCT |
---|
| 148 | xmldict['provider_bank_id'] = PROVIDER_BANK_ID |
---|
| 149 | xmldict['provider_item_name'] = PROVIDER_ITEM_NAME |
---|
| 150 | xmldict['institution_amt'] = 100 * self.context.amount_auth |
---|
| 151 | xmldict['institution_acct'] = INSTITUTION_ACCT |
---|
| 152 | xmldict['institution_bank_id'] = INSTITUTION_BANK_ID |
---|
| 153 | xmldict['institution_item_name'] = self.context.p_category |
---|
| 154 | xmldict['institution_name'] = INSTITUTION_NAME |
---|
| 155 | # Interswitch amount is not part of the xml data |
---|
| 156 | xmltext = """<payment_item_detail> |
---|
| 157 | <item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s"> |
---|
| 158 | <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" /> |
---|
| 159 | <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" /> |
---|
| 160 | </item_details> |
---|
| 161 | </payment_item_detail>""" % xmldict |
---|
| 162 | self.xml_data = """<input type="hidden" name="xml_data" value='%s' />""" % xmltext |
---|
| 163 | return |
---|
| 164 | |
---|
| 165 | class OnlinePaymentCallbackPage(UtilityView, grok.View): |
---|
| 166 | """ Callback view for the CollegePAY gateway |
---|
| 167 | """ |
---|
| 168 | grok.context(IStudentOnlinePayment) |
---|
| 169 | grok.name('callback') |
---|
| 170 | grok.require('waeup.payStudent') |
---|
| 171 | |
---|
| 172 | # This view is not yet working for offline querying transactions |
---|
| 173 | # since the query string differs from the query string sent after |
---|
| 174 | # posting transactions. This Interswitch bug must be removed first. |
---|
| 175 | # Alternatively, we could use the webservice only and replace |
---|
| 176 | # the RequestCallbackActionButton by a RequestWebserviceActionButton |
---|
| 177 | |
---|
| 178 | def update(self): |
---|
| 179 | if self.context.p_state == 'paid': |
---|
| 180 | self.flash(_('This ticket has already been paid.')) |
---|
| 181 | return |
---|
| 182 | student = self.context.getStudent() |
---|
| 183 | query = self.request.form |
---|
| 184 | # Should be logged instead of printed |
---|
| 185 | print query |
---|
| 186 | if query.get('resp', None) != '00': |
---|
| 187 | self.flash(_('Unsuccessful callback: ${a}', |
---|
| 188 | mapping = {'a': query.get('desc', _('Incomplete query string.'))})) |
---|
| 189 | write_log_message(self,'invalid callback: %s' % self.context.p_id) |
---|
| 190 | self.context.r_card_num = query.get('cardNum', None) |
---|
| 191 | self.context.r_code = query.get('resp', None) |
---|
| 192 | self.context.p_state = 'failed' |
---|
| 193 | return |
---|
| 194 | |
---|
[7896] | 195 | if query.get('apprAmt', None) != str(self.context.amount_auth): |
---|
| 196 | self.flash(_('Wrong amount')) |
---|
| 197 | write_log_message(self,'wrong amount: %s' % self.context.p_id) |
---|
| 198 | self.context.r_card_num = query.get('cardNum', None) |
---|
| 199 | self.context.r_code = query.get('resp', None) |
---|
| 200 | self.context.p_state = 'failed' |
---|
| 201 | return |
---|
[7894] | 202 | |
---|
| 203 | # Add webservice validation |
---|
[7898] | 204 | validation_list = get_SOAP_response( |
---|
| 205 | PRODUCT_ID, self.context.p_id).split(':') |
---|
| 206 | # Validation does not make sense yet since the query string |
---|
| 207 | # formats are conflicting. |
---|
| 208 | print validation_list |
---|
[7894] | 209 | |
---|
| 210 | write_log_message(self,'valid callback: %s' % self.context.p_id) |
---|
| 211 | self.context.r_amount_approved = self.context.amount_auth |
---|
| 212 | self.context.r_card_num = query.get('cardNum', None) |
---|
| 213 | self.context.r_code = query.get('resp', None) |
---|
| 214 | self.context.r_desc = query.get('desc', None) |
---|
| 215 | self.context.r_pay_reference = query.get('payRef', None) |
---|
| 216 | self.context.p_state = 'paid' |
---|
| 217 | self.context.payment_date = datetime.now() |
---|
| 218 | |
---|
| 219 | if self.context.p_category == 'clearance': |
---|
| 220 | # Create CLR access code |
---|
| 221 | pin, error = create_accesscode('CLR',0,student.student_id) |
---|
| 222 | if error: |
---|
| 223 | self.flash(_('Valid callback received. ${a}', |
---|
| 224 | mapping = {'a':error})) |
---|
| 225 | return |
---|
| 226 | self.context.ac = pin |
---|
| 227 | elif self.context.p_category == 'schoolfee': |
---|
| 228 | # Create SFE access code |
---|
| 229 | pin, error = create_accesscode('SFE',0,student.student_id) |
---|
| 230 | if error: |
---|
| 231 | self.flash(_('Valid callback received. ${a}', |
---|
| 232 | mapping = {'a':error})) |
---|
| 233 | return |
---|
| 234 | self.context.ac = pin |
---|
| 235 | elif self.context.p_category == 'bed_allocation': |
---|
| 236 | # Create HOS access code |
---|
| 237 | pin, error = create_accesscode('HOS',0,student.student_id) |
---|
| 238 | if error: |
---|
| 239 | self.flash(_('Valid callback received. ${a}', |
---|
| 240 | mapping = {'a':error})) |
---|
| 241 | return |
---|
| 242 | self.context.ac = pin |
---|
| 243 | self.flash(_('Valid callback received.')) |
---|
| 244 | return |
---|
| 245 | |
---|
| 246 | def render(self): |
---|
| 247 | self.redirect(self.url(self.context, '@@index')) |
---|
| 248 | return |
---|