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

Last change on this file since 11136 was 10844, checked in by Henrik Bettermann, 11 years ago

Implement special application for supplementary payments.

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