source: main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/interswitch/browser.py @ 18034

Last change on this file since 18034 was 18034, checked in by Henrik Bettermann, 38 hours ago

Implement required combi split payments.

  • Property svn:keywords set to Id
File size: 15.5 KB
RevLine 
[10765]1## $Id: browser.py 18034 2025-03-10 16:40:53Z 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##
18import httplib
[12479]19import hashlib
[10765]20import grok
[18034]21from waeup.kofa.students.interfaces import IStudentsUtils
22from zope.component import getUtility
[10765]23from kofacustom.nigeria.interswitch.browser import (
[15999]24    module_activated,
[10765]25    InterswitchPaymentRequestWebservicePageApplicant,
[11638]26    InterswitchPaymentRequestWebservicePageStudent,
[13587]27    InterswitchPaymentVerifyWebservicePageApplicant,
28    InterswitchPaymentVerifyWebservicePageStudent,
[11638]29    InterswitchPageStudent, InterswitchPageApplicant,
[10765]30    )
[15563]31from kofacustom.iuokada.students.interfaces import ICustomStudentOnlinePayment
32from kofacustom.iuokada.applicants.interfaces import ICustomApplicantOnlinePayment
33from kofacustom.iuokada.interfaces import MessageFactory as _
[10765]34
[15728]35PRODUCT_ID = '7922'
[16624]36SITE_NAME = 'iuokada.waeup.org'
[15645]37PROVIDER_ACCT = '0773411069'
38PROVIDER_BANK_ID = '31'
39PROVIDER_ITEM_NAME = 'WAeAC'
[15563]40INSTITUTION_NAME = 'IUOkada'
[10765]41CURRENCY = '566'
[15728]42GATEWAY_AMT = 250.0
43#MAC = 'CEF793CBBE838AA0CBB29B74D571113B4EA6586D3BA77E7CFA0B95E278364EFC4526ED7BD255A366CDDE11F1F607F0F844B09D93B16F7CFE87563B2272007AB3' # must be provided by Interswitch
44MAC = '4D8723F33D728BE3F4A77B2DFB3F57B0BCF2DD818759D54A87B2A60874067F28D94F2B91E29BFB884F920F48E0973D826835A8F2D6F74220C64EDE4DE00E45AA'
[10765]45
[15728]46POST_ACTION = 'https://webpay.interswitchng.com/paydirect/pay'
47#POST_ACTION = 'https://sandbox.interswitchng.com/webpay/pay'
48HOST = 'webpay.interswitchng.com'
49#HOST = 'sandbox.interswitchng.com'
50URL = '/paydirect/api/v1/gettransaction.json'
51#URL = '/webpay/api/v1/gettransaction.json'
[14276]52
[12479]53httplib.HTTPSConnection.debuglevel = 0
54HTTPS = True
[10765]55
[18034]56BANK_ACCOUNTS = {
57    'access':               ('1228744877', '117'),
58    'parentsconsult':       ('1228747029', '117'),
59    'health_insurance':     ('1228744884', '117'),
60    'municipal_returning':  ('0040621193','31'),
61    'clearance':            ('0040621193','31'),
62    'develop':              ('0040621193','31'),
63    'medical_screening':    ('1311220657','117'),
64    'conv':                 ('0040621193','31'),
65    'registration_fresh':   ('0040621193','31'),
66    'science':              ('0040621193','31'),
67    'id_card':              ('0040621193','31'),
68    'alumni':               ('1311974981','117'),
69    'lab_support':          ('0040621193','31'),
70    'registration_return':  ('0040621193','31'),
71    'book':                 ('1228744877','117'),
72    'waecneco':             ('0040621193','31'),
73    'jambver':              ('0040621193','31'),
74    'pg_other':             ('0040621193','31'),
75    'municipal_fresh':      ('0040621193','31'),
76    'matric':               ('0040621193','31')
77    }
78
[11638]79class CustomInterswitchPageStudent(InterswitchPageStudent):
[10765]80    """ View which sends a POST request to the Interswitch
81    CollegePAY payment gateway.
82    """
83    grok.context(ICustomStudentOnlinePayment)
84    action = POST_ACTION
85    site_name = SITE_NAME
86    currency = CURRENCY
87    product_id = PRODUCT_ID
[15269]88    mac = MAC 
[15773]89    gateway_amt = GATEWAY_AMT
[10765]90
91    def update(self):
[18034]92        student_utils = getUtility(IStudentsUtils)
[15999]93        if not module_activated(
94            self.context.student.current_session, self.context):
95            self.flash(_('Forbidden'), type='danger')
96            self.redirect(self.url(self.context, '@@index'))
97            return
[12979]98        error = self.init_update()
99        if error:
100            self.flash(error, type='danger')
101            self.redirect(self.url(self.context, '@@index'))
102            return
[15773]103        # Already now it becomes an Interswitch payment. We set the net amount
104        # and add the gateway amount.
105        if not self.context.r_company:
106            self.context.net_amt = self.context.amount_auth
107            self.context.amount_auth += self.gateway_amt
108            self.context.gateway_amt = self.gateway_amt
109            self.context.r_company = u'interswitch'
[11647]110        student = self.student
111        xmldict = self.xmldict
[10765]112        # Provider data
113        xmldict['detail_ref'] = self.context.p_id
114        xmldict['provider_acct'] = PROVIDER_ACCT
115        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
116        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
[15728]117        xmldict['institution_acct'] = ''
118        xmldict['institution_bank_id'] = ''
[15773]119        provider_amt = 0.0
[15728]120        self.pay_item_id = ''
[10765]121        # Institution data
[15728]122        if self.context.p_category in (
123            'schoolfee', 'schoolfee40', 'secondinstal'):
124            self.pay_item_id = '101'
125        else:
[18034]126            # we use the same item_id for all sundry payments
[15728]127            self.pay_item_id = '102'
[16233]128        if self.context.p_category in (
[17900]129            'registration', 'registration_fresh', 'registration_return',
130            'required_combi', 'pg_other', 'jupeb_reg'):
[15728]131            provider_amt = 5000.0
[16047]132        if self.context.p_category in (
133            'schoolfee', 'schoolfee40') and student.is_jupeb:
134            provider_amt = 5000.0
[16395]135        if self.context.p_category.startswith('jupeb'):
136            self.pay_item_id = '102'
[10765]137        xmldict['provider_amt'] = 100 * provider_amt
[12511]138        xmldict['institution_item_name'] = self.context.category
[10765]139        xmldict['institution_name'] = INSTITUTION_NAME
[16270]140        xmldict['institution_amt'] = 100 * self.context.net_amt - (
141            100 * provider_amt)
[16434]142
[17672]143        if grok.getSite().__name__ == 'iuokada-cdl':
144            xmldict['institution_acct'] = '1229771245'
[16434]145            xmldict['institution_bank_id'] = '117'
[17672]146            if self.context.p_category.startswith('schoolfee'):
147                xmldict['institution_acct'] = '1828332878'
148                xmldict['institution_bank_id'] = '31'
[16434]149        else:
[17672]150            if self.context.p_option == 'access':
151                xmldict['institution_acct'] = '0040484781'
152                xmldict['institution_bank_id'] = '31'
153            elif self.context.p_option == 'first':
154                xmldict['institution_acct'] = '2021420049'
155                xmldict['institution_bank_id'] = '8'
156            elif self.context.p_option == 'zenith':
157                xmldict['institution_acct'] = '1011005811'
158                xmldict['institution_bank_id'] = '117'
159            else:
160                xmldict['institution_acct'] = '0040484781'
161                xmldict['institution_bank_id'] = '31'
[16434]162
[17672]163            # Overwrite above selection
[18034]164            sundry_acct = BANK_ACCOUNTS.get(self.context.p_category, None)
165            if sundry_acct is True:
166                xmldict['institution_acct'] = sundry_acct[0]
167                xmldict['institution_bank_id'] = sundry_acct[0]
[17474]168
[10765]169        if provider_amt == 0:
170            xmltext = """<payment_item_detail>
171<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
172<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" />
173</item_details>
174</payment_item_detail>""" % xmldict
175        else:
176            xmltext = """<payment_item_detail>
177<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
178<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" />
179<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" />
180</item_details>
181</payment_item_detail>""" % xmldict
[18034]182
183        # Overwrite xmltext above because we have to split required combi
184        if self.context.p_category == 'required_combi':
185            rp = student_utils._collect_required_payment_items(self.context.student)
186            academic_session = student_utils._getSessionConfiguration(self.context.p_session)
187            xmltext = """<payment_item_detail>
188<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
189<item_detail item_id="1" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />""" % xmldict
190            item_id = 2
191            for cat in rp:
192                fee_name = cat + '_fee'
193                item_name = cat
194                item_amt = 100 * getattr(academic_session, fee_name, 0.0)
195                if item_name.startswith('registration'):
196                    item_amt -= 100 * provider_amt
197                bank_id = BANK_ACCOUNTS[cat][1]
198                acct_num = BANK_ACCOUNTS[cat][0]
199                xmltext += """
200<item_detail item_id="%s" item_name="%s" item_amt="%d" bank_id="%s" acct_num="%s" />""" % (item_id, item_name, item_amt, bank_id, acct_num)
201                item_id += 1
202            xmltext += """
203</item_details>
204</payment_item_detail>""" % xmldict
205
[10765]206        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
[15773]207        self.amount_auth = int(100 * self.context.amount_auth)
[12479]208        hashargs = (
209            self.context.p_id +
210            PRODUCT_ID +
211            self.pay_item_id +
212            str(int(self.amount_auth)) +
213            self.site_redirect_url +
214            self.mac)
215        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
[10765]216        return
217
[11638]218class CustomInterswitchPageApplicant(InterswitchPageApplicant):
[10765]219    """ View which sends a POST request to the Interswitch
220    CollegePAY payment gateway.
221    """
222    grok.context(ICustomApplicantOnlinePayment)
223    action = POST_ACTION
224    site_name = SITE_NAME
225    currency = CURRENCY
226    product_id = PRODUCT_ID
[15269]227    mac = MAC
[15773]228    gateway_amt = GATEWAY_AMT
[10765]229
230    def update(self):
[15999]231        if not module_activated(
232            self.context.__parent__.__parent__.year, self.context):
233            self.flash(_('Forbidden'), type='danger')
234            self.redirect(self.url(self.context, '@@index'))
235            return
[12979]236        error = self.init_update()
237        if error:
238            self.flash(error, type='danger')
239            self.redirect(self.url(self.context, '@@index'))
240            return
[15773]241        # Already now it becomes an Interswitch payment. We set the net amount
242        # and add the gateway amount.
243        if not self.context.r_company:
244            self.context.net_amt = self.context.amount_auth
245            self.context.amount_auth += self.gateway_amt
246            self.context.gateway_amt = self.gateway_amt
247            self.context.r_company = u'interswitch'
248        self.amount_auth = int(100 * self.context.amount_auth)
[10765]249        xmldict = {}
[15773]250        provider_amt = 0.0
[15786]251        self.pay_item_id = '101'
[15783]252        xmldict['institution_acct'] = '1011005811'
253        xmldict['institution_bank_id'] = '117'
[17672]254        if grok.getSite().__name__ == 'iuokada-cdl':
255            xmldict['institution_acct'] = '1229774710'
256            xmldict['institution_bank_id'] = '117'
[10765]257        xmldict['detail_ref'] = self.context.p_id
258        xmldict['provider_amt'] = 100 * provider_amt
259        xmldict['provider_acct'] = PROVIDER_ACCT
260        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
261        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
[12511]262        xmldict['institution_item_name'] = self.context.category
[10765]263        xmldict['institution_name'] = INSTITUTION_NAME
[15773]264        xmldict['institution_amt'] = 100 * self.context.net_amt
265        if not self.context.provider_amt:
266            self.context.provider_amt = provider_amt
267            self.context.amount_auth += provider_amt
[15784]268        if provider_amt == 0:
269            xmltext = """<payment_item_detail>
[15785]270<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
[10765]271<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" />
[15784]272</item_details>
273</payment_item_detail>""" % xmldict
274        else:
275            xmltext = """<payment_item_detail>
[15785]276<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
[15784]277<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" />
[10765]278<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" />
279</item_details>
280</payment_item_detail>""" % xmldict
281        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
[15773]282        self.amount_auth = int(100 * self.context.amount_auth)
[12479]283        hashargs = (
284            self.context.p_id +
285            PRODUCT_ID +
[15786]286            self.pay_item_id +
[12479]287            str(int(self.amount_auth)) +
288            self.site_redirect_url +
289            self.mac)
290        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
[10765]291        return
292
[11638]293class CustomInterswitchPaymentRequestWebservicePageStudent(
[10765]294    InterswitchPaymentRequestWebservicePageStudent):
[13587]295    """Request webservice view for the CollegePAY gateway
[10765]296    """
297    grok.context(ICustomStudentOnlinePayment)
298    product_id = PRODUCT_ID
299    gateway_host = HOST
300    gateway_url = URL
[15269]301    mac = MAC
[10765]302
[13587]303class CustomInterswitchPaymentVerifyWebservicePageStudent(
304    InterswitchPaymentVerifyWebservicePageStudent):
305    """Payment verify view for the CollegePAY gateway
306    """
307    grok.context(ICustomStudentOnlinePayment)
308    product_id = PRODUCT_ID
309    gateway_host = HOST
310    gateway_url = URL
[15269]311    mac = MAC
[13587]312
[11638]313class CustomInterswitchPaymentRequestWebservicePageApplicant(
[10765]314    InterswitchPaymentRequestWebservicePageApplicant):
[13587]315    """Request webservice view for the CollegePAY gateway
[10765]316    """
317    grok.context(ICustomApplicantOnlinePayment)
318    product_id = PRODUCT_ID
319    gateway_host = HOST
[12479]320    gateway_url = URL
[15269]321    mac = MAC
[13587]322
323class CustomInterswitchPaymentVerifyWebservicePageApplicant(
324    InterswitchPaymentVerifyWebservicePageApplicant):
325    """Payment verify view for the CollegePAY gateway
326    """
327    grok.context(ICustomApplicantOnlinePayment)
328    product_id = PRODUCT_ID
329    gateway_host = HOST
[15269]330    gateway_url = URL
[16490]331    mac = MAC
332
333
334# PAYDirect added on 19/05/2021
335
[16706]336from kofacustom.nigeria.interswitch.paydirectbrowser import (
337    PAYDirectPageStudent, PAYDirectPageApplicant,
338    StudentRefNumberSlip, ApplicantRefNumberSlip,
339    )
340
[16574]341PAYDIRECT_HOST = 'orion.interswitchng.com'
342PAYDIRECT_URL = '/bookonhold/bookonhold.asmx'
343MERCHANT_ID = '8124'
344
[16490]345class CustomPAYDirectPageStudent(PAYDirectPageStudent):
346    """Inform student how to proceed
347    """
348    grok.context(ICustomStudentOnlinePayment)
349    gateway_amt = GATEWAY_AMT
[16574]350    merchant_id = MERCHANT_ID
351    gateway_url = PAYDIRECT_URL
352    gateway_host = PAYDIRECT_HOST
[16490]353    https = True
354
355class CustomPAYDirectPageApplicant(PAYDirectPageApplicant):
356    """ Inform applicant how to proceed.
357    """
358    grok.context(ICustomApplicantOnlinePayment)
359    gateway_amt = GATEWAY_AMT
[16574]360    merchant_id = MERCHANT_ID
361    gateway_url = PAYDIRECT_URL
362    gateway_host = PAYDIRECT_HOST
[16490]363    https = True
[16574]364
365class CustomStudentRefNumberSlip(StudentRefNumberSlip):
366    """Deliver a PDF slip of the context.
367    """
368    merchant_id = MERCHANT_ID
369
370class CustomApplicantRefNumberSlip(ApplicantRefNumberSlip):
371    """Deliver a PDF slip of the context.
372    """
373    merchant_id = MERCHANT_ID
Note: See TracBrowser for help on using the repository browser.