1 | ## $Id: interswitch.py 7894 2012-03-16 07:24:24Z 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 | ## |
---|
18 | from datetime import datetime |
---|
19 | from urllib import urlencode |
---|
20 | import grok |
---|
21 | from waeup.kofa.browser.layout import KofaPage, UtilityView |
---|
22 | from waeup.kofa.accesscodes import create_accesscode |
---|
23 | from waeup.kofa.students.interfaces import IStudentOnlinePayment |
---|
24 | from waeup.kofa.students.browser import write_log_message |
---|
25 | from waeup.kofa.students.viewlets import RequestCallbackActionButton |
---|
26 | from waeup.custom.interfaces import MessageFactory as _ |
---|
27 | |
---|
28 | PRODUCT_ID = '57' |
---|
29 | SITE_NAME = 'xyz.waeup.org' |
---|
30 | PROVIDER_ACCT = '2345' |
---|
31 | PROVIDER_BANK_ID = '8' |
---|
32 | PROVIDER_ITEM_NAME = 'Kofa Provider Fee' |
---|
33 | INSTITUTION_ACCT = '1234' |
---|
34 | INSTITUTION_BANK_ID = '9' |
---|
35 | INSTITUTION_NAME = 'Sample University' |
---|
36 | CURRENCY = '566' |
---|
37 | PAY_ITEM_ID = '5700' |
---|
38 | QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx' |
---|
39 | POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx' |
---|
40 | |
---|
41 | class InterswitchActionButton(RequestCallbackActionButton): |
---|
42 | grok.order(2) |
---|
43 | icon = 'actionicon_pay.png' |
---|
44 | text = _('CollegePAY') |
---|
45 | target = 'goto_interswitch' |
---|
46 | |
---|
47 | @property |
---|
48 | def target_url(self): |
---|
49 | if self.context.p_state != 'unpaid': |
---|
50 | return '' |
---|
51 | return self.view.url(self.view.context, self.target) |
---|
52 | |
---|
53 | class InterswitchRequestCallbackActionButton(RequestCallbackActionButton): |
---|
54 | grok.order(3) |
---|
55 | icon = 'actionicon_call.png' |
---|
56 | text = _('Request CollegePAY callback') |
---|
57 | |
---|
58 | def target_url(self): |
---|
59 | if self.context.p_state == 'paid': |
---|
60 | return '' |
---|
61 | site_redirect_url = self.view.url(self.view.context, 'callback') |
---|
62 | args = { |
---|
63 | 'transRef':self.context.p_id, |
---|
64 | 'prodID':PRODUCT_ID, |
---|
65 | 'redirectURL':site_redirect_url} |
---|
66 | return QUERY_URL + '?%s' % urlencode(args) |
---|
67 | |
---|
68 | class InterswitchPage(KofaPage): |
---|
69 | """ View which sends a POST request to the Interswitch |
---|
70 | CollegePAY payment gateway. |
---|
71 | """ |
---|
72 | grok.context(IStudentOnlinePayment) |
---|
73 | grok.name('goto_interswitch') |
---|
74 | grok.template('goto_interswitch') |
---|
75 | grok.require('waeup.payStudent') |
---|
76 | label = _('Submit data to CollegePAY (Interswitch Payment Gateway)') |
---|
77 | submit_button = _('Submit') |
---|
78 | action = POST_ACTION |
---|
79 | site_name = SITE_NAME |
---|
80 | currency = CURRENCY |
---|
81 | pay_item_id = PAY_ITEM_ID |
---|
82 | product_id = PRODUCT_ID |
---|
83 | |
---|
84 | def update(self): |
---|
85 | if self.context.p_state != 'unpaid': |
---|
86 | self.flash(_("Payment ticket can't be re-send to CollegePAY.")) |
---|
87 | self.redirect(self.url(self.context, '@@index')) |
---|
88 | return |
---|
89 | self.student = self.context.getStudent() |
---|
90 | self.amount = (self.context.amount_auth + self.context.surcharge_1 + |
---|
91 | self.context.surcharge_2 + self.context.surcharge_3) |
---|
92 | self.amount_100 = 100 * self.amount |
---|
93 | self.local_date_time = str(self.context.creation_date) |
---|
94 | self.site_redirect_url = self.url(self.context, 'callback') |
---|
95 | certificate = getattr(self.student['studycourse'],'certificate',None) |
---|
96 | xmldict = {} |
---|
97 | if certificate is not None: |
---|
98 | xmldict['department'] = certificate.__parent__.__parent__.code |
---|
99 | xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code |
---|
100 | else: |
---|
101 | xmldict['department'] = None |
---|
102 | xmldict['faculty'] = None |
---|
103 | xmldict['detail_ref'] = self.context.p_id |
---|
104 | xmldict['provider_amt'] = 100 * self.context.surcharge_1 |
---|
105 | xmldict['provider_acct'] = PROVIDER_ACCT |
---|
106 | xmldict['provider_bank_id'] = PROVIDER_BANK_ID |
---|
107 | xmldict['provider_item_name'] = PROVIDER_ITEM_NAME |
---|
108 | xmldict['institution_amt'] = 100 * self.context.amount_auth |
---|
109 | xmldict['institution_acct'] = INSTITUTION_ACCT |
---|
110 | xmldict['institution_bank_id'] = INSTITUTION_BANK_ID |
---|
111 | xmldict['institution_item_name'] = self.context.p_category |
---|
112 | xmldict['institution_name'] = INSTITUTION_NAME |
---|
113 | # Interswitch amount is not part of the xml data |
---|
114 | xmltext = """<payment_item_detail> |
---|
115 | <item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s"> |
---|
116 | <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" /> |
---|
117 | <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" /> |
---|
118 | </item_details> |
---|
119 | </payment_item_detail>""" % xmldict |
---|
120 | self.xml_data = """<input type="hidden" name="xml_data" value='%s' />""" % xmltext |
---|
121 | return |
---|
122 | |
---|
123 | class OnlinePaymentCallbackPage(UtilityView, grok.View): |
---|
124 | """ Callback view for the CollegePAY gateway |
---|
125 | """ |
---|
126 | grok.context(IStudentOnlinePayment) |
---|
127 | grok.name('callback') |
---|
128 | grok.require('waeup.payStudent') |
---|
129 | |
---|
130 | # This view is not yet working for offline querying transactions |
---|
131 | # since the query string differs from the query string sent after |
---|
132 | # posting transactions. This Interswitch bug must be removed first. |
---|
133 | # Alternatively, we could use the webservice only and replace |
---|
134 | # the RequestCallbackActionButton by a RequestWebserviceActionButton |
---|
135 | |
---|
136 | def update(self): |
---|
137 | if self.context.p_state == 'paid': |
---|
138 | self.flash(_('This ticket has already been paid.')) |
---|
139 | return |
---|
140 | student = self.context.getStudent() |
---|
141 | query = self.request.form |
---|
142 | # Should be logged instead of printed |
---|
143 | print query |
---|
144 | if query.get('resp', None) != '00': |
---|
145 | self.flash(_('Unsuccessful callback: ${a}', |
---|
146 | mapping = {'a': query.get('desc', _('Incomplete query string.'))})) |
---|
147 | write_log_message(self,'invalid callback: %s' % self.context.p_id) |
---|
148 | self.context.r_card_num = query.get('cardNum', None) |
---|
149 | self.context.r_code = query.get('resp', None) |
---|
150 | self.context.p_state = 'failed' |
---|
151 | return |
---|
152 | |
---|
153 | # Add amount checking |
---|
154 | |
---|
155 | |
---|
156 | # Add webservice validation |
---|
157 | |
---|
158 | |
---|
159 | write_log_message(self,'valid callback: %s' % self.context.p_id) |
---|
160 | self.context.r_amount_approved = self.context.amount_auth |
---|
161 | self.context.r_card_num = query.get('cardNum', None) |
---|
162 | self.context.r_code = query.get('resp', None) |
---|
163 | self.context.r_desc = query.get('desc', None) |
---|
164 | self.context.r_pay_reference = query.get('payRef', None) |
---|
165 | self.context.p_state = 'paid' |
---|
166 | self.context.payment_date = datetime.now() |
---|
167 | |
---|
168 | if self.context.p_category == 'clearance': |
---|
169 | # Create CLR access code |
---|
170 | pin, error = create_accesscode('CLR',0,student.student_id) |
---|
171 | if error: |
---|
172 | self.flash(_('Valid callback received. ${a}', |
---|
173 | mapping = {'a':error})) |
---|
174 | return |
---|
175 | self.context.ac = pin |
---|
176 | elif self.context.p_category == 'schoolfee': |
---|
177 | # Create SFE access code |
---|
178 | pin, error = create_accesscode('SFE',0,student.student_id) |
---|
179 | if error: |
---|
180 | self.flash(_('Valid callback received. ${a}', |
---|
181 | mapping = {'a':error})) |
---|
182 | return |
---|
183 | self.context.ac = pin |
---|
184 | elif self.context.p_category == 'bed_allocation': |
---|
185 | # Create HOS access code |
---|
186 | pin, error = create_accesscode('HOS',0,student.student_id) |
---|
187 | if error: |
---|
188 | self.flash(_('Valid callback received. ${a}', |
---|
189 | mapping = {'a':error})) |
---|
190 | return |
---|
191 | self.context.ac = pin |
---|
192 | self.flash(_('Valid callback received.')) |
---|
193 | return |
---|
194 | |
---|
195 | def render(self): |
---|
196 | self.redirect(self.url(self.context, '@@index')) |
---|
197 | return |
---|