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

Last change on this file since 18334 was 18334, checked in by Henrik Bettermann, 3 months ago

Add folder payment_category.

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