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

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

We can use dynamic_gateway_amt everywhere.

  • Property svn:keywords set to Id
File size: 20.7 KB
Line 
1## $Id: browser.py 13407 2015-11-07 12:11:10Z 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        current_session = student['studycourse'].current_session
140        try:
141            academic_session = grok.getSite()[
142                'configuration'][str(current_session)]
143        except KeyError:
144            self.flash(_(u'Session configuration object is not available.'),
145                       type='danger')
146            self.redirect(self.url(self.context, '@@index'))
147            return
148        if contr_agreement(student) == 'first':
149            self.product_id = PRODUCT_ID_PT
150        else:
151            self.product_id = PRODUCT_ID_REGULAR
152
153        # To guarantee that cleared students pay both acceptance fee
154        # and school fees, the page can only be accessed
155        # for school fee payments if acceptance/clearance fee has
156        # been successfully queried/paid beforehand. This
157        # requirement applies to students in state 'cleared' and
158        # entry_session greater than 2013 only.
159        if self.context.p_category.startswith('schoolfee') and \
160            student.state == CLEARED and \
161            student.entry_session > 2012:
162            acceptance_fee_paid = False
163            for ticket in student['payments'].values():
164                if ticket.p_state == 'paid' and \
165                    ticket.p_category.startswith('clearance'):
166                    acceptance_fee_paid = True
167                    break
168            if not acceptance_fee_paid:
169                self.flash(
170                    _('Please pay acceptance fee first.'), type="danger")
171                self.redirect(self.url(self.context, '@@index'))
172                return
173
174        xmldict = self.xmldict
175        xmltext = ""
176        # Provider data
177        xmldict['detail_ref'] = self.context.p_id
178        xmldict['provider_acct'] = PROVIDER_ACCT
179        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
180        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
181        xmldict['institution_item_name'] = self.category
182        xmldict['institution_name'] = INSTITUTION_NAME
183        provider_amt = 0.0
184
185        # Schoolfee
186        if self.context.p_category.startswith('schoolfee'):
187            if contr_agreement(student) == 'first':
188                # First agreement
189                provider_amt = 1900.0
190                joint_venture_amt = 1100.0
191                aaue_share_amt = 1000.0
192                student_union_due_amt = academic_session.union_fee
193                student_welfare_assurance_amt = academic_session.welfare_fee
194                xmldict['institution_bank_id'] = '7'
195                xmldict['institution_acct'] = '1014847058'
196                if student.current_mode == 'found':
197                    self.pay_item_id = '103'
198                else:
199                    self.pay_item_id = '105'
200            else:
201                # Second agreement
202                provider_amt = 1500.0
203                joint_venture_amt = 1000.0
204                aaue_share_amt = 1500.0
205                student_union_due_amt = academic_session.union_fee
206                student_welfare_assurance_amt = academic_session.welfare_fee
207                xmldict['institution_bank_id'] = '117'
208                xmldict['institution_acct'] = '1010827641'
209                self.pay_item_id = '101'
210
211            xmldict['provider_amt'] = 100 * provider_amt
212            xmldict['joint_venture_amt'] = 100 * joint_venture_amt
213            xmldict['aaue_share_amt'] = 100 * aaue_share_amt
214            if self.context.p_category == 'schoolfee_incl':
215                # Schoolfee including additional fees
216                xmldict['student_union_due_amt'] = 100 * student_union_due_amt
217                xmldict['student_welfare_assurance_amt'] = 100 * student_welfare_assurance_amt
218                xmldict['institution_amt'] = 100 * (
219                    self.context.amount_auth
220                    - provider_amt
221                    - joint_venture_amt
222                    - aaue_share_amt
223                    - (student_union_due_amt - dynamic_gateway_amt(self.context.amount_auth))
224                    - (student_welfare_assurance_amt - dynamic_gateway_amt(self.context.amount_auth))
225                    - dynamic_gateway_amt(self.context.amount_auth))
226                xmltext = """<payment_item_detail>
227<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
228<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" />
229<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" />
230<item_detail item_id="3" item_name="Joint Venture" item_amt="%(joint_venture_amt)d" bank_id="51" acct_num="5060023759" />
231<item_detail item_id="4" item_name="AAUE Share" item_amt="%(aaue_share_amt)d" bank_id="51" acct_num="5060020947" />
232<item_detail item_id="5" item_name="Student Union" item_amt="%(student_union_due_amt)d" bank_id="123" acct_num="1006360118" />
233<item_detail item_id="6" item_name="Student Welfare Assurance" item_amt="%(student_welfare_assurance_amt)d" bank_id="31" acct_num="1006407792" />
234</item_details>
235</payment_item_detail>""" % xmldict
236            else:
237                # Schoolfee without additional fees
238                xmldict['institution_amt'] = 100 * (
239                    self.context.amount_auth
240                    - provider_amt
241                    - joint_venture_amt
242                    - aaue_share_amt
243                    - dynamic_gateway_amt(self.context.amount_auth))
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_details>
251</payment_item_detail>""" % xmldict
252
253
254        # Clearance
255        elif self.context.p_category.startswith('clearance'):
256            if contr_agreement(student) == 'first':
257                # First agreement
258                if student.current_mode == 'found':
259                    self.pay_item_id = '102'
260                else:
261                    self.pay_item_id = '104'
262                xmldict['institution_acct'] = '1014066976'
263                xmldict['institution_bank_id'] = '117'
264            else:
265                # Second agreement
266                self.pay_item_id = '102'
267                xmldict['institution_acct'] = '1010827641'
268                xmldict['institution_bank_id'] = '117'
269
270            if self.context.p_category == 'clearance_incl':
271                # Clearance including additional fees
272                gown_fee_amt = academic_session.matric_gown_fee
273                aaue_fl_fee_amt = academic_session.lapel_fee
274                xmldict['gown_fee_amt'] = 100 * gown_fee_amt
275                xmldict['aaue_fl_fee_amt'] = 100 * aaue_fl_fee_amt
276                xmldict['institution_amt'] = 100 * (
277                    self.context.amount_auth
278                    - (gown_fee_amt - dynamic_gateway_amt(self.context.amount_auth))
279                    - (aaue_fl_fee_amt - dynamic_gateway_amt(self.context.amount_auth))
280                    - dynamic_gateway_amt(self.context.amount_auth))
281                xmltext = """<payment_item_detail>
282<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
283<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" />
284<item_detail item_id="2" item_name="Matriculation Gown Fee" item_amt="%(gown_fee_amt)d" bank_id="51" acct_num="5060020947" />
285<item_detail item_id="3" item_name="AAU File-Lapel Fee" item_amt="%(aaue_fl_fee_amt)d" bank_id="51" acct_num="4010660109" />
286</item_details>
287</payment_item_detail>""" % xmldict
288
289            else:
290                # Clearance without additional fees
291                xmldict['institution_amt'] = 100 * (
292                    self.context.amount_auth
293                    - dynamic_gateway_amt(self.context.amount_auth))
294                xmltext = """<payment_item_detail>
295<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
296<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" />
297</item_details>
298</payment_item_detail>""" % xmldict
299
300        # Union Dues
301        elif self.context.p_category == 'union':
302            self.pay_item_id = '103'
303            xmldict['institution_acct'] = '1006360118'
304            xmldict['institution_bank_id'] = '123'
305            xmldict['institution_amt'] = 100 * (
306                self.context.amount_auth -
307                dynamic_gateway_amt(self.context.amount_auth))
308            xmltext = """<payment_item_detail>
309<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
310<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" />
311</item_details>
312</payment_item_detail>""" % xmldict
313
314        # Lapel/File
315        elif self.context.p_category == 'lapel':
316            self.pay_item_id = '104'
317            xmldict['institution_acct'] = '4010660109'
318            xmldict['institution_bank_id'] = '51'
319            xmldict['institution_amt'] = 100 * (
320                self.context.amount_auth -
321                dynamic_gateway_amt(self.context.amount_auth))
322            xmltext = """<payment_item_detail>
323<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
324<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" />
325</item_details>
326</payment_item_detail>""" % xmldict
327
328        # Welfare Assurance
329        elif self.context.p_category == 'welfare':
330            self.pay_item_id = '105'
331            xmldict['institution_acct'] = '1006407792'
332            xmldict['institution_bank_id'] = '123'
333            xmldict['institution_amt'] = 100 * (
334                self.context.amount_auth -
335                dynamic_gateway_amt(self.context.amount_auth))
336            xmltext = """<payment_item_detail>
337<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
338<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" />
339</item_details>
340</payment_item_detail>""" % xmldict
341
342        # Matric Gown
343        elif self.context.p_category == 'matric_gown':
344            self.pay_item_id = '106'
345            xmldict['institution_acct'] = '5060023429'
346            xmldict['institution_bank_id'] = '51'
347            xmldict['institution_amt'] = 100 * (
348                self.context.amount_auth -
349                dynamic_gateway_amt(self.context.amount_auth))
350            xmltext = """<payment_item_detail>
351<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
352<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" />
353</item_details>
354</payment_item_detail>""" % xmldict
355
356        # Concessional
357        elif self.context.p_category == 'concessional':
358            self.pay_item_id = '107'
359            xmldict['institution_acct'] = '1010835352'
360            xmldict['institution_bank_id'] = '117'
361            xmldict['institution_amt'] = 100 * (
362                self.context.amount_auth -
363                dynamic_gateway_amt(self.context.amount_auth))
364            xmltext = """<payment_item_detail>
365<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
366<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" />
367</item_details>
368</payment_item_detail>""" % xmldict
369
370        # Medical Acceptance
371        elif self.context.p_category == 'medical':
372            self.pay_item_id = '108'
373            xmldict['institution_acct'] = '1010827641'
374            xmldict['institution_bank_id'] = '117'
375            xmldict['institution_amt'] = 100 * (
376                self.context.amount_auth -
377                dynamic_gateway_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                self.context.amount_auth -
391                dynamic_gateway_amt(self.context.amount_auth))
392            xmltext = """<payment_item_detail>
393<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
394<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" />
395</item_details>
396</payment_item_detail>""" % xmldict
397
398        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
399        self.context.provider_amt = provider_amt
400        self.context.gateway_amt = GATEWAY_AMT
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    product_id = PRODUCT_ID_PT
420    gateway_host = HOST
421    gateway_url = URL
422    https = HTTPS
423
424class CustomInterswitchPaymentRequestWebservicePageStudent(
425    InterswitchPaymentRequestWebservicePageStudent):
426    """ Request webservice view for the CollegePAY gateway
427    """
428    grok.context(ICustomStudentOnlinePayment)
429    gateway_host = HOST
430    gateway_url = '/paydirect/api/v1/gettransaction.json'
431    https = HTTPS
432    mac = '9718FA00B0F5070B388A9896ADCED9B2FB02D30F71E12E68BDADC63F6852A3496FF97D8A0F9DA9F753B911A49BB09BB87B55FD02046BD325C74C46C0123CF023'
433
434    @property
435    def product_id(self):
436        if contr_agreement(self.context.student) == 'first':
437            return PRODUCT_ID_PT
438        return PRODUCT_ID_REGULAR
Note: See TracBrowser for help on using the repository browser.