source: main/waeup.uniben/trunk/src/waeup/uniben/interswitch/browser.py @ 9747

Last change on this file since 9747 was 9745, checked in by Henrik Bettermann, 12 years ago

Catch connection errors.

  • Property svn:keywords set to Id
File size: 20.9 KB
RevLine 
[7894]1## $Id: browser.py 9745 2012-11-30 18:16:40Z 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##
18from datetime import datetime
[7898]19import httplib
20import urllib
21from xml.dom.minidom import parseString
[7894]22import grok
[9713]23from zope.event import notify
[8281]24from zope.component import getUtility
[7894]25from waeup.kofa.browser.layout import KofaPage, UtilityView
26from waeup.kofa.accesscodes import create_accesscode
[8281]27from waeup.kofa.interfaces import RETURNING, IKofaUtils
28from waeup.kofa.utils.helpers import to_timezone
[8421]29from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
30from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
[8263]31from waeup.uniben.students.interfaces import ICustomStudentOnlinePayment
32from waeup.uniben.applicants.interfaces import ICustomApplicantOnlinePayment
[8020]33from waeup.uniben.interfaces import MessageFactory as _
[7894]34
35PRODUCT_ID = '57'
[8263]36SITE_NAME = 'uniben-kofa.waeup.org'
[8424]37PROVIDER_ACCT = '1010764827'
38PROVIDER_BANK_ID = '117'
[8263]39PROVIDER_ITEM_NAME = 'BT Education'
40INSTITUTION_NAME = 'Uniben'
[7894]41CURRENCY = '566'
[8401]42#QUERY_URL = 'https://webpay.interswitchng.com/paydirect/services/TransactionQueryURL.aspx'
[8293]43#QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx'
[8385]44POST_ACTION = 'https://webpay.interswitchng.com/paydirect/webpay/pay.aspx'
[8293]45#POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx'
[7894]46
[8293]47HOST = 'webpay.interswitchng.com'
48#HOST = 'testwebpay.interswitchng.com'
49URL = '/paydirect/services/TransactionQueryWs.asmx'
50#URL = '/test_paydirect/services/TransactionQueryWs.asmx'
[7898]51httplib.HTTPConnection.debuglevel = 0
52
[8256]53
[7898]54def SOAP_post(soap_action,xml):
55    """Handles making the SOAP request.
56
57    Further reading:
58    http://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryWs.asmx?op=getTransactionData
59    """
60    h = httplib.HTTPConnection(HOST)
61    headers={
62        'Host':HOST,
63        'Content-Type':'text/xml; charset=utf-8',
64        'Content-Length':len(xml),
65        'SOAPAction':'"%s"' % soap_action,
66    }
67    h.request('POST', URL, body=xml,headers=headers)
[9745]68    response = h.getresponse()
69    return response
[7898]70
71def get_SOAP_response(product_id, transref):
72    xml="""\
73<?xml version="1.0" encoding="utf-8"?>
74<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/">
75  <soap:Body>
76    <getTransactionData xmlns="http://tempuri.org/">
77      <product_id>%s</product_id>
78      <trans_ref>%s</trans_ref>
79    </getTransactionData>
80  </soap:Body>
81</soap:Envelope>""" % (product_id, transref)
[9745]82    response=SOAP_post("http://tempuri.org/getTransactionData",xml)
83    if response.status!=200:
84        return 'Connection error (%s, %s)' % (response.status, response.reason)
85    result_xml = response.read()
[7898]86    doc=parseString(result_xml)
87    response=doc.getElementsByTagName('getTransactionDataResult')[0].firstChild.data
88    return response
89
[8430]90def query_interswitch(payment):
[8256]91    sr = get_SOAP_response(PRODUCT_ID, payment.p_id)
[9745]92    if sr.startswith('Connection error'):
93        msg = _('Connection error')
94        log = sr
95        return False, msg, log
[8256]96    wlist = sr.split(':')
97    if len(wlist) != 7:
[8430]98        msg = _('Invalid callback: ${a}', mapping = {'a': sr})
99        log = 'invalid callback for payment %s: %s' % (payment.p_id, sr)
100        return False, msg, log
[8256]101    payment.r_code = wlist[0]
102    payment.r_desc = wlist[1]
103    payment.r_amount_approved = float(wlist[2]) / 100
104    payment.r_card_num = wlist[3]
105    payment.r_pay_reference = wlist[5]
[8955]106    payment.r_company = u'interswitch'
[8256]107    if payment.r_code != '00':
[8430]108        msg = _('Unsuccessful callback: ${a}', mapping = {'a': sr})
[9443]109        log = 'unsuccessful callback for %s payment %s: %s' % (
110            payment.p_category, payment.p_id, sr)
[8256]111        payment.p_state = 'failed'
[9713]112        notify(grok.ObjectModifiedEvent(payment))
[8430]113        return False, msg, log
[8263]114    if payment.r_amount_approved != payment.amount_auth:
[8430]115        msg = _('Callback amount does not match.')
[9443]116        log = 'wrong callback for %s payment %s: %s' % (
117            payment.p_category, payment.p_id, sr)
[8256]118        payment.p_state = 'failed'
[9713]119        notify(grok.ObjectModifiedEvent(payment))
[8430]120        return False, msg, log
[8256]121    if wlist[4] != payment.p_id:
[8430]122        msg = _('Callback transaction id does not match.')
[9443]123        log = 'wrong callback for %s payment %s: %s' % (
124            payment.p_category, payment.p_id, sr)
[8256]125        payment.p_state = 'failed'
[9713]126        notify(grok.ObjectModifiedEvent(payment))
[8430]127        return False, msg, log
[8256]128    payment.p_state = 'paid'
[8433]129    payment.payment_date = datetime.utcnow()
[8430]130    msg = _('Successful callback received')
[9435]131    log = 'valid callback for %s payment %s: %s' % (
132        payment.p_category, payment.p_id, sr)
[9713]133    notify(grok.ObjectModifiedEvent(payment))
[8430]134    return True, msg, log
[8256]135
[8421]136class InterswitchActionButtonStudent(APABStudent):
[8259]137    grok.order(1)
[8255]138    grok.context(ICustomStudentOnlinePayment)
[8430]139    grok.require('waeup.payStudent')
[7894]140    icon = 'actionicon_pay.png'
141    text = _('CollegePAY')
142    target = 'goto_interswitch'
143
144    @property
145    def target_url(self):
146        if self.context.p_state != 'unpaid':
147            return ''
148        return self.view.url(self.view.context, self.target)
149
[8421]150class InterswitchActionButtonApplicant(APABApplicant):
[8259]151    grok.order(1)
[8256]152    grok.context(ICustomApplicantOnlinePayment)
[8430]153    grok.require('waeup.payApplicant')
[8256]154    icon = 'actionicon_pay.png'
155    text = _('CollegePAY')
156    target = 'goto_interswitch'
157
158    @property
159    def target_url(self):
160        if self.context.p_state != 'unpaid':
161            return ''
162        return self.view.url(self.view.context, self.target)
163
164# Deprecated
[8259]165#class InterswitchRequestCallbackActionButtonStudent(RCABStudent):
166#    grok.order(3)
167#    grok.context(ICustomStudentOnlinePayment)
168#    icon = 'actionicon_call.png'
169#    text = _('Request CollegePAY callback')
[7894]170
[8259]171#    def target_url(self):
172#        if self.context.p_state == 'paid':
173#            return ''
174#        site_redirect_url = self.view.url(self.view.context, 'isw_callback')
175#        args = {
176#            'transRef':self.context.p_id,
177#            'prodID':PRODUCT_ID,
178#            'redirectURL':site_redirect_url}
179#        return QUERY_URL + '?%s' % urllib.urlencode(args)
[7894]180
[7919]181# Alternative preferred solution
[8421]182class InterswitchRequestWebserviceActionButtonStudent(APABStudent):
[8259]183    grok.order(2)
[8255]184    grok.context(ICustomStudentOnlinePayment)
[8430]185    grok.require('waeup.payStudent')
[7919]186    icon = 'actionicon_call.png'
[8421]187    text = _('Requery CollegePAY')
[7919]188    target = 'request_webservice'
189
[8421]190class InterswitchRequestWebserviceActionButtonApplicant(APABApplicant):
[8259]191    grok.order(2)
[8256]192    grok.context(ICustomApplicantOnlinePayment)
[8430]193    grok.require('waeup.payApplicant')
[8256]194    icon = 'actionicon_call.png'
[8421]195    text = _('Requery CollegePAY')
[8256]196    target = 'request_webservice'
[7919]197
[8256]198
199class InterswitchPageStudent(KofaPage):
[7894]200    """ View which sends a POST request to the Interswitch
201    CollegePAY payment gateway.
202    """
[8255]203    grok.context(ICustomStudentOnlinePayment)
[7894]204    grok.name('goto_interswitch')
[8256]205    grok.template('student_goto_interswitch')
[7894]206    grok.require('waeup.payStudent')
207    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
208    submit_button = _('Submit')
209    action = POST_ACTION
210    site_name = SITE_NAME
211    currency = CURRENCY
212    product_id = PRODUCT_ID
213
214    def update(self):
[8256]215        #if self.context.p_state != 'unpaid':
216        if self.context.p_state == 'paid':
[7894]217            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
218            self.redirect(self.url(self.context, '@@index'))
219            return
[8256]220
[8741]221        student = self.student = self.context.student
222        certificate = getattr(student['studycourse'],'certificate',None)
[8276]223        self.amount_auth = 100 * self.context.amount_auth
[7894]224        xmldict = {}
225        if certificate is not None:
226            xmldict['department'] = certificate.__parent__.__parent__.code
227            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
228        else:
229            xmldict['department'] = None
230            xmldict['faculty'] = None
[9407]231        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
[8281]232        tz = getUtility(IKofaUtils).tzinfo
233        self.local_date_time = to_timezone(
234            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
[8256]235        self.site_redirect_url = self.url(self.context, 'request_webservice')
[8263]236        # Provider data
237        xmldict['detail_ref'] = self.context.p_id
238        xmldict['provider_acct'] = PROVIDER_ACCT
239        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
240        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
241        # Institution data
[9385]242        xmldict['institution_acct'] = '000000000000'
243        xmldict['institution_bank_id'] = '00'
[9384]244        xmldict['institution_amt'] = '0.0'
245        if self.context.p_category == 'schoolfee':
[9460]246            provider_amt = 1500
[9384]247            if student.current_mode.endswith('_ft'):
[9389]248                self.pay_item_id = '5700'
[9460]249                if student.current_mode in ('ug_ft','de_ft','ct_ft','ume_ft'):
250                    xmldict['institution_acct'] = '2017506430'
251                    xmldict['institution_bank_id'] = '8'
252                elif student.current_mode in ('dp_ft'):
253                    xmldict['institution_acct'] = '9201805071'
254                    xmldict['institution_bank_id'] = '17'
255                elif student.current_mode in ('pg_ft'):
256                    xmldict['institution_acct'] = '5330832799'
[9384]257                    xmldict['institution_bank_id'] = '51'
258            elif student.current_mode.endswith('_pt'):
[9389]259                self.pay_item_id = '5701'
[9460]260                if student.current_mode in ('ug_pt','de_pt','ct_pt'):
261                    xmldict['institution_acct'] = '0122009929'
[9384]262                    xmldict['institution_bank_id'] = '16'
[9460]263                elif student.current_mode in ('dp_pt'):
264                    xmldict['institution_acct'] = '9201805071'
265                    xmldict['institution_bank_id'] = '17'
266                elif student.current_mode in ('pg_pt'):
267                    xmldict['institution_acct'] = '0031716047'
268                    xmldict['institution_bank_id'] = '10'
[9384]269        elif self.context.p_category == 'clearance':
[9389]270            self.pay_item_id = '5702'
[9384]271            provider_amt = 1500
[9742]272            if student.current_mode == 'pg_ft':
273                xmldict['institution_acct'] = '5330832799'
274                xmldict['institution_bank_id'] = '51'
275            elif student.current_mode == 'pg_pt':
276                xmldict['institution_acct'] = '0031716047'
277                xmldict['institution_bank_id'] = '10'
278            elif student.current_mode == 'dp_pt':
279                xmldict['institution_acct'] = '9201805071'
280                xmldict['institution_bank_id'] = '17'
281            else:
282                xmldict['institution_bank_id'] = '7'
283                xmldict['institution_acct'] = '1003475516'
[9727]284        elif self.context.p_category == 'gown':
[9515]285            self.pay_item_id = '5704'
286            provider_amt = 0
287            xmldict['institution_bank_id'] = '7'
288            xmldict['institution_acct'] = '1016232382'
[9727]289        elif self.context.p_category.startswith('hostel_maintenance'):
290            self.pay_item_id = '5705'
[9728]291            provider_amt = 0
[9727]292            xmldict['institution_bank_id'] = '129'
293            xmldict['institution_acct'] = '0014414547'
[9384]294
295        xmldict['provider_amt'] = 100 * provider_amt
296        xmldict['institution_item_name'] = self.category
[8263]297        xmldict['institution_name'] = INSTITUTION_NAME
[9384]298        xmldict['institution_amt'] = 100 * (
299            self.context.amount_auth - provider_amt - 150)
[8263]300        # Interswitch amount is not part of the xml data
[9515]301        if provider_amt == 0:
302            xmltext = """<payment_item_detail>
[8263]303<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
304<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" />
[9515]305</item_details>
306</payment_item_detail>""" % xmldict
307        else:
308            xmltext = """<payment_item_detail>
309<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
310<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" />
[8263]311<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" />
312</item_details>
313</payment_item_detail>""" % xmldict
314        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
[7894]315        return
316
[8263]317class InterswitchPageApplicant(KofaPage):
[8256]318    """ View which sends a POST request to the Interswitch
319    CollegePAY payment gateway.
320    """
321    grok.context(ICustomApplicantOnlinePayment)
322    grok.require('waeup.payApplicant')
323    grok.template('applicant_goto_interswitch')
[8263]324    grok.name('goto_interswitch')
325    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
326    submit_button = _('Submit')
327    action = POST_ACTION
328    site_name = SITE_NAME
329    currency = CURRENCY
[8274]330    pay_item_id = '5703'
[8263]331    product_id = PRODUCT_ID
[8256]332
333    def update(self):
[8263]334        if self.context.p_state != 'unpaid':
335            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
336            self.redirect(self.url(self.context, '@@index'))
337            return
[8829]338        if self.context.__parent__.__parent__.expired \
339            and self.context.__parent__.__parent__.strict_deadline:
[8694]340            self.flash(_("Payment ticket can't be send to CollegePAY. "
341                         "Application period has expired."))
342            self.redirect(self.url(self.context, '@@index'))
343            return
[8256]344        self.applicant = self.context.__parent__
[8276]345        self.amount_auth = 100 * self.context.amount_auth
[8256]346        xmldict = {}
[9407]347        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
[8281]348        tz = getUtility(IKofaUtils).tzinfo
349        self.local_date_time = to_timezone(
350            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
[8256]351        self.site_redirect_url = self.url(self.context, 'request_webservice')
[9636]352        provider_amt = 400
[8545]353        if self.applicant.applicant_id.startswith('pg'):
[8568]354            xmldict['institution_acct'] = '0031716030'
355            xmldict['institution_bank_id'] = '10'
[9636]356        elif self.applicant.applicant_id.startswith('dp'):
357            xmldict['institution_acct'] = '9201805071'
358            xmldict['institution_bank_id'] = '17'
[8545]359        else:
[8568]360            xmldict['institution_acct'] = '6220032503'
361            xmldict['institution_bank_id'] = '51'
[8263]362        xmldict['detail_ref'] = self.context.p_id
[8545]363        xmldict['provider_amt'] = 100 * provider_amt
[8263]364        xmldict['provider_acct'] = PROVIDER_ACCT
365        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
366        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
[8545]367        xmldict['institution_amt'] = 100 * (self.context.amount_auth - provider_amt - 150)
[8263]368        xmldict['institution_item_name'] = self.context.p_category
369        xmldict['institution_name'] = INSTITUTION_NAME
370        # Interswitch amount is not part of the xml data
371        xmltext = """<payment_item_detail>
372<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
373<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" />
374<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" />
375</item_details>
376</payment_item_detail>""" % xmldict
377        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
[8256]378        return
379
380# Deprecated
[8263]381#class InterswitchPaymentCallbackPageStudent(UtilityView, grok.View):
382#    """ Callback view for the CollegePAY gateway
383#    """
384#    grok.context(ICustomStudentOnlinePayment)
385#    grok.name('isw_callback')
386#    grok.require('waeup.payStudent')
[7894]387
388    # This view is not yet working for offline querying transactions
389    # since the query string differs from the query string sent after
390    # posting transactions. This Interswitch bug must be removed first.
391    # Alternatively, we could use the webservice only and replace
392    # the RequestCallbackActionButton by a RequestWebserviceActionButton
393
[8263]394#    def update(self):
395#        if self.context.p_state == 'paid':
396#            self.flash(_('This ticket has already been paid.'))
397#            return
[8741]398#        student = self.context.student
[8263]399#        query = self.request.form
400#        write_log_message(self,'callback received: %s' % query)
401#        self.context.r_card_num = query.get('cardNum', None)
402#        self.context.r_code = query.get('resp', None)
403#        self.context.r_pay_reference  = query.get('payRef', None)
404#        self.context.r_amount_approved = float(query.get('apprAmt', '0.0')) / 100
405#        self.context.r_desc = query.get('desc', None)
406#        if self.context.r_code != '00':
407#            self.flash(_('Unsuccessful callback: ${a}',
408#                mapping = {'a': query.get('desc', _('Incomplete query string.'))}))
409#            write_log_message(self,'unsuccessful callback: %s' % self.context.p_id)
410#            self.context.p_state = 'failed'
411#            return
412#        if self.context.r_amount_approved != payment.amount_auth:
413#            self.flash(_('Wrong amount'))
414#            write_log_message(
415#                self,'successful but wrong amount: %s' % self.context.p_id)
416#            self.context.p_state = 'failed'
417#            return
418#        try:
419#            validation_list = get_SOAP_response(
420#                PRODUCT_ID, self.context.p_id).split(':')
[7934]421            # Validation does not make sense yet since the query string
[7970]422            # formats are conflicting. We are only printing the validation
423            # string, nothing else.
[8263]424#            print 'WARNING: Webservice validation is not yet implemented'
425#            print 'validation list: %s' % validation_list
426#        except:
427#            print 'Connection to webservice failed.'
[7970]428        # Add webservice validation here
[8263]429#        write_log_message(self,'valid callback: %s' % self.context.p_id)
430#        self.context.p_state = 'paid'
[8433]431#        self.context.payment_date = datetime.utcnow()
[8263]432#        actions_after_student_payment(student, self.context, self)
433#        return
[7970]434
[8263]435#    def render(self):
436#        self.redirect(self.url(self.context, '@@index'))
437#        return
[7894]438
[8256]439# Alternative solution, replaces InterswitchPaymentCallbackPage
440class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View):
[7919]441    """ Request webservice view for the CollegePAY gateway
442    """
[8255]443    grok.context(ICustomStudentOnlinePayment)
[7919]444    grok.name('request_webservice')
445    grok.require('waeup.payStudent')
446
447    def update(self):
[8430]448        ob_class = self.__implemented__.__name__
[7919]449        if self.context.p_state == 'paid':
450            self.flash(_('This ticket has already been paid.'))
451            return
[8741]452        student = self.context.student
[8430]453        success, msg, log = query_interswitch(self.context)
[8741]454        student.writeLogMessage(self, log)
[8430]455        if not success:
456            self.flash(msg)
457            return
458        success, msg, log = self.context.doAfterStudentPayment()
459        if log is not None:
[8741]460            student.writeLogMessage(self, log)
[8430]461        self.flash(msg)
[8256]462        return
[7919]463
[8256]464    def render(self):
465        self.redirect(self.url(self.context, '@@index'))
466        return
[7926]467
[8256]468class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View):
469    """ Request webservice view for the CollegePAY gateway
470    """
471    grok.context(ICustomApplicantOnlinePayment)
472    grok.name('request_webservice')
473    grok.require('waeup.payApplicant')
[7919]474
[8256]475    def update(self):
476        if self.context.p_state == 'paid':
477            self.flash(_('This ticket has already been paid.'))
[7919]478            return
[8256]479        applicant = self.context.__parent__
[8430]480        success, msg, log = query_interswitch(self.context)
[8743]481        applicant.writeLogMessage(self, log)
[8430]482        if not success:
483            self.flash(msg)
484            return
485        success, msg, log = self.context.doAfterApplicantPayment()
486        if log is not None:
[8743]487            applicant.writeLogMessage(self, log)
[8430]488        self.flash(msg)
[7919]489        return
490
491    def render(self):
492        self.redirect(self.url(self.context, '@@index'))
[9081]493        return
Note: See TracBrowser for help on using the repository browser.