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

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

pyflakes

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