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

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

Replace mac key for application payments.

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