source: main/waeup.kwarapoly/trunk/src/waeup/kwarapoly/interswitch/browser.py @ 11476

Last change on this file since 11476 was 11303, checked in by uli, 11 years ago

Merge changes from henrik-diazo-themed back into trunk.

  • Property svn:keywords set to Id
File size: 21.4 KB
Line 
1## $Id: browser.py 11303 2014-02-22 21:00:38Z uli $
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 getUtility, queryAdapter
23from kofacustom.nigeria.interswitch.browser import (
24    InterswitchPaymentRequestWebservicePageStudent,
25    InterswitchPaymentRequestWebservicePageApplicant
26    )
27from waeup.kofa.browser.layout import KofaPage, UtilityView
28from waeup.kofa.interfaces import IKofaUtils
29from waeup.kofa.utils.helpers import to_timezone
30from waeup.kwarapoly.students.interfaces import ICustomStudentOnlinePayment
31from waeup.kwarapoly.applicants.interfaces import ICustomApplicantOnlinePayment
32from waeup.kwarapoly.interfaces import MessageFactory as _
33
34PRODUCT_ID = '3930'
35SITE_NAME = 'kwarapoly.waeup.org'
36PROVIDER_ACCT = '1010764827'
37PROVIDER_BANK_ID = '117'
38PROVIDER_ITEM_NAME = 'BT Education'
39INSTITUTION_NAME = 'KwaraPoly'
40CURRENCY = '566'
41GATEWAY_AMT = 300.0
42#QUERY_URL = 'https://webpay.interswitchng.com/paydirect/services/TransactionQueryURL.aspx'
43#QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx'
44
45POST_ACTION = 'https://webpay.interswitchng.com/paydirect/webpay/pay.aspx'
46#POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx'
47
48HOST = 'webpay.interswitchng.com'
49#HOST = 'testwebpay.interswitchng.com'
50
51URL = '/paydirect/services/TransactionQueryWs.asmx'
52#URL = '/test_paydirect/services/TransactionQueryWs.asmx'
53httplib.HTTPConnection.debuglevel = 0
54
55class InterswitchPageStudent(KofaPage):
56    """ View which sends a POST request to the Interswitch
57    CollegePAY payment gateway.
58    """
59    grok.context(ICustomStudentOnlinePayment)
60    grok.name('goto_interswitch')
61    grok.template('student_goto_interswitch')
62    grok.require('waeup.payStudent')
63    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
64    submit_button = _('Submit')
65    action = POST_ACTION
66    site_name = SITE_NAME
67    currency = CURRENCY
68    product_id = PRODUCT_ID
69    mac = 'E6BA6CBBA9AF2871EE25C32C8D57C98895B9B001DC5B9CB2C463E2A9BDA44A3F1260C8A364F33789CDF74CB3EE7E6EF5D94F48D3AF7B727E75D97F07618DFA6D'
70
71    def interswitch_img_url(self):
72        return interswitch_img_url(self)
73
74    def update(self):
75        #if self.context.p_state != 'unpaid':
76        if self.context.p_state == 'paid':
77            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
78            self.redirect(self.url(self.context, '@@index'))
79            return
80
81        student = self.student = self.context.student
82        certificate = getattr(student['studycourse'],'certificate',None)
83        self.amount_auth = 100 * self.context.amount_auth
84        xmldict = {}
85        if certificate is not None:
86            xmldict['department'] = certificate.__parent__.__parent__.code
87            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
88        else:
89            xmldict['department'] = None
90            xmldict['faculty'] = None
91        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
92        tz = getUtility(IKofaUtils).tzinfo
93        self.local_date_time = to_timezone(
94            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
95        self.site_redirect_url = self.url(self.context, 'request_webservice')
96        # Provider data
97        xmldict['detail_ref'] = self.context.p_id
98        xmldict['provider_acct'] = PROVIDER_ACCT
99        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
100        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
101        # Institution data
102        xmldict['institution_acct'] = "0000000000000"
103        xmldict['institution_bank_id'] = '0'
104        xmldict['institution_item_name'] = self.category
105        xmldict['institution_name'] = INSTITUTION_NAME
106        xmldict['institution_amt'] = 0.0
107        dalash_amt = 200.0
108        provider_amt = 300.0
109        gateway_amt = GATEWAY_AMT
110        self.pay_item_id = '000'
111        if self.context.p_category == 'schoolfee':
112            self.pay_item_id = '101'
113            dalash_amt = 1800.0
114            provider_amt = 1200.0
115            if xmldict['faculty'] in ('CPGS',):
116                xmldict['institution_acct'] = "1771180233"
117                xmldict['institution_bank_id'] = '120'
118            elif xmldict['faculty'] in ('IBAS',):
119                xmldict['institution_acct'] = "2013910271"
120                xmldict['institution_bank_id'] = '8'
121                self.pay_item_id = '107'
122            elif xmldict['faculty'] in ('IETS',):
123                xmldict['institution_acct'] = "2013910271"
124                xmldict['institution_bank_id'] = '8'
125                self.pay_item_id = '109'
126            elif xmldict['faculty'] in ('IFMS',):
127                xmldict['institution_acct'] = "0106259811"
128                xmldict['institution_bank_id'] = '10'
129                self.pay_item_id = '106'
130            elif xmldict['faculty'] in ('ITCH',):
131                xmldict['institution_acct'] = "1771180233"
132                xmldict['institution_bank_id'] = '120'
133                self.pay_item_id = '110'
134            elif xmldict['faculty'] in ('IICT',):
135                xmldict['institution_acct'] = "2013910271"
136                xmldict['institution_bank_id'] = '8'
137                self.pay_item_id = '108'
138        elif self.context.p_category == 'certificate':
139            self.pay_item_id = '111'
140            xmldict['institution_acct'] = "0131959715"
141            xmldict['institution_bank_id'] = '10'
142        elif self.context.p_category == 'state_result':
143            self.pay_item_id = '112'
144            xmldict['institution_acct'] = "0131959715"
145            xmldict['institution_bank_id'] = '10'
146        elif self.context.p_category == 'transcript_local':
147            self.pay_item_id = '113'
148            xmldict['institution_acct'] = "0131959715"
149            xmldict['institution_bank_id'] = '10'
150        elif self.context.p_category == 'transcript_foreign':
151            self.pay_item_id = '114'
152            xmldict['institution_acct'] = "0131959715"
153            xmldict['institution_bank_id'] = '10'
154        elif self.context.p_category == 'ver_result':
155            self.pay_item_id = '115'
156            xmldict['institution_acct'] = "0131959715"
157            xmldict['institution_bank_id'] = '10'
158        elif self.context.p_category == 'change_course':
159            self.pay_item_id = '116'
160            xmldict['institution_acct'] = "0131959715"
161            xmldict['institution_bank_id'] = '10'
162        elif self.context.p_category == 'change_inst':
163            self.pay_item_id = '117'
164            xmldict['institution_acct'] = "0131959715"
165            xmldict['institution_bank_id'] = '10'
166        elif self.context.p_category == 'jamb_reject':
167            self.pay_item_id = '118'
168            xmldict['institution_acct'] = "0131959715"
169            xmldict['institution_bank_id'] = '10'
170        elif self.context.p_category == 'cert_of_cert':
171            self.pay_item_id = '119'
172            xmldict['institution_acct'] = "0131959715"
173            xmldict['institution_bank_id'] = '10'
174        elif self.context.p_category == 'ref_let':
175            self.pay_item_id = '120'
176            xmldict['institution_acct'] = "0131959715"
177            xmldict['institution_bank_id'] = '10'
178        elif self.context.p_category == 'proc_cert':
179            self.pay_item_id = '121'
180            xmldict['institution_acct'] = "0131959715"
181            xmldict['institution_bank_id'] = '10'
182        elif self.context.p_category == 'loss_idcard':
183            self.pay_item_id = '122'
184            xmldict['institution_acct'] = "0131959715"
185            xmldict['institution_bank_id'] = '10'
186            dalash_amt = 6.0
187            xmldict['dalash_amt'] = 100 * dalash_amt
188            provider_amt = 9.0
189            gateway_amt = 15.0
190        elif self.context.p_category == 'loss_examcard':
191            self.pay_item_id = '123'
192            xmldict['institution_acct'] = "0131959715"
193            xmldict['institution_bank_id'] = '10'
194            dalash_amt = 3.0
195            xmldict['dalash_amt'] = 100 * dalash_amt
196            provider_amt = 4.5
197            gateway_amt = 7.5
198        elif self.context.p_category == 'loss_result':
199            self.pay_item_id = '124'
200            xmldict['institution_acct'] = "0131959715"
201            xmldict['institution_bank_id'] = '10'
202        elif self.context.p_category == 'loss_receipt':
203            self.pay_item_id = '125'
204            xmldict['institution_acct'] = "0131959715"
205            xmldict['institution_bank_id'] = '10'
206        elif self.context.p_category == 'loss_clearance':
207            self.pay_item_id = '126'
208            xmldict['institution_acct'] = "0131959715"
209            xmldict['institution_bank_id'] = '10'
210        elif self.context.p_category == 'conv_brochure':
211            self.pay_item_id = '127'
212            xmldict['institution_acct'] = "0131959715"
213            xmldict['institution_bank_id'] = '10'
214        elif 'maintenance' in self.context.p_category:
215            self.pay_item_id = '102'
216            xmldict['institution_acct'] = "0039050937"
217            xmldict['institution_bank_id'] = '31'
218            dalash_amt = 0.0
219            provider_amt = 0.0
220
221
222        xmldict['dalash_amt'] = 100 * dalash_amt
223        xmldict['provider_amt'] = 100 * provider_amt
224        xmldict['institution_amt'] = 100 * (
225                self.context.amount_auth - provider_amt -
226                gateway_amt - dalash_amt)
227
228
229        hashargs = (
230            self.context.p_id +
231            PRODUCT_ID +
232            self.pay_item_id +
233            str(int(self.amount_auth)) +
234            self.site_redirect_url +
235            self.mac)
236        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
237
238        # Interswitch amount is not part of the xml data
239
240        if 'maintenance' in self.context.p_category:
241            xmltext = """<payment_item_detail>
242<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
243<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" />
244</item_details>
245</payment_item_detail>""" % xmldict
246
247        else:
248            xmltext = """<payment_item_detail>
249<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
250<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" />
251<item_detail item_id="2" item_name="Dalash" item_amt="%(dalash_amt)d" bank_id="117" acct_num="1013196791" />
252<item_detail item_id="3" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
253</item_details>
254</payment_item_detail>""" % xmldict
255
256        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
257        self.context.provider_amt = provider_amt
258        self.context.gateway_amt = gateway_amt
259        self.context.thirdparty_amt = dalash_amt
260        return
261
262class InterswitchPageApplicant(KofaPage):
263    """ View which sends a POST request to the Interswitch
264    CollegePAY payment gateway.
265    """
266    grok.context(ICustomApplicantOnlinePayment)
267    grok.require('waeup.payApplicant')
268    grok.template('applicant_goto_interswitch')
269    grok.name('goto_interswitch')
270    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
271    submit_button = _('Submit')
272    action = POST_ACTION
273    site_name = SITE_NAME
274    currency = CURRENCY
275    pay_item_id = '103'
276    product_id = PRODUCT_ID
277    mac = 'E6BA6CBBA9AF2871EE25C32C8D57C98895B9B001DC5B9CB2C463E2A9BDA44A3F1260C8A364F33789CDF74CB3EE7E6EF5D94F48D3AF7B727E75D97F07618DFA6D'
278
279    def interswitch_img_url(self):
280        return interswitch_img_url(self)
281
282    def update(self):
283        if self.context.p_state != 'unpaid':
284            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
285            self.redirect(self.url(self.context, '@@index'))
286            return
287        if self.context.__parent__.__parent__.expired \
288            and self.context.__parent__.__parent__.strict_deadline:
289            self.flash(_("Payment ticket can't be send to CollegePAY. "
290                         "Application period has expired."))
291            self.redirect(self.url(self.context, '@@index'))
292            return
293        self.applicant = self.context.__parent__
294        self.amount_auth = 100 * self.context.amount_auth
295        xmldict = {}
296        self.category = getUtility(
297            IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
298        tz = getUtility(IKofaUtils).tzinfo
299        self.local_date_time = to_timezone(
300            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
301        self.site_redirect_url = self.url(self.context, 'request_webservice')
302        provider_amt = 300.0
303        dalash_amt = 200.0
304        if self.applicant.applicant_id.startswith('hnd'):
305            xmldict['institution_acct'] = '7000016724'
306            xmldict['institution_bank_id'] = '9'
307        elif self.applicant.applicant_id.startswith('nd'):
308            xmldict['institution_acct'] = '2014191363'
309            xmldict['institution_bank_id'] = '8'
310        elif self.applicant.applicant_id.startswith('pgft'):
311            xmldict['institution_acct'] = '7000016724'
312            xmldict['institution_bank_id'] = '9'
313        elif self.applicant.applicant_id.startswith('prejambites'):
314            xmldict['institution_acct'] = '0106259811'
315            xmldict['institution_bank_id'] = '10'
316            self.pay_item_id = '104'
317            provider_amt = 0.0
318            dalash_amt = 0.0
319        elif self.applicant.applicant_id.startswith('pre'):
320            xmldict['institution_acct'] = '2013910271'
321            xmldict['institution_bank_id'] = '8'
322            provider_amt = 0.0
323            dalash_amt = 0.0
324        elif self.applicant.applicant_id.startswith('special'):
325            if self.applicant.special_application == 'certificate':
326                self.pay_item_id = '111'
327                xmldict['institution_acct'] = "0131959715"
328                xmldict['institution_bank_id'] = '10'
329            elif self.applicant.special_application == 'state_result':
330                self.pay_item_id = '112'
331                xmldict['institution_acct'] = "0131959715"
332                xmldict['institution_bank_id'] = '10'
333            elif self.applicant.special_application == 'transcript_local':
334                self.pay_item_id = '113'
335                xmldict['institution_acct'] = "0131959715"
336                xmldict['institution_bank_id'] = '10'
337            elif self.applicant.special_application == 'transcript_foreign':
338                self.pay_item_id = '114'
339                xmldict['institution_acct'] = "0131959715"
340                xmldict['institution_bank_id'] = '10'
341            elif self.applicant.special_application == 'ver_result':
342                self.pay_item_id = '115'
343                xmldict['institution_acct'] = "0131959715"
344                xmldict['institution_bank_id'] = '10'
345            elif self.applicant.special_application == 'change_course':
346                self.pay_item_id = '116'
347                xmldict['institution_acct'] = "0131959715"
348                xmldict['institution_bank_id'] = '10'
349            elif self.applicant.special_application == 'change_inst':
350                self.pay_item_id = '117'
351                xmldict['institution_acct'] = "0131959715"
352                xmldict['institution_bank_id'] = '10'
353            elif self.applicant.special_application == 'jamb_reject':
354                self.pay_item_id = '118'
355                xmldict['institution_acct'] = "0131959715"
356                xmldict['institution_bank_id'] = '10'
357            elif self.applicant.special_application == 'cert_of_cert':
358                self.pay_item_id = '119'
359                xmldict['institution_acct'] = "0131959715"
360                xmldict['institution_bank_id'] = '10'
361            elif self.applicant.special_application == 'ref_let':
362                self.pay_item_id = '120'
363                xmldict['institution_acct'] = "0131959715"
364                xmldict['institution_bank_id'] = '10'
365            elif self.applicant.special_application == 'proc_cert':
366                self.pay_item_id = '121'
367                xmldict['institution_acct'] = "0131959715"
368                xmldict['institution_bank_id'] = '10'
369            elif self.applicant.special_application == 'loss_idcard':
370                self.pay_item_id = '122'
371                xmldict['institution_acct'] = "0131959715"
372                xmldict['institution_bank_id'] = '10'
373                dalash_amt = 6.0
374                xmldict['dalash_amt'] = 100 * dalash_amt
375                provider_amt = 9.0
376                gateway_amt = 15.0
377            elif self.applicant.special_application == 'loss_examcard':
378                self.pay_item_id = '123'
379                xmldict['institution_acct'] = "0131959715"
380                xmldict['institution_bank_id'] = '10'
381                dalash_amt = 3.0
382                xmldict['dalash_amt'] = 100 * dalash_amt
383                provider_amt = 4.5
384                gateway_amt = 7.5
385            elif self.applicant.special_application == 'loss_result':
386                self.pay_item_id = '124'
387                xmldict['institution_acct'] = "0131959715"
388                xmldict['institution_bank_id'] = '10'
389            elif self.applicant.special_application == 'loss_receipt':
390                self.pay_item_id = '125'
391                xmldict['institution_acct'] = "0131959715"
392                xmldict['institution_bank_id'] = '10'
393            elif self.applicant.special_application == 'loss_clearance':
394                self.pay_item_id = '126'
395                xmldict['institution_acct'] = "0131959715"
396                xmldict['institution_bank_id'] = '10'
397            elif self.applicant.special_application == 'conv_brochure':
398                self.pay_item_id = '127'
399                xmldict['institution_acct'] = "0131959715"
400                xmldict['institution_bank_id'] = '10'
401            else:
402                xmldict['institution_acct'] = '00000000000'
403                xmldict['institution_bank_id'] = '00'
404        else:
405            xmldict['institution_acct'] = '00000000000'
406            xmldict['institution_bank_id'] = '00'
407        xmldict['dalash_amt'] = 100 * dalash_amt
408        xmldict['detail_ref'] = self.context.p_id
409        xmldict['provider_amt'] = 100 * provider_amt
410        xmldict['provider_acct'] = PROVIDER_ACCT
411        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
412        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
413        xmldict['institution_amt'] = 100 * (
414            self.context.amount_auth - provider_amt - GATEWAY_AMT - dalash_amt)
415        xmldict['institution_item_name'] = self.context.p_category
416        xmldict['institution_name'] = INSTITUTION_NAME
417
418        hashargs = (
419            self.context.p_id +
420            PRODUCT_ID +
421            self.pay_item_id +
422            str(int(self.amount_auth)) +
423            self.site_redirect_url +
424            self.mac)
425        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
426
427        # Interswitch amount is not part of the xml data
428
429        if not self.applicant.applicant_id.startswith('pre'):
430            xmltext = """<payment_item_detail>
431<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
432<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" />
433<item_detail item_id="2" item_name="Dalash" item_amt="%(dalash_amt)d" bank_id="117" acct_num="1013196791" />
434<item_detail item_id="3" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
435</item_details>
436</payment_item_detail>""" % xmldict
437
438        else:
439            xmltext = """<payment_item_detail>
440<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
441<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" />
442</item_details>
443</payment_item_detail>""" % xmldict
444
445        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
446        self.context.provider_amt = provider_amt
447        self.context.gateway_amt = GATEWAY_AMT
448        self.context.thirdparty_amt = dalash_amt
449        return
450
451class InterswitchPaymentRequestWebservicePageStudent(
452    InterswitchPaymentRequestWebservicePageStudent):
453    """ Request webservice view for the CollegePAY gateway
454    """
455    grok.context(ICustomStudentOnlinePayment)
456    product_id = PRODUCT_ID
457    gateway_host = HOST
458    gateway_url = URL
459
460class InterswitchPaymentRequestWebservicePageApplicant(
461    InterswitchPaymentRequestWebservicePageApplicant):
462    """ Request webservice view for the CollegePAY gateway
463    """
464    grok.context(ICustomApplicantOnlinePayment)
465    product_id = PRODUCT_ID
466    gateway_host = HOST
467    gateway_url = URL
Note: See TracBrowser for help on using the repository browser.