1 | ## $Id: studentsbrowser.py 14779 2017-08-07 17:04:11Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2017 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 | import grok |
---|
19 | import hashlib |
---|
20 | from datetime import datetime, timedelta |
---|
21 | from zope.component import getUtility |
---|
22 | from zope.security import checkPermission |
---|
23 | from waeup.kofa.interfaces import IKofaUtils |
---|
24 | from waeup.kofa.utils.helpers import to_timezone |
---|
25 | from waeup.kofa.browser.layout import UtilityView, KofaPage |
---|
26 | from waeup.kofa.browser.viewlets import ManageActionButton |
---|
27 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
28 | from waeup.kofa.students.browser import OnlinePaymentDisplayFormPage as OPDPStudent |
---|
29 | from kofacustom.nigeria.remita.helpers import ( |
---|
30 | get_JSON_POST_response, query_remita, write_payments_log) |
---|
31 | from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment |
---|
32 | from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment |
---|
33 | from kofacustom.nigeria.interfaces import MessageFactory as _ |
---|
34 | |
---|
35 | grok.templatedir('browser_templates') |
---|
36 | |
---|
37 | def module_activated(session): |
---|
38 | try: |
---|
39 | return getattr(grok.getSite()['configuration'][str(session)], |
---|
40 | 'remita_enabled', False) |
---|
41 | except KeyError: |
---|
42 | return False |
---|
43 | |
---|
44 | # Buttons |
---|
45 | |
---|
46 | class RemitaActionButtonStudent(ManageActionButton): |
---|
47 | grok.order(1) |
---|
48 | grok.context(INigeriaOnlinePayment) |
---|
49 | grok.view(OPDPStudent) |
---|
50 | grok.require('waeup.payStudent') |
---|
51 | icon = 'actionicon_pay.png' |
---|
52 | text = _('Pay via Remita') |
---|
53 | target = 'goto_remita' |
---|
54 | |
---|
55 | @property |
---|
56 | def target_url(self): |
---|
57 | if not module_activated(self.context.student.current_session): |
---|
58 | return '' |
---|
59 | if self.context.p_state != 'unpaid': |
---|
60 | return '' |
---|
61 | return self.view.url(self.view.context, self.target) |
---|
62 | |
---|
63 | class RemitaRequestPaymentStatusActionButtonStudent(ManageActionButton): |
---|
64 | grok.order(2) |
---|
65 | grok.context(INigeriaOnlinePayment) |
---|
66 | grok.view(OPDPStudent) |
---|
67 | grok.require('waeup.payStudent') |
---|
68 | icon = 'actionicon_call.png' |
---|
69 | text = _('Requery Remita Payment Status') |
---|
70 | target = 'request_payment_status' |
---|
71 | |
---|
72 | @property |
---|
73 | def target_url(self): |
---|
74 | if not module_activated(self.context.student.current_session): |
---|
75 | return '' |
---|
76 | if self.context.p_state in ('paid', 'waived'): |
---|
77 | return '' |
---|
78 | return self.view.url(self.view.context, self.target) |
---|
79 | |
---|
80 | |
---|
81 | class RemitaVerifyPaymentStatusActionButtonStudent(ManageActionButton): |
---|
82 | grok.order(3) |
---|
83 | grok.context(INigeriaOnlinePayment) |
---|
84 | grok.view(OPDPStudent) |
---|
85 | grok.require('waeup.manageStudent') |
---|
86 | icon = 'actionicon_call.png' |
---|
87 | text = _('Verify Remita Payment Status') |
---|
88 | target = 'verify_payment_status' |
---|
89 | |
---|
90 | @property |
---|
91 | def target_url(self): |
---|
92 | if not module_activated(self.context.student.current_session): |
---|
93 | return '' |
---|
94 | if self.context.p_state != 'paid' \ |
---|
95 | or self.context.r_company != u'remita': |
---|
96 | return '' |
---|
97 | return self.view.url(self.view.context, self.target) |
---|
98 | |
---|
99 | # Webservice request views |
---|
100 | |
---|
101 | class RemitaRequestPaymentStatusPageStudent(UtilityView, grok.View): |
---|
102 | """ Request webservice view for the Remita gateway. |
---|
103 | """ |
---|
104 | grok.context(INigeriaStudentOnlinePayment) |
---|
105 | grok.name('request_payment_status') |
---|
106 | grok.require('waeup.payStudent') |
---|
107 | |
---|
108 | # Here we use Remita test portal data |
---|
109 | merchantId = '2547916' |
---|
110 | host = 'www.remitademo.net' |
---|
111 | https = False |
---|
112 | api_key = '1946' |
---|
113 | |
---|
114 | def update(self): |
---|
115 | if not module_activated(self.context.student.current_session): |
---|
116 | return |
---|
117 | if self.context.p_state in ('paid', 'waived'): |
---|
118 | self.flash(_('This ticket has already been paid.'), type='danger') |
---|
119 | return |
---|
120 | student = self.context.student |
---|
121 | RRR = self.context.r_pay_reference |
---|
122 | if not RRR: |
---|
123 | self.flash(_('Remita Retrieval Reference not found.'), type='danger') |
---|
124 | return |
---|
125 | # Remita sends a POST request which may contain more information |
---|
126 | # if a payment was not successful. |
---|
127 | resp = self.request.form |
---|
128 | if resp and resp.get('statuscode') not in (None, '025', '00', '01'): |
---|
129 | self.flash('Transaction status message from Remita: %s' |
---|
130 | % resp.get('status'), type='warning') |
---|
131 | success, msg, log = query_remita( |
---|
132 | self.context, |
---|
133 | self.merchantId, |
---|
134 | self.api_key, |
---|
135 | RRR, |
---|
136 | self.host, |
---|
137 | self.https, |
---|
138 | False) |
---|
139 | student.writeLogMessage(self, log) |
---|
140 | if not success: |
---|
141 | self.flash(msg, type='danger') |
---|
142 | return |
---|
143 | write_payments_log(student.student_id, self.context) |
---|
144 | flashtype, msg, log = self.context.doAfterStudentPayment() |
---|
145 | if log is not None: |
---|
146 | student.writeLogMessage(self, log) |
---|
147 | self.flash(msg, type=flashtype) |
---|
148 | return |
---|
149 | |
---|
150 | def render(self): |
---|
151 | self.redirect(self.url(self.context, '@@index')) |
---|
152 | return |
---|
153 | |
---|
154 | class RemitaVerifyPaymentStatusPageStudent(UtilityView, grok.View): |
---|
155 | """ Request webservice view for the Remita gateway. |
---|
156 | """ |
---|
157 | grok.context(INigeriaStudentOnlinePayment) |
---|
158 | grok.name('verify_payment_status') |
---|
159 | grok.require('waeup.manageStudent') |
---|
160 | |
---|
161 | # Here we use Remita test portal data |
---|
162 | merchantId = '2547916' |
---|
163 | host = 'www.remitademo.net' |
---|
164 | https = False |
---|
165 | api_key = '1946' |
---|
166 | |
---|
167 | def update(self): |
---|
168 | if not module_activated(self.context.student.current_session): |
---|
169 | return |
---|
170 | if self.context.p_state != 'paid' \ |
---|
171 | or self.context.r_company != u'remita': |
---|
172 | self.flash(_('This ticket has not been paid.'), type='danger') |
---|
173 | return |
---|
174 | student = self.context.student |
---|
175 | RRR = self.context.r_pay_reference |
---|
176 | if not RRR: |
---|
177 | self.flash(_('Remita Retrieval Reference not found.'), type='danger') |
---|
178 | return |
---|
179 | # Remita sends a POST request which may contain more information |
---|
180 | # if a payment was not successful. |
---|
181 | resp = self.request.form |
---|
182 | if resp and resp.get('statuscode') not in (None, '025', '00', '01'): |
---|
183 | self.flash('Transaction status message from Remita: %s' |
---|
184 | % resp.get('status'), type='warning') |
---|
185 | success, msg, log = query_remita( |
---|
186 | self.context, |
---|
187 | self.merchantId, |
---|
188 | self.api_key, |
---|
189 | RRR, |
---|
190 | self.host, |
---|
191 | self.https, |
---|
192 | True) |
---|
193 | student.writeLogMessage(self, log) |
---|
194 | if not success: |
---|
195 | self.flash(msg, type='danger') |
---|
196 | return |
---|
197 | self.flash(msg) |
---|
198 | return |
---|
199 | |
---|
200 | def render(self): |
---|
201 | self.redirect(self.url(self.context, '@@index')) |
---|
202 | return |
---|
203 | |
---|
204 | |
---|
205 | # Forwarding pages |
---|
206 | |
---|
207 | class RemitaPageStudent(KofaPage): |
---|
208 | """ View which sends a POST request to the Remita payment gateway. |
---|
209 | """ |
---|
210 | grok.context(INigeriaOnlinePayment) |
---|
211 | grok.name('goto_remita') |
---|
212 | grok.template('goto_remita') |
---|
213 | grok.require('waeup.payStudent') |
---|
214 | label = _('Pay via Remita') |
---|
215 | submit_button = _('Pay now') |
---|
216 | https = False |
---|
217 | |
---|
218 | # Here we use Remita test portal data |
---|
219 | merchantId = '2547916' |
---|
220 | serviceTypeId = '4430731' |
---|
221 | api_key = '1946' |
---|
222 | orderId = '3456346346' |
---|
223 | host = 'www.remitademo.net' |
---|
224 | init_url = '/remita/ecomm/split/init.reg' |
---|
225 | amount='1000' |
---|
226 | lineitems = ( |
---|
227 | {"lineItemsId":"itemid1","beneficiaryName":"Klaus Mueller", |
---|
228 | "beneficiaryAccount":"6020067886","bankCode":"011", |
---|
229 | "beneficiaryAmount":"500","deductFeeFrom":"1"}, |
---|
230 | {"lineItemsId":"itemid2","beneficiaryName":"Werner Rumm", |
---|
231 | "beneficiaryAccount":"0360883515","bankCode":"050", |
---|
232 | "beneficiaryAmount":"500","deductFeeFrom":"0"} |
---|
233 | ) |
---|
234 | |
---|
235 | action = 'http://www.remitademo.net/remita/ecomm/finalize.reg' |
---|
236 | |
---|
237 | def init_update(self): |
---|
238 | if self.context.p_state == 'paid': |
---|
239 | return _("Payment ticket can't be re-sent to Remita.") |
---|
240 | now = datetime.utcnow() |
---|
241 | if self.context.creation_date.tzinfo is not None: |
---|
242 | # That's bad. Please store timezone-naive datetimes only! |
---|
243 | now = self.context.creation_date.tzinfo.localize(now) |
---|
244 | time_delta = now - self.context.creation_date |
---|
245 | if time_delta.days > 7: |
---|
246 | return _("This payment ticket is too old. Please create a new ticket.") |
---|
247 | certificate = getattr(self.context.student['studycourse'],'certificate',None) |
---|
248 | if certificate is None: |
---|
249 | return _("Study course data are incomplete.") |
---|
250 | kofa_utils = getUtility(IKofaUtils) |
---|
251 | student_utils = getUtility(IStudentsUtils) |
---|
252 | if student_utils.samePaymentMade(self.context.student, self.context.p_category, |
---|
253 | self.context.p_item, self.context.p_session): |
---|
254 | return _("This type of payment has already been made.") |
---|
255 | self.responseurl = self.url(self.context, 'request_payment_status') |
---|
256 | resp = get_JSON_POST_response( |
---|
257 | merchantId=self.merchantId, |
---|
258 | serviceTypeId=self.serviceTypeId, |
---|
259 | api_key=self.api_key, |
---|
260 | orderId=self.orderId, |
---|
261 | amount=self.amount, |
---|
262 | responseurl=self.responseurl, |
---|
263 | host=self.host, |
---|
264 | url=self.init_url, |
---|
265 | https=self.https, |
---|
266 | fullname=self.context.student.display_fullname, |
---|
267 | email=self.context.student.email, |
---|
268 | lineitems=self.lineitems) |
---|
269 | if resp.get('error'): |
---|
270 | return resp.get('error') |
---|
271 | if resp.get('statuscode') not in ('021', '025', '055'): |
---|
272 | return 'RRR generation message from Remita: ' + resp.get('status') |
---|
273 | # Already now it becomes a Remita payment |
---|
274 | self.context.r_company = u'remita' |
---|
275 | self.rrr = self.context.r_pay_reference = resp['RRR'].rstrip() |
---|
276 | hashargs = self.merchantId + self.rrr + self.api_key |
---|
277 | self.hashvalue = hashlib.sha512(hashargs).hexdigest() |
---|
278 | self.customer = self.context.student |
---|
279 | self.customer.writeLogMessage(self, 'RRR retrieved: %s' % self.rrr) |
---|
280 | return |
---|
281 | |
---|
282 | def update(self): |
---|
283 | if not module_activated(self.context.student.current_session): |
---|
284 | return |
---|
285 | error = self.init_update() |
---|
286 | if error: |
---|
287 | self.flash(error, type='danger') |
---|
288 | self.redirect(self.url(self.context, '@@index')) |
---|
289 | return |
---|
290 | return |
---|