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

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

Import helper functions from kofacustom.nigeria.

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