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

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

Replace mac key for application payments.

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