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

Last change on this file since 10669 was 10667, checked in by Henrik Bettermann, 12 years ago

Change account details and use different pay item ids.

  • Property svn:keywords set to Id
File size: 13.9 KB
RevLine 
[7894]1## $Id: browser.py 10667 2013-10-21 09:22:13Z 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
[9616]114        self.pay_item_id = '000'
[9737]115        if self.context.p_category == 'schoolfee':
[9789]116            provider_amt = 1200.0
117            xmldict['provider_amt'] = 100 * provider_amt
[9616]118            self.pay_item_id = '101'
[9789]119            dalash_amt = 1800.0
120            xmldict['dalash_amt'] = 100 * dalash_amt
121            xmldict['institution_amt'] = 100 * (
122                self.context.amount_auth - provider_amt -
123                GATEWAY_AMT - dalash_amt)
[9616]124            if xmldict['faculty'] in ('CPGS',):
125                xmldict['institution_acct'] = "1771180233"
126                xmldict['institution_bank_id'] = '120'
127            elif xmldict['faculty'] in ('IBAS',):
[10667]128                xmldict['institution_acct'] = "2013910271"
129                xmldict['institution_bank_id'] = '8'
130                self.pay_item_id = '107'
[9616]131            elif xmldict['faculty'] in ('IETS',):
[10667]132                xmldict['institution_acct'] = "2013910271"
133                xmldict['institution_bank_id'] = '8'
134                self.pay_item_id = '109'
135            elif xmldict['faculty'] in ('IFMS',):
[9616]136                xmldict['institution_acct'] = "0106259811"
137                xmldict['institution_bank_id'] = '10'
[10667]138                self.pay_item_id = '106'
[9616]139            elif xmldict['faculty'] in ('ITCH',):
[10667]140                xmldict['institution_acct'] = "1771180233"
141                xmldict['institution_bank_id'] = '120'
142                self.pay_item_id = '110'
143            elif xmldict['faculty'] in ('IICT',):
[9616]144                xmldict['institution_acct'] = "2013910271"
145                xmldict['institution_bank_id'] = '8'
[10667]146                self.pay_item_id = '108'
[9392]147
[9617]148        elif 'maintenance' in self.context.p_category:
[9626]149            self.pay_item_id = '102'
[9617]150            xmldict['institution_acct'] = "0039050937"
151            xmldict['institution_bank_id'] = '31'
[9789]152            xmldict['institution_amt'] = 100 * (
153                self.context.amount_auth - GATEWAY_AMT)
154            dalash_amt = 0.0
155            provider_amt = 0.0
[9617]156
[9392]157        hashargs = (
158            self.context.p_id +
159            PRODUCT_ID +
160            self.pay_item_id +
161            str(int(self.amount_auth)) +
162            self.site_redirect_url +
163            self.mac)
164        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
165
[8263]166        # Interswitch amount is not part of the xml data
[9617]167
[9789]168        if self.context.p_category == 'schoolfee':
[9617]169            xmltext = """<payment_item_detail>
[8263]170<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
171<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" />
[9789]172<item_detail item_id="2" item_name="Dalash" item_amt="%(dalash_amt)d" bank_id="117" acct_num="1013196791" />
[9129]173<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" />
[8263]174</item_details>
175</payment_item_detail>""" % xmldict
[9617]176
177        elif 'maintenance' in self.context.p_category:
178            xmltext = """<payment_item_detail>
179<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
180<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" />
181</item_details>
182</payment_item_detail>""" % xmldict
183
[8263]184        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
[9789]185        self.context.provider_amt = provider_amt
186        self.context.gateway_amt = GATEWAY_AMT
187        self.context.thirdparty_amt = dalash_amt
[7894]188        return
189
[10131]190class InterswitchPageApplicant(KofaPage):
191    """ View which sends a POST request to the Interswitch
192    CollegePAY payment gateway.
193    """
194    grok.context(ICustomApplicantOnlinePayment)
195    grok.require('waeup.payApplicant')
196    grok.template('applicant_goto_interswitch')
197    grok.name('goto_interswitch')
198    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
199    submit_button = _('Submit')
200    action = POST_ACTION
201    site_name = SITE_NAME
202    currency = CURRENCY
203    pay_item_id = '103'
204    product_id = PRODUCT_ID
205    mac = 'E6BA6CBBA9AF2871EE25C32C8D57C98895B9B001DC5B9CB2C463E2A9BDA44A3F1260C8A364F33789CDF74CB3EE7E6EF5D94F48D3AF7B727E75D97F07618DFA6D'
206
207    def interswitch_img_url(self):
208        return interswitch_img_url(self)
209
210    def update(self):
211        if self.context.p_state != 'unpaid':
212            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
213            self.redirect(self.url(self.context, '@@index'))
214            return
215        if self.context.__parent__.__parent__.expired \
216            and self.context.__parent__.__parent__.strict_deadline:
217            self.flash(_("Payment ticket can't be send to CollegePAY. "
218                         "Application period has expired."))
219            self.redirect(self.url(self.context, '@@index'))
220            return
221        self.applicant = self.context.__parent__
222        self.amount_auth = 100 * self.context.amount_auth
223        xmldict = {}
224        self.category = getUtility(
225            IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
226        tz = getUtility(IKofaUtils).tzinfo
227        self.local_date_time = to_timezone(
228            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
229        self.site_redirect_url = self.url(self.context, 'request_webservice')
[10176]230        provider_amt = 300.0
231        dalash_amt = 200.0
[10131]232        if self.applicant.applicant_id.startswith('hnd'):
[10254]233            xmldict['institution_acct'] = '7000016724'
[10131]234            xmldict['institution_bank_id'] = '9'
[10597]235        elif self.applicant.applicant_id.startswith('nd'):
[10131]236            xmldict['institution_acct'] = '2014191363'
237            xmldict['institution_bank_id'] = '8'
[10651]238        elif self.applicant.applicant_id.startswith('pgft'):
239            xmldict['institution_acct'] = '7000016724'
240            xmldict['institution_bank_id'] = '9'
[10597]241        elif self.applicant.applicant_id.startswith('prejambites'):
242            xmldict['institution_acct'] = '0106259811'
243            xmldict['institution_bank_id'] = '10'
244            self.pay_item_id = '104'
245            provider_amt = 0.0
246            dalash_amt = 0.0
247        elif self.applicant.applicant_id.startswith('pre'):
248            xmldict['institution_acct'] = '2013910271'
249            xmldict['institution_bank_id'] = '8'
250            provider_amt = 0.0
251            dalash_amt = 0.0
[10131]252        else:
253            xmldict['institution_acct'] = '00000000000'
254            xmldict['institution_bank_id'] = '00'
[10176]255        xmldict['dalash_amt'] = 100 * dalash_amt
[10131]256        xmldict['detail_ref'] = self.context.p_id
257        xmldict['provider_amt'] = 100 * provider_amt
258        xmldict['provider_acct'] = PROVIDER_ACCT
259        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
260        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
261        xmldict['institution_amt'] = 100 * (
[10176]262            self.context.amount_auth - provider_amt - GATEWAY_AMT - dalash_amt)
[10131]263        xmldict['institution_item_name'] = self.context.p_category
264        xmldict['institution_name'] = INSTITUTION_NAME
265
266        hashargs = (
267            self.context.p_id +
268            PRODUCT_ID +
269            self.pay_item_id +
270            str(int(self.amount_auth)) +
271            self.site_redirect_url +
272            self.mac)
273        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
274
275        # Interswitch amount is not part of the xml data
[10597]276
277        if not self.applicant.applicant_id.startswith('pre'):
278            xmltext = """<payment_item_detail>
[10131]279<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
280<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]281<item_detail item_id="2" item_name="Dalash" item_amt="%(dalash_amt)d" bank_id="117" acct_num="1013196791" />
282<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]283</item_details>
284</payment_item_detail>""" % xmldict
[10597]285
286        else:
287            xmltext = """<payment_item_detail>
288<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
289<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" />
290</item_details>
291</payment_item_detail>""" % xmldict
292
[10131]293        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
294        self.context.provider_amt = provider_amt
295        self.context.gateway_amt = GATEWAY_AMT
[10176]296        self.context.thirdparty_amt = dalash_amt
[10131]297        return
298
[9789]299class InterswitchPaymentRequestWebservicePageStudent(
300    InterswitchPaymentRequestWebservicePageStudent):
[7919]301    """ Request webservice view for the CollegePAY gateway
302    """
[8255]303    grok.context(ICustomStudentOnlinePayment)
[9789]304    product_id = PRODUCT_ID
305    gateway_host = HOST
[10131]306    gateway_url = URL
307
308class InterswitchPaymentRequestWebservicePageApplicant(
309    InterswitchPaymentRequestWebservicePageApplicant):
310    """ Request webservice view for the CollegePAY gateway
311    """
312    grok.context(ICustomApplicantOnlinePayment)
313    product_id = PRODUCT_ID
314    gateway_host = HOST
[9789]315    gateway_url = URL
Note: See TracBrowser for help on using the repository browser.