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

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

Changes according to r9775.

  • Property svn:keywords set to Id
File size: 13.4 KB
Line 
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##
18import httplib
19import grok
20from zope.component import getUtility
21from kofacustom.nigeria.interswitch.helpers import (
22    query_interswitch, write_payments_log)
23from waeup.kofa.browser.layout import KofaPage, UtilityView
24from waeup.kofa.interfaces import IKofaUtils
25from waeup.kofa.utils.helpers import to_timezone
26from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
27from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
28from waeup.fceokene.students.interfaces import ICustomStudentOnlinePayment
29from waeup.fceokene.applicants.interfaces import ICustomApplicantOnlinePayment
30from waeup.fceokene.interfaces import MessageFactory as _
31
32PRODUCT_ID = '83'
33SITE_NAME = 'fceokene-kofa.waeup.org'
34PROVIDER_ACCT = '0026781725'
35PROVIDER_BANK_ID = '31'
36PROVIDER_ITEM_NAME = 'BT Education'
37INSTITUTION_NAME = 'FCEOkene'
38CURRENCY = '566'
39GATEWAY_AMT = 150.0
40#QUERY_URL = 'https://webpay.interswitchng.com/paydirect/services/TransactionQueryURL.aspx'
41#QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx'
42POST_ACTION = 'https://webpay.interswitchng.com/paydirect/webpay/pay.aspx'
43#POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx'
44
45HOST = 'webpay.interswitchng.com'
46#HOST = 'testwebpay.interswitchng.com'
47URL = '/paydirect/services/TransactionQueryWs.asmx'
48#URL = '/test_paydirect/services/TransactionQueryWs.asmx'
49httplib.HTTPConnection.debuglevel = 0
50
51class InterswitchActionButtonStudent(APABStudent):
52    grok.order(1)
53    grok.context(ICustomStudentOnlinePayment)
54    grok.require('waeup.payStudent')
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
65class InterswitchActionButtonApplicant(APABApplicant):
66    grok.order(1)
67    grok.context(ICustomApplicantOnlinePayment)
68    grok.require('waeup.payApplicant')
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
79class InterswitchRequestWebserviceActionButtonStudent(APABStudent):
80    grok.order(2)
81    grok.context(ICustomStudentOnlinePayment)
82    grok.require('waeup.payStudent')
83    icon = 'actionicon_call.png'
84    text = _('Requery CollegePAY')
85    target = 'request_webservice'
86
87class InterswitchRequestWebserviceActionButtonApplicant(APABApplicant):
88    grok.order(2)
89    grok.context(ICustomApplicantOnlinePayment)
90    grok.require('waeup.payApplicant')
91    icon = 'actionicon_call.png'
92    text = _('Requery CollegePAY')
93    target = 'request_webservice'
94
95class InterswitchPageStudent(KofaPage):
96    """ View which sends a POST request to the Interswitch
97    CollegePAY payment gateway.
98    """
99    grok.context(ICustomStudentOnlinePayment)
100    grok.name('goto_interswitch')
101    grok.template('student_goto_interswitch')
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
108    pay_item_id = ''
109    product_id = PRODUCT_ID
110
111    def update(self):
112        if self.context.p_state == 'paid':
113            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
114            self.redirect(self.url(self.context, '@@index'))
115            return
116
117        student = self.student = self.context.student
118        certificate = getattr(student['studycourse'],'certificate',None)
119        self.amount_auth = 100 * self.context.amount_auth
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
127        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
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")
131        self.site_redirect_url = self.url(self.context, 'request_webservice')
132        # Provider data
133        provider_amt = 1600.0
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
138        xmldict['provider_amt'] = 100 * provider_amt
139        # Institution data
140        fceokene_split_amt = 1400.0
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'
160            xmldict['fceokene_split'] = 100 * fceokene_split_amt
161            xmldict['institution_amt'] = 100 * (
162                self.context.amount_auth - provider_amt -
163                GATEWAY_AMT - fceokene_split_amt)
164        elif 'maintenance' in self.context.p_category:
165            fceokene_split_amt = 0.0
166            provider_amt = 0.0
167            self.pay_item_id = '8300'
168            xmldict['institution_amt'] = 100 * (
169                self.context.amount_auth - GATEWAY_AMT)
170            xmldict['institution_acct'] = "1012044132"
171            xmldict['institution_bank_id'] = '117'
172        # Interswitch amount is not part of the xml data
173
174        if self.context.p_category == 'schoolfee':
175            xmltext = """<payment_item_detail>
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" />
178<item_detail item_id="2" item_name="FCEOkene Split" item_amt="%(fceokene_split)d" bank_id="117" acct_num="6216801058" />
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" />
180</item_details>
181</payment_item_detail>""" % xmldict
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
190        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
191        self.context.provider_amt = provider_amt
192        self.context.gateway_amt = GATEWAY_AMT
193        self.context.thirdparty_amt = fceokene_split_amt
194        return
195
196class InterswitchPageApplicant(KofaPage):
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')
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
209    pay_item_id = '8303'
210    product_id = PRODUCT_ID
211
212    def update(self):
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
217        if self.context.__parent__.__parent__.expired \
218            and self.context.__parent__.__parent__.strict_deadline:
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
223        self.applicant = self.context.__parent__
224        self.amount_auth = 100 * self.context.amount_auth
225        xmldict = {}
226        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
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")
230        self.site_redirect_url = self.url(self.context, 'request_webservice')
231        xmldict['detail_ref'] = self.context.p_id
232        # Provider data
233        provider_amt = 500.0
234        xmldict['provider_amt'] = 100 * provider_amt
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
239        xmldict['institution_amt'] = 100 * (self.context.amount_auth -
240            provider_amt - GATEWAY_AMT)
241        xmldict['institution_acct'] = '1012445289'
242        xmldict['institution_bank_id'] = '117'
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
253        self.context.provider_amt = provider_amt
254        self.context.gateway_amt = GATEWAY_AMT
255        return
256
257
258class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View):
259    """ Request webservice view for the CollegePAY gateway
260    """
261    grok.context(ICustomStudentOnlinePayment)
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
269        student = self.context.student
270        success, msg, log = query_interswitch(
271            self.context, PRODUCT_ID, HOST, URL)
272        student.writeLogMessage(self, log)
273        if not success:
274            self.flash(msg)
275            return
276        write_payments_log(student.student_id, self.context)
277        success, msg, log = self.context.doAfterStudentPayment()
278        if log is not None:
279            student.writeLogMessage(self, log)
280        self.flash(msg)
281        return
282
283    def render(self):
284        self.redirect(self.url(self.context, '@@index'))
285        return
286
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')
293
294    def update(self):
295        if self.context.p_state == 'paid':
296            self.flash(_('This ticket has already been paid.'))
297            return
298        applicant = self.context.__parent__
299        success, msg, log = query_interswitch(
300            self.context, PRODUCT_ID, HOST, URL)
301        applicant.writeLogMessage(self, log)
302        if not success:
303            self.flash(msg)
304            return
305        write_payments_log(applicant.applicant_id, self.context)
306        success, msg, log = self.context.doAfterApplicantPayment()
307        if log is not None:
308            applicant.writeLogMessage(self, log)
309        self.flash(msg)
310        return
311
312    def render(self):
313        self.redirect(self.url(self.context, '@@index'))
314        return
Note: See TracBrowser for help on using the repository browser.