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

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

Set different account details for pg students.

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