source: main/waeup.aaue/trunk/src/waeup/aaue/interswitch/browser.py @ 13563

Last change on this file since 13563 was 13532, checked in by Henrik Bettermann, 9 years ago

Configure application fee accounts.

  • Property svn:keywords set to Id
File size: 20.6 KB
RevLine 
[12730]1## $Id: browser.py 13532 2015-12-03 17:37:00Z henrik $
[11846]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##
18import httplib
19import hashlib
20import grok
21from zope.interface import Interface
22from zope.component import queryAdapter
[12975]23from waeup.kofa.interfaces import CLEARED
[11846]24from kofacustom.nigeria.interswitch.browser import (
25    InterswitchPaymentRequestWebservicePageStudent,
26    InterswitchPaymentRequestWebservicePageApplicant,
27    InterswitchPageStudent, InterswitchPageApplicant,
28    )
29from waeup.aaue.students.interfaces import ICustomStudentOnlinePayment
30from waeup.aaue.applicants.interfaces import ICustomApplicantOnlinePayment
31from waeup.aaue.interfaces import MessageFactory as _
32
[13379]33PRODUCT_ID_PT = '5040'
34PRODUCT_ID_REGULAR = '5845'
[11846]35SITE_NAME = 'aaue.waeup.org'
[13397]36PROVIDER_ACCT = '2022866811'
37PROVIDER_BANK_ID = '8'
[11846]38PROVIDER_ITEM_NAME = 'BT Education'
39INSTITUTION_NAME = 'AAU Ekpoma'
40CURRENCY = '566'
[11934]41GATEWAY_AMT = 250.0
[11933]42POST_ACTION = 'https://webpay.interswitchng.com/paydirect/pay'
[11846]43
[11933]44HOST = 'webpay.interswitchng.com'
[11916]45HTTPS = True
[11846]46
[11917]47httplib.HTTPSConnection.debuglevel = 0
[11846]48
[13379]49
[13414]50def gateway_net_amt(fee):
[13438]51    if fee > GATEWAY_AMT:
[13414]52        return fee - GATEWAY_AMT
[13438]53    return 0.0
[13406]54
[13532]55def contr_agreement_applicant(applicant):
56    if applicant.__parent__.code[:2] in ('fp', 'pt'):
57        return 'first'
58    return 'second'
59
60def contr_agreement_student(student):
[13379]61    if student.current_mode == 'found' or student.current_mode.endswith('_pt'):
[13403]62        return 'first'
63    return 'second'
[13379]64
[11846]65class CustomInterswitchPageApplicant(InterswitchPageApplicant):
66    """ View which sends a POST request to the Interswitch
67    CollegePAY payment gateway.
[13379]68
69    So far only PT application has been configured.
[11846]70    """
71    grok.context(ICustomApplicantOnlinePayment)
72    action = POST_ACTION
73    site_name = SITE_NAME
74    currency = CURRENCY
[11933]75    mac = '74424F1DFECD6058F153148255CDD55E16724B4F380ADB2C63C5D1D7A5675759010C8153DCB930AAF2D38903CBF7CE32B8A6BA2C16BBC46721DF2E3F3E4548E3'
[11846]76
77    def update(self):
78
[12975]79        error = self.init_update()
80        if error:
81            self.flash(error, type='danger')
82            self.redirect(self.url(self.context, '@@index'))
83            return
[13532]84        if contr_agreement_applicant(self.context.__parent__) == 'first':
85            self.product_id = PRODUCT_ID_PT
86            self.pay_item_id = '101'
87        else:
88            self.product_id = PRODUCT_ID_REGULAR
89            self.pay_item_id = '109'
[11846]90        xmldict = {}
91        provider_amt = 1000.0
92        xmldict['institution_acct'] = '1010835352'
93        xmldict['institution_bank_id'] = '117'
94        xmldict['detail_ref'] = self.context.p_id
95        xmldict['provider_amt'] = 100 * provider_amt
96        xmldict['provider_acct'] = PROVIDER_ACCT
97        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
98        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
[13414]99        xmldict['institution_amt'] = 100 * (
100            self.context.amount_auth - provider_amt - GATEWAY_AMT)
[13532]101        xmldict['institution_item_name'] = self.category
[11846]102        xmldict['institution_name'] = INSTITUTION_NAME
103        # Interswitch amount is not part of the xml data
104        xmltext = """<payment_item_detail>
105<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
106<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" />
107<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" />
108</item_details>
109</payment_item_detail>""" % xmldict
110        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
111        self.context.provider_amt = provider_amt
112        self.context.gateway_amt = GATEWAY_AMT
113
114        hashargs = (
115            self.context.p_id +
[13532]116            self.product_id +
[11846]117            self.pay_item_id +
118            str(int(self.amount_auth)) +
119            self.site_redirect_url +
120            self.mac)
121        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
122
123        return
124
[11868]125class CustomInterswitchPageStudent(InterswitchPageStudent):
126    """ View which sends a POST request to the Interswitch
127    CollegePAY payment gateway.
128    """
129    grok.context(ICustomStudentOnlinePayment)
130    action = POST_ACTION
131    site_name = SITE_NAME
132    currency = CURRENCY
[13532]133    #pay_item_id = '101'
[13381]134    #mac = '74424F1DFECD6058F153148255CDD55E16724B4F380ADB2C63C5D1D7A5675759010C8153DCB930AAF2D38903CBF7CE32B8A6BA2C16BBC46721DF2E3F3E4548E3'
135    mac = '9718FA00B0F5070B388A9896ADCED9B2FB02D30F71E12E68BDADC63F6852A3496FF97D8A0F9DA9F753B911A49BB09BB87B55FD02046BD325C74C46C0123CF023'
[11846]136
[11868]137    def update(self):
[12975]138        error = self.init_update()
[13376]139
140        ######################################
[13381]141        #error = 'Sorry, Interswitch payments are temporarily disabled.'
[13376]142        ######################################
143
[12975]144        if error:
145            self.flash(error, type='danger')
146            self.redirect(self.url(self.context, '@@index'))
147            return
[13400]148
[11868]149        student = self.student
[13408]150        p_session = self.context.p_session
[13400]151        try:
[13408]152            academic_session = grok.getSite()['configuration'][str(p_session)]
[13400]153        except KeyError:
[13407]154            self.flash(_(u'Session configuration object is not available.'),
155                       type='danger')
[13400]156            self.redirect(self.url(self.context, '@@index'))
157            return
[13532]158        if contr_agreement_student(student) == 'first':
[13382]159            self.product_id = PRODUCT_ID_PT
160        else:
161            self.product_id = PRODUCT_ID_REGULAR
162
[12975]163        # To guarantee that cleared students pay both acceptance fee
164        # and school fees, the page can only be accessed
165        # for school fee payments if acceptance/clearance fee has
166        # been successfully queried/paid beforehand. This
167        # requirement applies to students in state 'cleared' and
168        # entry_session greater than 2013 only.
[13400]169        if self.context.p_category.startswith('schoolfee') and \
[12975]170            student.state == CLEARED and \
171            student.entry_session > 2012:
172            acceptance_fee_paid = False
173            for ticket in student['payments'].values():
174                if ticket.p_state == 'paid' and \
[13400]175                    ticket.p_category.startswith('clearance'):
[12975]176                    acceptance_fee_paid = True
177                    break
178            if not acceptance_fee_paid:
179                self.flash(
180                    _('Please pay acceptance fee first.'), type="danger")
181                self.redirect(self.url(self.context, '@@index'))
182                return
183
[11868]184        xmldict = self.xmldict
[12729]185        xmltext = ""
[11868]186        # Provider data
187        xmldict['detail_ref'] = self.context.p_id
188        xmldict['provider_acct'] = PROVIDER_ACCT
189        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
190        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
191        xmldict['institution_item_name'] = self.category
192        xmldict['institution_name'] = INSTITUTION_NAME
[13381]193        provider_amt = 0.0
[12729]194
[13400]195        # Schoolfee
196        if self.context.p_category.startswith('schoolfee'):
[13532]197            if contr_agreement_student(student) == 'first':
[13403]198                # First agreement
[13400]199                provider_amt = 1900.0
200                joint_venture_amt = 1100.0
201                aaue_share_amt = 1000.0
[13414]202                student_union_due_amt = gateway_net_amt(
203                    academic_session.union_fee)
204                student_welfare_assurance_amt = gateway_net_amt(
205                    academic_session.welfare_fee)
[13379]206                xmldict['institution_bank_id'] = '7'
207                xmldict['institution_acct'] = '1014847058'
208                if student.current_mode == 'found':
209                    self.pay_item_id = '103'
210                else:
211                    self.pay_item_id = '105'
[13400]212            else:
[13403]213                # Second agreement
[13400]214                provider_amt = 1500.0
215                joint_venture_amt = 1000.0
216                aaue_share_amt = 1500.0
[13414]217                student_union_due_amt = gateway_net_amt(
218                    academic_session.union_fee)
219                student_welfare_assurance_amt = gateway_net_amt(
220                    academic_session.welfare_fee)
[13400]221                xmldict['institution_bank_id'] = '117'
222                xmldict['institution_acct'] = '1010827641'
223                self.pay_item_id = '101'
[13527]224                if student.is_postgrad:
225                    xmldict['institution_bank_id'] = '51'
226                    xmldict['institution_acct'] = '5210006575'
227                    self.pay_item_id = '111'
[13400]228
229            xmldict['provider_amt'] = 100 * provider_amt
230            xmldict['joint_venture_amt'] = 100 * joint_venture_amt
231            xmldict['aaue_share_amt'] = 100 * aaue_share_amt
[13512]232            if self.context.p_category in ('schoolfee_incl', 'schoolfee_1'):
[13400]233                # Schoolfee including additional fees
[13379]234                xmldict['student_union_due_amt'] = 100 * student_union_due_amt
235                xmldict['student_welfare_assurance_amt'] = 100 * student_welfare_assurance_amt
236                xmldict['institution_amt'] = 100 * (
[13414]237                    gateway_net_amt(self.context.amount_auth)
[13379]238                    - provider_amt
239                    - joint_venture_amt
240                    - aaue_share_amt
[13409]241                    - student_union_due_amt
[13414]242                    - student_welfare_assurance_amt)
[13379]243                xmltext = """<payment_item_detail>
[11868]244<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
245<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" />
[12729]246<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" />
247<item_detail item_id="3" item_name="Joint Venture" item_amt="%(joint_venture_amt)d" bank_id="51" acct_num="5060023759" />
248<item_detail item_id="4" item_name="AAUE Share" item_amt="%(aaue_share_amt)d" bank_id="51" acct_num="5060020947" />
249<item_detail item_id="5" item_name="Student Union" item_amt="%(student_union_due_amt)d" bank_id="123" acct_num="1006360118" />
250<item_detail item_id="6" item_name="Student Welfare Assurance" item_amt="%(student_welfare_assurance_amt)d" bank_id="31" acct_num="1006407792" />
[11868]251</item_details>
252</payment_item_detail>""" % xmldict
[13400]253            else:
254                # Schoolfee without additional fees
255                xmldict['institution_amt'] = 100 * (
[13414]256                    gateway_net_amt(self.context.amount_auth)
[13400]257                    - provider_amt
258                    - joint_venture_amt
[13414]259                    - aaue_share_amt)
[13400]260                xmltext = """<payment_item_detail>
261<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
262<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" />
263<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" />
264<item_detail item_id="3" item_name="Joint Venture" item_amt="%(joint_venture_amt)d" bank_id="51" acct_num="5060023759" />
265<item_detail item_id="4" item_name="AAUE Share" item_amt="%(aaue_share_amt)d" bank_id="51" acct_num="5060020947" />
266</item_details>
267</payment_item_detail>""" % xmldict
268
269
270        # Clearance
271        elif self.context.p_category.startswith('clearance'):
[13532]272            if contr_agreement_student(student) == 'first':
[13403]273                # First agreement
[13379]274                if student.current_mode == 'found':
275                    self.pay_item_id = '102'
276                else:
277                    self.pay_item_id = '104'
278                xmldict['institution_acct'] = '1014066976'
279                xmldict['institution_bank_id'] = '117'
[13400]280            else:
[13403]281                # Second agreement
[13400]282                self.pay_item_id = '102'
283                xmldict['institution_acct'] = '1010827641'
284                xmldict['institution_bank_id'] = '117'
[13527]285                if student.is_postgrad:
286                    xmldict['institution_bank_id'] = '51'
287                    xmldict['institution_acct'] = '5210006575'
288                    self.pay_item_id = '110'
[13400]289
[13410]290            if self.context.p_category.endswith('_incl'):
[13400]291                # Clearance including additional fees
[13414]292                gown_fee_amt = gateway_net_amt(academic_session.matric_gown_fee)
293                aaue_lf_fee_amt = gateway_net_amt(academic_session.lapel_fee)
[13379]294                xmldict['gown_fee_amt'] = 100 * gown_fee_amt
[13414]295                xmldict['aaue_lf_fee_amt'] = 100 * aaue_lf_fee_amt
[13379]296                xmldict['institution_amt'] = 100 * (
[13414]297                    gateway_net_amt(self.context.amount_auth)
[13409]298                    - gown_fee_amt
[13414]299                    - aaue_lf_fee_amt)
[13379]300                xmltext = """<payment_item_detail>
[11868]301<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
302<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" />
[12729]303<item_detail item_id="2" item_name="Matriculation Gown Fee" item_amt="%(gown_fee_amt)d" bank_id="51" acct_num="5060020947" />
[13414]304<item_detail item_id="3" item_name="AAU File-Lapel Fee" item_amt="%(aaue_lf_fee_amt)d" bank_id="51" acct_num="4010660109" />
[11868]305</item_details>
306</payment_item_detail>""" % xmldict
[13400]307
[13381]308            else:
[13400]309                # Clearance without additional fees
[13381]310                xmldict['institution_amt'] = 100 * (
[13414]311                    gateway_net_amt(self.context.amount_auth))
[13381]312                xmltext = """<payment_item_detail>
313<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
314<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" />
315</item_details>
316</payment_item_detail>""" % xmldict
[13379]317
[13403]318        # Union Dues
[13400]319        elif self.context.p_category == 'union':
320            self.pay_item_id = '103'
321            xmldict['institution_acct'] = '1006360118'
322            xmldict['institution_bank_id'] = '123'
323            xmldict['institution_amt'] = 100 * (
[13414]324                gateway_net_amt(self.context.amount_auth))
[13400]325            xmltext = """<payment_item_detail>
[13381]326<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
327<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" />
328</item_details>
329</payment_item_detail>""" % xmldict
[13379]330
[13403]331        # Lapel/File
[13400]332        elif self.context.p_category == 'lapel':
333            self.pay_item_id = '104'
334            xmldict['institution_acct'] = '4010660109'
335            xmldict['institution_bank_id'] = '51'
336            xmldict['institution_amt'] = 100 * (
[13414]337                gateway_net_amt(self.context.amount_auth))
[13400]338            xmltext = """<payment_item_detail>
[13381]339<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
340<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" />
341</item_details>
342</payment_item_detail>""" % xmldict
343
[13403]344        # Welfare Assurance
[13400]345        elif self.context.p_category == 'welfare':
346            self.pay_item_id = '105'
347            xmldict['institution_acct'] = '1006407792'
348            xmldict['institution_bank_id'] = '123'
349            xmldict['institution_amt'] = 100 * (
[13414]350                gateway_net_amt(self.context.amount_auth))
[13400]351            xmltext = """<payment_item_detail>
[13381]352<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
353<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" />
354</item_details>
355</payment_item_detail>""" % xmldict
356
[13400]357        # Matric Gown
358        elif self.context.p_category == 'matric_gown':
359            self.pay_item_id = '106'
360            xmldict['institution_acct'] = '5060023429'
361            xmldict['institution_bank_id'] = '51'
362            xmldict['institution_amt'] = 100 * (
[13414]363                gateway_net_amt(self.context.amount_auth))
[13400]364            xmltext = """<payment_item_detail>
[13381]365<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
366<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" />
367</item_details>
368</payment_item_detail>""" % xmldict
369
[13400]370        # Concessional
371        elif self.context.p_category == 'concessional':
372            self.pay_item_id = '107'
373            xmldict['institution_acct'] = '1010835352'
374            xmldict['institution_bank_id'] = '117'
375            xmldict['institution_amt'] = 100 * (
[13414]376                gateway_net_amt(self.context.amount_auth))
[13400]377            xmltext = """<payment_item_detail>
[13381]378<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
379<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" />
380</item_details>
381</payment_item_detail>""" % xmldict
382
[13400]383        # Hostel Maintenance
384        elif self.context.p_category == 'hostel_maintenance':
385            self.pay_item_id = '109'
386            xmldict['institution_acct'] = '1006406795'
387            xmldict['institution_bank_id'] = '123'
388            xmldict['institution_amt'] = 100 * (
[13414]389                gateway_net_amt(self.context.amount_auth))
[13400]390            xmltext = """<payment_item_detail>
[13383]391<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
392<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" />
393</item_details>
394</payment_item_detail>""" % xmldict
395
[11868]396        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
397        self.context.provider_amt = provider_amt
[13437]398        self.context.gateway_amt = self.amount_auth - gateway_net_amt(
399            self.amount_auth)
[11868]400        hashargs = (
401            self.context.p_id +
[13382]402            self.product_id +
[11868]403            self.pay_item_id +
404            str(int(self.amount_auth)) +
405            self.site_redirect_url +
406            self.mac)
407        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
408        return
409
410
[11846]411class CustomInterswitchPaymentRequestWebservicePageApplicant(
412    InterswitchPaymentRequestWebservicePageApplicant):
413    """ Request webservice view for the CollegePAY gateway
[13379]414
415    So far only PT application has been configured.
[11846]416    """
417    grok.context(ICustomApplicantOnlinePayment)
418    gateway_host = HOST
[13532]419    gateway_url = '/paydirect/api/v1/gettransaction.json'
[11916]420    https = HTTPS
[13532]421    mac = '74424F1DFECD6058F153148255CDD55E16724B4F380ADB2C63C5D1D7A5675759010C8153DCB930AAF2D38903CBF7CE32B8A6BA2C16BBC46721DF2E3F3E4548E3'
[11868]422
[13532]423    @property
424    def product_id(self):
425        if contr_agreement_applicant(self.context.__parent__) == 'first':
426            return PRODUCT_ID_PT
427        return PRODUCT_ID_REGULAR
428
[11868]429class CustomInterswitchPaymentRequestWebservicePageStudent(
430    InterswitchPaymentRequestWebservicePageStudent):
431    """ Request webservice view for the CollegePAY gateway
432    """
433    grok.context(ICustomStudentOnlinePayment)
434    gateway_host = HOST
[13388]435    gateway_url = '/paydirect/api/v1/gettransaction.json'
[11916]436    https = HTTPS
[13388]437    mac = '9718FA00B0F5070B388A9896ADCED9B2FB02D30F71E12E68BDADC63F6852A3496FF97D8A0F9DA9F753B911A49BB09BB87B55FD02046BD325C74C46C0123CF023'
[13379]438
439    @property
440    def product_id(self):
[13532]441        if contr_agreement_student(self.context.student) == 'first':
[13379]442            return PRODUCT_ID_PT
443        return PRODUCT_ID_REGULAR
Note: See TracBrowser for help on using the repository browser.