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

Last change on this file since 9752 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
Line 
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##
18import httplib
19import grok
20from zope.component import getUtility
21from kofacustom.nigeria.interswitch.helpers import query_interswitch
22from waeup.kofa.browser.layout import KofaPage, UtilityView
23from waeup.kofa.interfaces import IKofaUtils
24from waeup.kofa.utils.helpers import to_timezone
25from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
26from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
27from waeup.fceokene.students.interfaces import ICustomStudentOnlinePayment
28from waeup.fceokene.applicants.interfaces import ICustomApplicantOnlinePayment
29from waeup.fceokene.interfaces import MessageFactory as _
30
31PRODUCT_ID = '83'
32SITE_NAME = 'fceokene-kofa.waeup.org'
33PROVIDER_ACCT = '0026781725'
34PROVIDER_BANK_ID = '31'
35PROVIDER_ITEM_NAME = 'BT Education'
36INSTITUTION_NAME = 'FCEOkene'
37CURRENCY = '566'
38#QUERY_URL = 'https://webpay.interswitchng.com/paydirect/services/TransactionQueryURL.aspx'
39#QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx'
40POST_ACTION = 'https://webpay.interswitchng.com/paydirect/webpay/pay.aspx'
41#POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx'
42
43HOST = 'webpay.interswitchng.com'
44#HOST = 'testwebpay.interswitchng.com'
45URL = '/paydirect/services/TransactionQueryWs.asmx'
46#URL = '/test_paydirect/services/TransactionQueryWs.asmx'
47httplib.HTTPConnection.debuglevel = 0
48
49class InterswitchActionButtonStudent(APABStudent):
50    grok.order(1)
51    grok.context(ICustomStudentOnlinePayment)
52    grok.require('waeup.payStudent')
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
63class InterswitchActionButtonApplicant(APABApplicant):
64    grok.order(1)
65    grok.context(ICustomApplicantOnlinePayment)
66    grok.require('waeup.payApplicant')
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
77class InterswitchRequestWebserviceActionButtonStudent(APABStudent):
78    grok.order(2)
79    grok.context(ICustomStudentOnlinePayment)
80    grok.require('waeup.payStudent')
81    icon = 'actionicon_call.png'
82    text = _('Requery CollegePAY')
83    target = 'request_webservice'
84
85class InterswitchRequestWebserviceActionButtonApplicant(APABApplicant):
86    grok.order(2)
87    grok.context(ICustomApplicantOnlinePayment)
88    grok.require('waeup.payApplicant')
89    icon = 'actionicon_call.png'
90    text = _('Requery CollegePAY')
91    target = 'request_webservice'
92
93class InterswitchPageStudent(KofaPage):
94    """ View which sends a POST request to the Interswitch
95    CollegePAY payment gateway.
96    """
97    grok.context(ICustomStudentOnlinePayment)
98    grok.name('goto_interswitch')
99    grok.template('student_goto_interswitch')
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
106    pay_item_id = ''
107    product_id = PRODUCT_ID
108
109    def update(self):
110        if self.context.p_state == 'paid':
111            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
112            self.redirect(self.url(self.context, '@@index'))
113            return
114
115        student = self.student = self.context.student
116        certificate = getattr(student['studycourse'],'certificate',None)
117        self.amount_auth = 100 * self.context.amount_auth
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
125        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
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")
129        self.site_redirect_url = self.url(self.context, 'request_webservice')
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
135        xmldict['provider_amt'] = 100 * 1600
136        # Institution data
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 * (
158                self.context.amount_auth - 1600 - 150 - 1400)
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"
164            xmldict['institution_bank_id'] = '117'
165        # Interswitch amount is not part of the xml data
166
167        if self.context.p_category == 'schoolfee':
168            xmltext = """<payment_item_detail>
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" />
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" />
173</item_details>
174</payment_item_detail>""" % xmldict
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
183        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
184        return
185
186class InterswitchPageApplicant(KofaPage):
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')
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
199    pay_item_id = '8303'
200    product_id = PRODUCT_ID
201
202    def update(self):
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
207        if self.context.__parent__.__parent__.expired \
208            and self.context.__parent__.__parent__.strict_deadline:
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
213        self.applicant = self.context.__parent__
214        self.amount_auth = 100 * self.context.amount_auth
215        xmldict = {}
216        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
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")
220        self.site_redirect_url = self.url(self.context, 'request_webservice')
221        xmldict['detail_ref'] = self.context.p_id
222        # Provider data
223        xmldict['provider_amt'] = 100 * 500
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
228        xmldict['institution_amt'] = 100 * (self.context.amount_auth - 500 - 150)
229        xmldict['institution_acct'] = '1012445289'
230        xmldict['institution_bank_id'] = '117'
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
241        return
242
243
244class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View):
245    """ Request webservice view for the CollegePAY gateway
246    """
247    grok.context(ICustomStudentOnlinePayment)
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
255        student = self.context.student
256        success, msg, log = query_interswitch(
257            self.context, PRODUCT_ID, HOST, URL)
258        student.writeLogMessage(self, log)
259        if not success:
260            msg = self.request['QUERY_STRING'].replace('%20',' ')
261            self.flash(_('Response from Interswitch: ') + msg)
262            return
263        success, msg, log = self.context.doAfterStudentPayment()
264        if log is not None:
265            student.writeLogMessage(self, log)
266        self.flash(msg)
267        return
268
269    def render(self):
270        self.redirect(self.url(self.context, '@@index'))
271        return
272
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')
279
280    def update(self):
281        if self.context.p_state == 'paid':
282            self.flash(_('This ticket has already been paid.'))
283            return
284        applicant = self.context.__parent__
285        success, msg, log = query_interswitch(
286            self.context, PRODUCT_ID, HOST, URL)
287        applicant.writeLogMessage(self, log)
288        if not success:
289            self.flash(msg)
290            return
291        success, msg, log = self.context.doAfterApplicantPayment()
292        if log is not None:
293            applicant.writeLogMessage(self, log)
294        self.flash(msg)
295        return
296
297    def render(self):
298        self.redirect(self.url(self.context, '@@index'))
299        return
Note: See TracBrowser for help on using the repository browser.