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

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

Remove duplicate payment category.

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