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

Last change on this file since 14113 was 14088, checked in by Henrik Bettermann, 8 years ago

Exchange account numbers.

  • Property svn:keywords set to Id
File size: 22.9 KB
Line 
1## $Id: browser.py 14088 2016-08-18 06:10:37Z 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 zope.interface import Interface
22from zope.component import queryAdapter
23from waeup.kofa.interfaces import CLEARED
24from kofacustom.nigeria.interswitch.browser import (
25    InterswitchPaymentRequestWebservicePageStudent,
26    InterswitchPaymentRequestWebservicePageApplicant,
27    InterswitchPaymentVerifyWebservicePageApplicant,
28    InterswitchPaymentVerifyWebservicePageStudent,
29    InterswitchPageStudent, InterswitchPageApplicant,
30    )
31from waeup.aaue.students.interfaces import ICustomStudentOnlinePayment
32from waeup.aaue.applicants.interfaces import ICustomApplicantOnlinePayment
33from waeup.aaue.interfaces import MessageFactory as _
34
35PRODUCT_ID_PT = '5040'
36PRODUCT_ID_REGULAR = '5845'
37SITE_NAME = 'aaue.waeup.org'
38PROVIDER_ACCT = '2022866811'
39PROVIDER_BANK_ID = '8'
40PROVIDER_ITEM_NAME = 'BT Education'
41INSTITUTION_NAME = 'AAU Ekpoma'
42CURRENCY = '566'
43GATEWAY_AMT = 250.0
44POST_ACTION = 'https://webpay.interswitchng.com/paydirect/pay'
45
46HOST = 'webpay.interswitchng.com'
47URL = '/paydirect/api/v1/gettransaction.json'
48HTTPS = True
49MAC_REGULAR = '9718FA00B0F5070B388A9896ADCED9B2FB02D30F71E12E68BDADC63F6852A3496FF97D8A0F9DA9F753B911A49BB09BB87B55FD02046BD325C74C46C0123CF023'
50MAC_PT = '74424F1DFECD6058F153148255CDD55E16724B4F380ADB2C63C5D1D7A5675759010C8153DCB930AAF2D38903CBF7CE32B8A6BA2C16BBC46721DF2E3F3E4548E3'
51
52httplib.HTTPSConnection.debuglevel = 0
53
54
55def gateway_net_amt(fee):
56    if fee > GATEWAY_AMT:
57        return fee - GATEWAY_AMT
58    return 0.0
59
60def contr_agreement_applicant(applicant):
61    if applicant.__parent__.code[:2] in ('fp', 'pt'):
62        return 'first'
63    return 'second'
64
65def contr_agreement_student(student):
66    if student.current_mode == 'found' or student.current_mode.endswith('_pt'):
67        return 'first'
68    return 'second'
69
70class CustomInterswitchPageApplicant(InterswitchPageApplicant):
71    """ View which sends a POST request to the Interswitch
72    CollegePAY payment gateway.
73
74    So far only PT application has been configured.
75    """
76    grok.context(ICustomApplicantOnlinePayment)
77    action = POST_ACTION
78    site_name = SITE_NAME
79    currency = CURRENCY
80
81    def update(self):
82
83        error = self.init_update()
84        if error:
85            self.flash(error, type='danger')
86            self.redirect(self.url(self.context, '@@index'))
87            return
88        if contr_agreement_applicant(self.context.__parent__) == 'first':
89            self.product_id = PRODUCT_ID_PT
90            self.pay_item_id = '101'
91            self.mac = MAC_PT
92        else:
93            self.product_id = PRODUCT_ID_REGULAR
94            self.pay_item_id = '109'
95            self.mac = MAC_REGULAR
96        xmldict = {}
97        provider_amt = 2000.0
98        xmldict['institution_acct'] = '1010835352'
99        xmldict['institution_bank_id'] = '117'
100        xmldict['detail_ref'] = self.context.p_id
101        xmldict['provider_amt'] = 100 * provider_amt
102        xmldict['provider_acct'] = PROVIDER_ACCT
103        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
104        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
105        xmldict['institution_amt'] = 100 * (
106            self.context.amount_auth - provider_amt - GATEWAY_AMT)
107        xmldict['institution_item_name'] = self.category
108        xmldict['institution_name'] = INSTITUTION_NAME
109        # Interswitch amount is not part of the xml data
110        xmltext = """<payment_item_detail>
111<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
112<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" />
113<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" />
114</item_details>
115</payment_item_detail>""" % xmldict
116        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
117        self.context.provider_amt = provider_amt
118        self.context.gateway_amt = GATEWAY_AMT
119
120        hashargs = (
121            self.context.p_id +
122            self.product_id +
123            self.pay_item_id +
124            str(int(self.amount_auth)) +
125            self.site_redirect_url +
126            self.mac)
127        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
128
129        return
130
131class CustomInterswitchPageStudent(InterswitchPageStudent):
132    """ View which sends a POST request to the Interswitch
133    CollegePAY payment gateway.
134    """
135    grok.context(ICustomStudentOnlinePayment)
136    action = POST_ACTION
137    site_name = SITE_NAME
138    currency = CURRENCY
139    #pay_item_id = '101'
140
141    def update(self):
142        error = self.init_update()
143
144        ######################################
145        #error = 'Sorry, Interswitch payments are temporarily disabled.'
146        ######################################
147
148        if error:
149            self.flash(error, type='danger')
150            self.redirect(self.url(self.context, '@@index'))
151            return
152
153        student = self.student
154        p_session = self.context.p_session
155        try:
156            academic_session = grok.getSite()['configuration'][str(p_session)]
157        except KeyError:
158            self.flash(_(u'Session configuration object is not available.'),
159                       type='danger')
160            self.redirect(self.url(self.context, '@@index'))
161            return
162        if contr_agreement_student(student) == 'first':
163            self.product_id = PRODUCT_ID_PT
164            self.mac = MAC_PT
165        else:
166            self.product_id = PRODUCT_ID_REGULAR
167            self.mac = MAC_REGULAR
168
169        # To guarantee that cleared students pay both acceptance fee
170        # and school fees, the page can only be accessed
171        # for school fee payments if acceptance/clearance fee has
172        # been successfully queried/paid beforehand. This
173        # requirement applies to students in state 'cleared' and
174        # entry_session greater than 2013 only.
175        if self.context.p_category.startswith('schoolfee') and \
176            student.state == CLEARED and \
177            student.entry_session > 2012:
178            acceptance_fee_paid = False
179            for ticket in student['payments'].values():
180                if ticket.p_state == 'paid' and \
181                    ticket.p_category.startswith('clearance'):
182                    acceptance_fee_paid = True
183                    break
184            if not acceptance_fee_paid:
185                self.flash(
186                    _('Please pay acceptance fee first.'), type="danger")
187                self.redirect(self.url(self.context, '@@index'))
188                return
189
190        xmldict = self.xmldict
191        xmltext = ""
192        # Provider data
193        xmldict['detail_ref'] = self.context.p_id
194        xmldict['provider_acct'] = PROVIDER_ACCT
195        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
196        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
197        xmldict['institution_item_name'] = self.category
198        xmldict['institution_name'] = INSTITUTION_NAME
199        provider_amt = 0.0
200
201        # Schoolfee
202        if self.context.p_category.startswith('schoolfee'):
203            if contr_agreement_student(student) == 'first':
204                # First agreement
205                provider_amt = 1900.0
206                joint_venture_amt = 1100.0
207                aaue_share_amt = 1000.0
208                student_union_due_amt = gateway_net_amt(
209                    academic_session.union_fee)
210                student_welfare_assurance_amt = gateway_net_amt(
211                    academic_session.welfare_fee)
212                xmldict['institution_bank_id'] = '7'
213                xmldict['institution_acct'] = '1014847058'
214                xmldict['student_union_bank_id'] = '31'
215                xmldict['student_union_acct'] = '0051005007'
216                if student.current_mode == 'found':
217                    self.pay_item_id = '103'
218                else:
219                    self.pay_item_id = '105'
220            else:
221                # Second agreement
222                provider_amt = 1500.0
223                joint_venture_amt = 1000.0
224                aaue_share_amt = 1500.0
225                student_union_due_amt = gateway_net_amt(
226                    academic_session.union_fee)
227                student_welfare_assurance_amt = gateway_net_amt(
228                    academic_session.welfare_fee)
229                xmldict['institution_bank_id'] = '117'
230                xmldict['institution_acct'] = '1010827641'
231                xmldict['student_union_bank_id'] = '123'
232                xmldict['student_union_acct'] = '1006360118'
233                self.pay_item_id = '107'
234                if student.is_postgrad:
235                    xmldict['institution_bank_id'] = '51'
236                    xmldict['institution_acct'] = '5210006575'
237                    self.pay_item_id = '111'
238
239            xmldict['provider_amt'] = 100 * provider_amt
240            xmldict['joint_venture_amt'] = 100 * joint_venture_amt
241            xmldict['aaue_share_amt'] = 100 * aaue_share_amt
242            if self.context.p_item == 'Balance':
243                xmldict['institution_amt'] = 100 * (
244                    gateway_net_amt(self.context.amount_auth))
245                xmltext = """<payment_item_detail>
246<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
247<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" />
248</item_details>
249</payment_item_detail>""" % xmldict
250            elif self.context.p_category in ('schoolfee_incl', 'schoolfee_1'):
251                # Schoolfee including additional fees
252                xmldict['student_union_due_amt'] = 100 * student_union_due_amt
253                xmldict['student_welfare_assurance_amt'] = 100 * student_welfare_assurance_amt
254                xmldict['institution_amt'] = 100 * (
255                    gateway_net_amt(self.context.amount_auth)
256                    - provider_amt
257                    - joint_venture_amt
258                    - aaue_share_amt
259                    - student_union_due_amt
260                    - student_welfare_assurance_amt)
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_detail item_id="5" item_name="Student Union" item_amt="%(student_union_due_amt)d" bank_id="%(student_union_bank_id)s" acct_num="%(student_union_acct)s" />
268<item_detail item_id="6" item_name="Student Welfare Assurance" item_amt="%(student_welfare_assurance_amt)d" bank_id="123" acct_num="1006407792" />
269</item_details>
270</payment_item_detail>""" % xmldict
271            else:
272                # Schoolfee without additional fees
273                xmldict['institution_amt'] = 100 * (
274                    gateway_net_amt(self.context.amount_auth)
275                    - provider_amt
276                    - joint_venture_amt
277                    - aaue_share_amt)
278                xmltext = """<payment_item_detail>
279<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)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_detail item_id="3" item_name="Joint Venture" item_amt="%(joint_venture_amt)d" bank_id="51" acct_num="5060023759" />
283<item_detail item_id="4" item_name="AAUE Share" item_amt="%(aaue_share_amt)d" bank_id="51" acct_num="5060020947" />
284</item_details>
285</payment_item_detail>""" % xmldict
286
287
288        # Clearance
289        elif self.context.p_category.startswith('clearance'):
290            if contr_agreement_student(student) == 'first':
291                # First agreement
292                if student.current_mode == 'found':
293                    self.pay_item_id = '102'
294                else:
295                    self.pay_item_id = '104'
296                xmldict['institution_acct'] = '1014066976'
297                xmldict['institution_bank_id'] = '117'
298            else:
299                # Second agreement
300                self.pay_item_id = '102'
301                xmldict['institution_acct'] = '1010827641'
302                xmldict['institution_bank_id'] = '117'
303                if student.is_postgrad:
304                    xmldict['institution_bank_id'] = '51'
305                    xmldict['institution_acct'] = '5210006575'
306                    self.pay_item_id = '110'
307
308            if self.context.p_category.endswith('_incl'):
309                # Clearance including additional fees
310                gown_fee_amt = gateway_net_amt(academic_session.matric_gown_fee)
311                aaue_lf_fee_amt = gateway_net_amt(academic_session.lapel_fee)
312                xmldict['gown_fee_amt'] = 100 * gown_fee_amt
313                xmldict['aaue_lf_fee_amt'] = 100 * aaue_lf_fee_amt
314                xmldict['institution_amt'] = 100 * (
315                    gateway_net_amt(self.context.amount_auth)
316                    - gown_fee_amt
317                    - aaue_lf_fee_amt)
318                xmltext = """<payment_item_detail>
319<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
320<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" />
321<item_detail item_id="2" item_name="Matriculation Gown Fee" item_amt="%(gown_fee_amt)d" bank_id="51" acct_num="5060020947" />
322<item_detail item_id="3" item_name="AAU File-Lapel Fee" item_amt="%(aaue_lf_fee_amt)d" bank_id="51" acct_num="4010660109" />
323</item_details>
324</payment_item_detail>""" % xmldict
325
326            else:
327                # Clearance without additional fees
328                xmldict['institution_amt'] = 100 * (
329                    gateway_net_amt(self.context.amount_auth))
330                xmltext = """<payment_item_detail>
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
336        # Union Dues
337        elif self.context.p_category == 'union':
338            self.pay_item_id = '103'
339            if contr_agreement_student(student) == 'first':
340                # First agreement
341                xmldict['institution_acct'] = '0051005007'
342                xmldict['institution_bank_id'] = '31'
343            else:
344                # Second agreement
345                xmldict['institution_bank_id'] = '123'
346                xmldict['institution_acct'] = '1006360118'
347            xmldict['institution_amt'] = 100 * (
348                gateway_net_amt(self.context.amount_auth))
349            xmltext = """<payment_item_detail>
350<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
351<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" />
352</item_details>
353</payment_item_detail>""" % xmldict
354
355        # Lapel/File
356        elif self.context.p_category == 'lapel':
357            self.pay_item_id = '104'
358            xmldict['institution_acct'] = '4010660109'
359            xmldict['institution_bank_id'] = '51'
360            xmldict['institution_amt'] = 100 * (
361                gateway_net_amt(self.context.amount_auth))
362            xmltext = """<payment_item_detail>
363<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
364<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" />
365</item_details>
366</payment_item_detail>""" % xmldict
367
368        # Welfare Assurance
369        elif self.context.p_category == 'welfare':
370            self.pay_item_id = '105'
371            xmldict['institution_acct'] = '1006407792'
372            xmldict['institution_bank_id'] = '123'
373            xmldict['institution_amt'] = 100 * (
374                gateway_net_amt(self.context.amount_auth))
375            xmltext = """<payment_item_detail>
376<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
377<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" />
378</item_details>
379</payment_item_detail>""" % xmldict
380
381        # Matric Gown
382        elif self.context.p_category == 'matric_gown':
383            self.pay_item_id = '106'
384            xmldict['institution_acct'] = '5060023429'
385            xmldict['institution_bank_id'] = '51'
386            xmldict['institution_amt'] = 100 * (
387                gateway_net_amt(self.context.amount_auth))
388            xmltext = """<payment_item_detail>
389<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
390<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" />
391</item_details>
392</payment_item_detail>""" % xmldict
393
394        # Concessional
395        elif self.context.p_category == 'concessional':
396            self.pay_item_id = '107'
397            xmldict['institution_acct'] = '1010835352'
398            xmldict['institution_bank_id'] = '117'
399            xmldict['institution_amt'] = 100 * (
400                gateway_net_amt(self.context.amount_auth))
401            xmltext = """<payment_item_detail>
402<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
403<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" />
404</item_details>
405</payment_item_detail>""" % xmldict
406
407        # Hostel Maintenance
408        elif self.context.p_category == 'hostel_maintenance':
409            self.pay_item_id = '109'
410            xmldict['institution_acct'] = '1006406795'
411            xmldict['institution_bank_id'] = '123'
412            xmldict['institution_amt'] = 100 * (
413                gateway_net_amt(self.context.amount_auth))
414            xmltext = """<payment_item_detail>
415<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
416<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" />
417</item_details>
418</payment_item_detail>""" % xmldict
419
420        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
421        self.context.provider_amt = provider_amt
422        self.context.gateway_amt = self.amount_auth - gateway_net_amt(
423            self.amount_auth)
424        hashargs = (
425            self.context.p_id +
426            self.product_id +
427            self.pay_item_id +
428            str(int(self.amount_auth)) +
429            self.site_redirect_url +
430            self.mac)
431        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
432        return
433
434
435class CustomInterswitchPaymentRequestWebservicePageApplicant(
436    InterswitchPaymentRequestWebservicePageApplicant):
437    """Request webservice view for the CollegePAY gateway
438    """
439    grok.context(ICustomApplicantOnlinePayment)
440    gateway_host = HOST
441    gateway_url = URL
442    https = HTTPS
443
444    @property
445    def mac(self):
446        if contr_agreement_applicant(self.context.__parent__) == 'first':
447            return MAC_PT
448        return MAC_REGULAR
449
450    @property
451    def product_id(self):
452        if contr_agreement_applicant(self.context.__parent__) == 'first':
453            return PRODUCT_ID_PT
454        return PRODUCT_ID_REGULAR
455
456class CustomInterswitchPaymentVerifyWebservicePageApplicant(
457    InterswitchPaymentVerifyWebservicePageApplicant):
458    """Payment verify view for the CollegePAY gateway
459    """
460    grok.context(ICustomApplicantOnlinePayment)
461    gateway_host = HOST
462    gateway_url = URL
463    https = HTTPS
464
465    @property
466    def mac(self):
467        if contr_agreement_applicant(self.context.__parent__) == 'first':
468            return MAC_PT
469        return MAC_REGULAR
470
471    @property
472    def product_id(self):
473        if contr_agreement_applicant(self.context.__parent__) == 'first':
474            return PRODUCT_ID_PT
475        return PRODUCT_ID_REGULAR
476
477class CustomInterswitchPaymentRequestWebservicePageStudent(
478    InterswitchPaymentRequestWebservicePageStudent):
479    """Request webservice view for the CollegePAY gateway
480    """
481    grok.context(ICustomStudentOnlinePayment)
482    gateway_host = HOST
483    gateway_url = URL
484    https = HTTPS
485
486    @property
487    def mac(self):
488        if contr_agreement_student(self.context.student) == 'first':
489            return MAC_PT
490        return MAC_REGULAR
491
492    @property
493    def product_id(self):
494        if contr_agreement_student(self.context.student) == 'first':
495            return PRODUCT_ID_PT
496        return PRODUCT_ID_REGULAR
497
498class CustomInterswitchPaymentVerifyWebservicePageStudent(
499    InterswitchPaymentVerifyWebservicePageStudent):
500    """Payment verify view for the CollegePAY gateway
501    """
502    grok.context(ICustomStudentOnlinePayment)
503    gateway_host = HOST
504    gateway_url = URL
505    https = HTTPS
506
507    @property
508    def mac(self):
509        if contr_agreement_student(self.context.student) == 'first':
510            return MAC_PT
511        return MAC_REGULAR
512
513    @property
514    def product_id(self):
515        if contr_agreement_student(self.context.student) == 'first':
516            return PRODUCT_ID_PT
517        return PRODUCT_ID_REGULAR
Note: See TracBrowser for help on using the repository browser.