source: main/waeup.uniben/trunk/src/waeup/uniben/interswitch/browser.py @ 9749

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

pyflakes

  • Property svn:keywords set to Id
File size: 17.7 KB
Line 
1## $Id: browser.py 9749 2012-12-01 06:41:35Z 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.uniben.students.interfaces import ICustomStudentOnlinePayment
28from waeup.uniben.applicants.interfaces import ICustomApplicantOnlinePayment
29from waeup.uniben.interfaces import MessageFactory as _
30
31PRODUCT_ID = '57'
32SITE_NAME = 'uniben-kofa.waeup.org'
33PROVIDER_ACCT = '1010764827'
34PROVIDER_BANK_ID = '117'
35PROVIDER_ITEM_NAME = 'BT Education'
36INSTITUTION_NAME = 'Uniben'
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
77# Deprecated
78#class InterswitchRequestCallbackActionButtonStudent(RCABStudent):
79#    grok.order(3)
80#    grok.context(ICustomStudentOnlinePayment)
81#    icon = 'actionicon_call.png'
82#    text = _('Request CollegePAY callback')
83
84#    def target_url(self):
85#        if self.context.p_state == 'paid':
86#            return ''
87#        site_redirect_url = self.view.url(self.view.context, 'isw_callback')
88#        args = {
89#            'transRef':self.context.p_id,
90#            'prodID':PRODUCT_ID,
91#            'redirectURL':site_redirect_url}
92#        return QUERY_URL + '?%s' % urllib.urlencode(args)
93
94# Alternative preferred solution
95class InterswitchRequestWebserviceActionButtonStudent(APABStudent):
96    grok.order(2)
97    grok.context(ICustomStudentOnlinePayment)
98    grok.require('waeup.payStudent')
99    icon = 'actionicon_call.png'
100    text = _('Requery CollegePAY')
101    target = 'request_webservice'
102
103class InterswitchRequestWebserviceActionButtonApplicant(APABApplicant):
104    grok.order(2)
105    grok.context(ICustomApplicantOnlinePayment)
106    grok.require('waeup.payApplicant')
107    icon = 'actionicon_call.png'
108    text = _('Requery CollegePAY')
109    target = 'request_webservice'
110
111class InterswitchPageStudent(KofaPage):
112    """ View which sends a POST request to the Interswitch
113    CollegePAY payment gateway.
114    """
115    grok.context(ICustomStudentOnlinePayment)
116    grok.name('goto_interswitch')
117    grok.template('student_goto_interswitch')
118    grok.require('waeup.payStudent')
119    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
120    submit_button = _('Submit')
121    action = POST_ACTION
122    site_name = SITE_NAME
123    currency = CURRENCY
124    product_id = PRODUCT_ID
125
126    def update(self):
127        #if self.context.p_state != 'unpaid':
128        if self.context.p_state == 'paid':
129            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
130            self.redirect(self.url(self.context, '@@index'))
131            return
132
133        student = self.student = self.context.student
134        certificate = getattr(student['studycourse'],'certificate',None)
135        self.amount_auth = 100 * self.context.amount_auth
136        xmldict = {}
137        if certificate is not None:
138            xmldict['department'] = certificate.__parent__.__parent__.code
139            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
140        else:
141            xmldict['department'] = None
142            xmldict['faculty'] = None
143        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
144        tz = getUtility(IKofaUtils).tzinfo
145        self.local_date_time = to_timezone(
146            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
147        self.site_redirect_url = self.url(self.context, 'request_webservice')
148        # Provider data
149        xmldict['detail_ref'] = self.context.p_id
150        xmldict['provider_acct'] = PROVIDER_ACCT
151        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
152        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
153        # Institution data
154        xmldict['institution_acct'] = '000000000000'
155        xmldict['institution_bank_id'] = '00'
156        xmldict['institution_amt'] = '0.0'
157        if self.context.p_category == 'schoolfee':
158            provider_amt = 1500
159            if student.current_mode.endswith('_ft'):
160                self.pay_item_id = '5700'
161                if student.current_mode in ('ug_ft','de_ft','ct_ft','ume_ft'):
162                    xmldict['institution_acct'] = '2017506430'
163                    xmldict['institution_bank_id'] = '8'
164                elif student.current_mode in ('dp_ft'):
165                    xmldict['institution_acct'] = '9201805071'
166                    xmldict['institution_bank_id'] = '17'
167                elif student.current_mode in ('pg_ft'):
168                    xmldict['institution_acct'] = '5330832799'
169                    xmldict['institution_bank_id'] = '51'
170            elif student.current_mode.endswith('_pt'):
171                self.pay_item_id = '5701'
172                if student.current_mode in ('ug_pt','de_pt','ct_pt'):
173                    xmldict['institution_acct'] = '0122009929'
174                    xmldict['institution_bank_id'] = '16'
175                elif student.current_mode in ('dp_pt'):
176                    xmldict['institution_acct'] = '9201805071'
177                    xmldict['institution_bank_id'] = '17'
178                elif student.current_mode in ('pg_pt'):
179                    xmldict['institution_acct'] = '0031716047'
180                    xmldict['institution_bank_id'] = '10'
181        elif self.context.p_category == 'clearance':
182            self.pay_item_id = '5702'
183            provider_amt = 1500
184            if student.current_mode == 'pg_ft':
185                xmldict['institution_acct'] = '5330832799'
186                xmldict['institution_bank_id'] = '51'
187            elif student.current_mode == 'pg_pt':
188                xmldict['institution_acct'] = '0031716047'
189                xmldict['institution_bank_id'] = '10'
190            elif student.current_mode == 'dp_pt':
191                xmldict['institution_acct'] = '9201805071'
192                xmldict['institution_bank_id'] = '17'
193            else:
194                xmldict['institution_bank_id'] = '7'
195                xmldict['institution_acct'] = '1003475516'
196        elif self.context.p_category == 'gown':
197            self.pay_item_id = '5704'
198            provider_amt = 0
199            xmldict['institution_bank_id'] = '7'
200            xmldict['institution_acct'] = '1016232382'
201        elif self.context.p_category.startswith('hostel_maintenance'):
202            self.pay_item_id = '5705'
203            provider_amt = 0
204            xmldict['institution_bank_id'] = '129'
205            xmldict['institution_acct'] = '0014414547'
206
207        xmldict['provider_amt'] = 100 * provider_amt
208        xmldict['institution_item_name'] = self.category
209        xmldict['institution_name'] = INSTITUTION_NAME
210        xmldict['institution_amt'] = 100 * (
211            self.context.amount_auth - provider_amt - 150)
212        # Interswitch amount is not part of the xml data
213        if provider_amt == 0:
214            xmltext = """<payment_item_detail>
215<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
216<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" />
217</item_details>
218</payment_item_detail>""" % xmldict
219        else:
220            xmltext = """<payment_item_detail>
221<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
222<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" />
223<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" />
224</item_details>
225</payment_item_detail>""" % xmldict
226        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
227        return
228
229class InterswitchPageApplicant(KofaPage):
230    """ View which sends a POST request to the Interswitch
231    CollegePAY payment gateway.
232    """
233    grok.context(ICustomApplicantOnlinePayment)
234    grok.require('waeup.payApplicant')
235    grok.template('applicant_goto_interswitch')
236    grok.name('goto_interswitch')
237    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
238    submit_button = _('Submit')
239    action = POST_ACTION
240    site_name = SITE_NAME
241    currency = CURRENCY
242    pay_item_id = '5703'
243    product_id = PRODUCT_ID
244
245    def update(self):
246        if self.context.p_state != 'unpaid':
247            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
248            self.redirect(self.url(self.context, '@@index'))
249            return
250        if self.context.__parent__.__parent__.expired \
251            and self.context.__parent__.__parent__.strict_deadline:
252            self.flash(_("Payment ticket can't be send to CollegePAY. "
253                         "Application period has expired."))
254            self.redirect(self.url(self.context, '@@index'))
255            return
256        self.applicant = self.context.__parent__
257        self.amount_auth = 100 * self.context.amount_auth
258        xmldict = {}
259        self.category = getUtility(IKofaUtils).PAYMENT_CATEGORIES[self.context.p_category]
260        tz = getUtility(IKofaUtils).tzinfo
261        self.local_date_time = to_timezone(
262            self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
263        self.site_redirect_url = self.url(self.context, 'request_webservice')
264        provider_amt = 400
265        if self.applicant.applicant_id.startswith('pg'):
266            xmldict['institution_acct'] = '0031716030'
267            xmldict['institution_bank_id'] = '10'
268        elif self.applicant.applicant_id.startswith('dp'):
269            xmldict['institution_acct'] = '9201805071'
270            xmldict['institution_bank_id'] = '17'
271        else:
272            xmldict['institution_acct'] = '6220032503'
273            xmldict['institution_bank_id'] = '51'
274        xmldict['detail_ref'] = self.context.p_id
275        xmldict['provider_amt'] = 100 * provider_amt
276        xmldict['provider_acct'] = PROVIDER_ACCT
277        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
278        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
279        xmldict['institution_amt'] = 100 * (self.context.amount_auth - provider_amt - 150)
280        xmldict['institution_item_name'] = self.context.p_category
281        xmldict['institution_name'] = INSTITUTION_NAME
282        # Interswitch amount is not part of the xml data
283        xmltext = """<payment_item_detail>
284<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
285<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" />
286<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" />
287</item_details>
288</payment_item_detail>""" % xmldict
289        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
290        return
291
292# Deprecated
293#class InterswitchPaymentCallbackPageStudent(UtilityView, grok.View):
294#    """ Callback view for the CollegePAY gateway
295#    """
296#    grok.context(ICustomStudentOnlinePayment)
297#    grok.name('isw_callback')
298#    grok.require('waeup.payStudent')
299
300    # This view is not yet working for offline querying transactions
301    # since the query string differs from the query string sent after
302    # posting transactions. This Interswitch bug must be removed first.
303    # Alternatively, we could use the webservice only and replace
304    # the RequestCallbackActionButton by a RequestWebserviceActionButton
305
306#    def update(self):
307#        if self.context.p_state == 'paid':
308#            self.flash(_('This ticket has already been paid.'))
309#            return
310#        student = self.context.student
311#        query = self.request.form
312#        write_log_message(self,'callback received: %s' % query)
313#        self.context.r_card_num = query.get('cardNum', None)
314#        self.context.r_code = query.get('resp', None)
315#        self.context.r_pay_reference  = query.get('payRef', None)
316#        self.context.r_amount_approved = float(query.get('apprAmt', '0.0')) / 100
317#        self.context.r_desc = query.get('desc', None)
318#        if self.context.r_code != '00':
319#            self.flash(_('Unsuccessful callback: ${a}',
320#                mapping = {'a': query.get('desc', _('Incomplete query string.'))}))
321#            write_log_message(self,'unsuccessful callback: %s' % self.context.p_id)
322#            self.context.p_state = 'failed'
323#            return
324#        if self.context.r_amount_approved != payment.amount_auth:
325#            self.flash(_('Wrong amount'))
326#            write_log_message(
327#                self,'successful but wrong amount: %s' % self.context.p_id)
328#            self.context.p_state = 'failed'
329#            return
330#        try:
331#            validation_list = get_SOAP_response(
332#                PRODUCT_ID, self.context.p_id).split(':')
333            # Validation does not make sense yet since the query string
334            # formats are conflicting. We are only printing the validation
335            # string, nothing else.
336#            print 'WARNING: Webservice validation is not yet implemented'
337#            print 'validation list: %s' % validation_list
338#        except:
339#            print 'Connection to webservice failed.'
340        # Add webservice validation here
341#        write_log_message(self,'valid callback: %s' % self.context.p_id)
342#        self.context.p_state = 'paid'
343#        self.context.payment_date = datetime.utcnow()
344#        actions_after_student_payment(student, self.context, self)
345#        return
346
347#    def render(self):
348#        self.redirect(self.url(self.context, '@@index'))
349#        return
350
351# Alternative solution, replaces InterswitchPaymentCallbackPage
352class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View):
353    """ Request webservice view for the CollegePAY gateway
354    """
355    grok.context(ICustomStudentOnlinePayment)
356    grok.name('request_webservice')
357    grok.require('waeup.payStudent')
358
359    def update(self):
360        if self.context.p_state == 'paid':
361            self.flash(_('This ticket has already been paid.'))
362            return
363        student = self.context.student
364        success, msg, log = query_interswitch(
365            self.context, PRODUCT_ID, HOST, URL)
366        student.writeLogMessage(self, log)
367        if not success:
368            self.flash(msg)
369            return
370        success, msg, log = self.context.doAfterStudentPayment()
371        if log is not None:
372            student.writeLogMessage(self, log)
373        self.flash(msg)
374        return
375
376    def render(self):
377        self.redirect(self.url(self.context, '@@index'))
378        return
379
380class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View):
381    """ Request webservice view for the CollegePAY gateway
382    """
383    grok.context(ICustomApplicantOnlinePayment)
384    grok.name('request_webservice')
385    grok.require('waeup.payApplicant')
386
387    def update(self):
388        if self.context.p_state == 'paid':
389            self.flash(_('This ticket has already been paid.'))
390            return
391        applicant = self.context.__parent__
392        success, msg, log = query_interswitch(
393            self.context, PRODUCT_ID, HOST, URL)
394        applicant.writeLogMessage(self, log)
395        if not success:
396            self.flash(msg)
397            return
398        success, msg, log = self.context.doAfterApplicantPayment()
399        if log is not None:
400            applicant.writeLogMessage(self, log)
401        self.flash(msg)
402        return
403
404    def render(self):
405        self.redirect(self.url(self.context, '@@index'))
406        return
Note: See TracBrowser for help on using the repository browser.