source: main/waeup.fceokene/trunk/src/waeup/fceokene/interswitch/browser.py @ 9783

Last change on this file since 9783 was 9780, checked in by Henrik Bettermann, 13 years ago

Changes according to r9775.

  • Property svn:keywords set to Id
File size: 13.4 KB
RevLine 
[7894]1## $Id: browser.py 9780 2012-12-06 17:52:21Z 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
[7894]19import grok
[8281]20from zope.component import getUtility
[9780]21from kofacustom.nigeria.interswitch.helpers import (
22    query_interswitch, write_payments_log)
[7894]23from waeup.kofa.browser.layout import KofaPage, UtilityView
[9752]24from waeup.kofa.interfaces import IKofaUtils
[8281]25from waeup.kofa.utils.helpers import to_timezone
[8421]26from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
27from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
[8460]28from waeup.fceokene.students.interfaces import ICustomStudentOnlinePayment
29from waeup.fceokene.applicants.interfaces import ICustomApplicantOnlinePayment
30from waeup.fceokene.interfaces import MessageFactory as _
[7894]31
[8644]32PRODUCT_ID = '83'
[8460]33SITE_NAME = 'fceokene-kofa.waeup.org'
[8641]34PROVIDER_ACCT = '0026781725'
35PROVIDER_BANK_ID = '31'
[8263]36PROVIDER_ITEM_NAME = 'BT Education'
[8460]37INSTITUTION_NAME = 'FCEOkene'
[7894]38CURRENCY = '566'
[9780]39GATEWAY_AMT = 150.0
[8401]40#QUERY_URL = 'https://webpay.interswitchng.com/paydirect/services/TransactionQueryURL.aspx'
[8293]41#QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx'
[8385]42POST_ACTION = 'https://webpay.interswitchng.com/paydirect/webpay/pay.aspx'
[8293]43#POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx'
[7894]44
[8293]45HOST = 'webpay.interswitchng.com'
46#HOST = 'testwebpay.interswitchng.com'
47URL = '/paydirect/services/TransactionQueryWs.asmx'
48#URL = '/test_paydirect/services/TransactionQueryWs.asmx'
[7898]49httplib.HTTPConnection.debuglevel = 0
50
[8421]51class InterswitchActionButtonStudent(APABStudent):
[8259]52    grok.order(1)
[8255]53    grok.context(ICustomStudentOnlinePayment)
[8430]54    grok.require('waeup.payStudent')
[7894]55    icon = 'actionicon_pay.png'
56    text = _('CollegePAY')
57    target = 'goto_interswitch'
58
59    @property
60    def target_url(self):
61        if self.context.p_state != 'unpaid':
62            return ''
63        return self.view.url(self.view.context, self.target)
64
[8421]65class InterswitchActionButtonApplicant(APABApplicant):
[8259]66    grok.order(1)
[8256]67    grok.context(ICustomApplicantOnlinePayment)
[8430]68    grok.require('waeup.payApplicant')
[8256]69    icon = 'actionicon_pay.png'
70    text = _('CollegePAY')
71    target = 'goto_interswitch'
72
73    @property
74    def target_url(self):
75        if self.context.p_state != 'unpaid':
76            return ''
77        return self.view.url(self.view.context, self.target)
78
[8421]79class InterswitchRequestWebserviceActionButtonStudent(APABStudent):
[8259]80    grok.order(2)
[8255]81    grok.context(ICustomStudentOnlinePayment)
[8430]82    grok.require('waeup.payStudent')
[7919]83    icon = 'actionicon_call.png'
[8421]84    text = _('Requery CollegePAY')
[7919]85    target = 'request_webservice'
86
[8421]87class InterswitchRequestWebserviceActionButtonApplicant(APABApplicant):
[8259]88    grok.order(2)
[8256]89    grok.context(ICustomApplicantOnlinePayment)
[8430]90    grok.require('waeup.payApplicant')
[8256]91    icon = 'actionicon_call.png'
[8421]92    text = _('Requery CollegePAY')
[8256]93    target = 'request_webservice'
[7919]94
[8256]95class InterswitchPageStudent(KofaPage):
[7894]96    """ View which sends a POST request to the Interswitch
97    CollegePAY payment gateway.
98    """
[8255]99    grok.context(ICustomStudentOnlinePayment)
[7894]100    grok.name('goto_interswitch')
[8256]101    grok.template('student_goto_interswitch')
[7894]102    grok.require('waeup.payStudent')
103    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
104    submit_button = _('Submit')
105    action = POST_ACTION
106    site_name = SITE_NAME
107    currency = CURRENCY
[9613]108    pay_item_id = ''
[7894]109    product_id = PRODUCT_ID
110
111    def update(self):
[8256]112        if self.context.p_state == 'paid':
[7894]113            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
114            self.redirect(self.url(self.context, '@@index'))
115            return
[8256]116
[8745]117        student = self.student = self.context.student
118        certificate = getattr(student['studycourse'],'certificate',None)
[8276]119        self.amount_auth = 100 * self.context.amount_auth
[7894]120        xmldict = {}
121        if certificate is not None:
122            xmldict['department'] = certificate.__parent__.__parent__.code
123            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
124        else:
125            xmldict['department'] = None
126            xmldict['faculty'] = None
[9406]127        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
[8281]128        tz = getUtility(IKofaUtils).tzinfo
129        self.local_date_time = to_timezone(
130            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
[8256]131        self.site_redirect_url = self.url(self.context, 'request_webservice')
[8263]132        # Provider data
[9780]133        provider_amt = 1600.0
[8263]134        xmldict['detail_ref'] = self.context.p_id
135        xmldict['provider_acct'] = PROVIDER_ACCT
136        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
137        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
[9780]138        xmldict['provider_amt'] = 100 * provider_amt
[8263]139        # Institution data
[9780]140        fceokene_split_amt = 1400.0
[9613]141        xmldict['fceokene_acct'] = "0000000000000"
142        xmldict['institution_bank_id'] = '0'
143        xmldict['institution_item_name'] = self.category
144        xmldict['institution_name'] = INSTITUTION_NAME
145        if self.context.p_category == 'schoolfee':
146            self.pay_item_id = '8302'
147            if student.current_mode in ('ug_sw','prence',):
148                xmldict['institution_acct'] = "6216801025"
149                xmldict['institution_bank_id'] = '117'
150            elif student.current_mode in ('ug_ft',) and \
151                student['studycourse'].current_verdict == 'O':
152                xmldict['institution_acct'] = "6216801025"
153                xmldict['institution_bank_id'] = '117'
154            elif student.current_mode in ('ug_ft',):
155                xmldict['institution_acct'] = "6216801033"
156                xmldict['institution_bank_id'] = '117'
157            elif student.current_mode in ('pd_ft',):
158                xmldict['institution_acct'] = "6216801025"
159                xmldict['institution_bank_id'] = '117'
[9780]160            xmldict['fceokene_split'] = 100 * fceokene_split_amt
[9613]161            xmldict['institution_amt'] = 100 * (
[9780]162                self.context.amount_auth - provider_amt -
163                GATEWAY_AMT - fceokene_split_amt)
[9613]164        elif 'maintenance' in self.context.p_category:
[9780]165            fceokene_split_amt = 0.0
166            provider_amt = 0.0
[9613]167            self.pay_item_id = '8300'
168            xmldict['institution_amt'] = 100 * (
[9780]169                self.context.amount_auth - GATEWAY_AMT)
[9613]170            xmldict['institution_acct'] = "1012044132"
[9129]171            xmldict['institution_bank_id'] = '117'
[8263]172        # Interswitch amount is not part of the xml data
[9613]173
174        if self.context.p_category == 'schoolfee':
175            xmltext = """<payment_item_detail>
[8263]176<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
177<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" />
[9780]178<item_detail item_id="2" item_name="FCEOkene Split" item_amt="%(fceokene_split)d" bank_id="117" acct_num="6216801058" />
[9129]179<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]180</item_details>
181</payment_item_detail>""" % xmldict
[9613]182
183        elif 'maintenance' in self.context.p_category:
184            xmltext = """<payment_item_detail>
185<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
186<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" />
187</item_details>
188</payment_item_detail>""" % xmldict
189
[8263]190        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
[9780]191        self.context.provider_amt = provider_amt
192        self.context.gateway_amt = GATEWAY_AMT
193        self.context.thirdparty_amt = fceokene_split_amt
[7894]194        return
195
[8263]196class InterswitchPageApplicant(KofaPage):
[8256]197    """ View which sends a POST request to the Interswitch
198    CollegePAY payment gateway.
199    """
200    grok.context(ICustomApplicantOnlinePayment)
201    grok.require('waeup.payApplicant')
202    grok.template('applicant_goto_interswitch')
[8263]203    grok.name('goto_interswitch')
204    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
205    submit_button = _('Submit')
206    action = POST_ACTION
207    site_name = SITE_NAME
208    currency = CURRENCY
[8644]209    pay_item_id = '8303'
[8263]210    product_id = PRODUCT_ID
[8256]211
212    def update(self):
[8263]213        if self.context.p_state != 'unpaid':
214            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
215            self.redirect(self.url(self.context, '@@index'))
216            return
[8831]217        if self.context.__parent__.__parent__.expired \
218            and self.context.__parent__.__parent__.strict_deadline:
[8706]219            self.flash(_("Payment ticket can't be send to CollegePAY. "
220                         "Application period has expired."))
221            self.redirect(self.url(self.context, '@@index'))
222            return
[8256]223        self.applicant = self.context.__parent__
[8276]224        self.amount_auth = 100 * self.context.amount_auth
[8256]225        xmldict = {}
[9406]226        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
[8281]227        tz = getUtility(IKofaUtils).tzinfo
228        self.local_date_time = to_timezone(
229            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
[8256]230        self.site_redirect_url = self.url(self.context, 'request_webservice')
[8641]231        xmldict['detail_ref'] = self.context.p_id
[8263]232        # Provider data
[9780]233        provider_amt = 500.0
234        xmldict['provider_amt'] = 100 * provider_amt
[8263]235        xmldict['provider_acct'] = PROVIDER_ACCT
236        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
237        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
238        # Institution data
[9780]239        xmldict['institution_amt'] = 100 * (self.context.amount_auth -
240            provider_amt - GATEWAY_AMT)
[9108]241        xmldict['institution_acct'] = '1012445289'
[8641]242        xmldict['institution_bank_id'] = '117'
[8263]243        xmldict['institution_item_name'] = self.context.p_category
244        xmldict['institution_name'] = INSTITUTION_NAME
245        # Interswitch amount is not part of the xml data
246        xmltext = """<payment_item_detail>
247<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
248<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" />
249<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" />
250</item_details>
251</payment_item_detail>""" % xmldict
252        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
[9780]253        self.context.provider_amt = provider_amt
254        self.context.gateway_amt = GATEWAY_AMT
[8256]255        return
256
[7894]257
[8256]258class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View):
[7919]259    """ Request webservice view for the CollegePAY gateway
260    """
[8255]261    grok.context(ICustomStudentOnlinePayment)
[7919]262    grok.name('request_webservice')
263    grok.require('waeup.payStudent')
264
265    def update(self):
266        if self.context.p_state == 'paid':
267            self.flash(_('This ticket has already been paid.'))
268            return
[8745]269        student = self.context.student
[9752]270        success, msg, log = query_interswitch(
271            self.context, PRODUCT_ID, HOST, URL)
[8745]272        student.writeLogMessage(self, log)
[8430]273        if not success:
[9780]274            self.flash(msg)
[8430]275            return
[9780]276        write_payments_log(student.student_id, self.context)
[8430]277        success, msg, log = self.context.doAfterStudentPayment()
278        if log is not None:
[8745]279            student.writeLogMessage(self, log)
[8430]280        self.flash(msg)
[8256]281        return
[7919]282
[8256]283    def render(self):
284        self.redirect(self.url(self.context, '@@index'))
285        return
[7926]286
[8256]287class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View):
288    """ Request webservice view for the CollegePAY gateway
289    """
290    grok.context(ICustomApplicantOnlinePayment)
291    grok.name('request_webservice')
292    grok.require('waeup.payApplicant')
[7919]293
[8256]294    def update(self):
295        if self.context.p_state == 'paid':
296            self.flash(_('This ticket has already been paid.'))
[7919]297            return
[8256]298        applicant = self.context.__parent__
[9752]299        success, msg, log = query_interswitch(
300            self.context, PRODUCT_ID, HOST, URL)
[8745]301        applicant.writeLogMessage(self, log)
[8430]302        if not success:
303            self.flash(msg)
304            return
[9780]305        write_payments_log(applicant.applicant_id, self.context)
[8430]306        success, msg, log = self.context.doAfterApplicantPayment()
307        if log is not None:
[8745]308            applicant.writeLogMessage(self, log)
[8430]309        self.flash(msg)
[7919]310        return
311
312    def render(self):
313        self.redirect(self.url(self.context, '@@index'))
[9108]314        return
Note: See TracBrowser for help on using the repository browser.