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

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

Set different account details for pg students.

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