source: main/waeup.kwarapoly/trunk/src/waeup/kwarapoly/interswitch/browser.py @ 9762

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

Import helper functions from kofacustom.nigeria.

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