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

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

Add sundry payment category.

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