[9781] | 1 | ## $Id: browser.py 13007 2015-05-27 15:55:13Z 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 | import grok |
---|
[12973] | 19 | from datetime import datetime, timedelta |
---|
[11630] | 20 | from zope.component import getUtility |
---|
| 21 | from waeup.kofa.interfaces import IKofaUtils |
---|
| 22 | from waeup.kofa.utils.helpers import to_timezone |
---|
| 23 | from waeup.kofa.browser.layout import UtilityView, KofaPage |
---|
[9781] | 24 | from waeup.kofa.browser.viewlets import ManageActionButton |
---|
[11642] | 25 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
[9781] | 26 | from waeup.kofa.students.browser import OnlinePaymentDisplayFormPage as OPDPStudent |
---|
| 27 | from waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant |
---|
| 28 | from kofacustom.nigeria.interswitch.helpers import ( |
---|
| 29 | query_interswitch, write_payments_log) |
---|
| 30 | from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment |
---|
| 31 | from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment |
---|
| 32 | from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment |
---|
| 33 | from kofacustom.nigeria.interfaces import MessageFactory as _ |
---|
| 34 | |
---|
| 35 | class InterswitchActionButtonStudent(ManageActionButton): |
---|
| 36 | grok.order(1) |
---|
| 37 | grok.context(INigeriaOnlinePayment) |
---|
| 38 | grok.view(OPDPStudent) |
---|
| 39 | grok.require('waeup.payStudent') |
---|
| 40 | icon = 'actionicon_pay.png' |
---|
| 41 | text = _('CollegePAY') |
---|
| 42 | target = 'goto_interswitch' |
---|
| 43 | |
---|
| 44 | @property |
---|
| 45 | def target_url(self): |
---|
| 46 | if self.context.p_state != 'unpaid': |
---|
| 47 | return '' |
---|
| 48 | return self.view.url(self.view.context, self.target) |
---|
| 49 | |
---|
| 50 | class InterswitchActionButtonApplicant(InterswitchActionButtonStudent): |
---|
| 51 | grok.view(OPDPApplicant) |
---|
| 52 | grok.require('waeup.payApplicant') |
---|
| 53 | |
---|
| 54 | class InterswitchRequestWebserviceActionButtonStudent(ManageActionButton): |
---|
| 55 | grok.order(2) |
---|
| 56 | grok.context(INigeriaOnlinePayment) |
---|
| 57 | grok.view(OPDPStudent) |
---|
| 58 | grok.require('waeup.payStudent') |
---|
| 59 | icon = 'actionicon_call.png' |
---|
| 60 | text = _('Requery CollegePAY') |
---|
| 61 | target = 'request_webservice' |
---|
| 62 | |
---|
| 63 | @property |
---|
| 64 | def target_url(self): |
---|
| 65 | if self.context.p_state == 'paid': |
---|
| 66 | return '' |
---|
| 67 | return self.view.url(self.view.context, self.target) |
---|
| 68 | |
---|
| 69 | class InterswitchRequestWebserviceActionButtonApplicant( |
---|
| 70 | InterswitchRequestWebserviceActionButtonStudent): |
---|
| 71 | grok.view(OPDPApplicant) |
---|
| 72 | grok.require('waeup.payApplicant') |
---|
| 73 | |
---|
| 74 | class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View): |
---|
| 75 | """ Request webservice view for the CollegePAY gateway |
---|
| 76 | """ |
---|
| 77 | grok.context(INigeriaStudentOnlinePayment) |
---|
| 78 | grok.name('request_webservice') |
---|
| 79 | grok.require('waeup.payStudent') |
---|
| 80 | |
---|
| 81 | product_id = None |
---|
| 82 | gateway_host = None |
---|
| 83 | gateway_url = None |
---|
[11915] | 84 | https = False |
---|
[9781] | 85 | |
---|
| 86 | def update(self): |
---|
| 87 | if self.context.p_state == 'paid': |
---|
[11579] | 88 | self.flash(_('This ticket has already been paid.'), type='warning') |
---|
[9781] | 89 | return |
---|
| 90 | student = self.context.student |
---|
| 91 | success, msg, log = query_interswitch( |
---|
[11915] | 92 | self.context, self.product_id, |
---|
| 93 | self.gateway_host, self.gateway_url, |
---|
| 94 | self.https) |
---|
[9781] | 95 | student.writeLogMessage(self, log) |
---|
| 96 | if not success: |
---|
[11579] | 97 | self.flash(msg, type='danger') |
---|
[9781] | 98 | return |
---|
| 99 | write_payments_log(student.student_id, self.context) |
---|
[11581] | 100 | flashtype, msg, log = self.context.doAfterStudentPayment() |
---|
[9781] | 101 | if log is not None: |
---|
| 102 | student.writeLogMessage(self, log) |
---|
[11581] | 103 | self.flash(msg, type=flashtype) |
---|
[9781] | 104 | return |
---|
| 105 | |
---|
| 106 | def render(self): |
---|
| 107 | self.redirect(self.url(self.context, '@@index')) |
---|
| 108 | return |
---|
| 109 | |
---|
| 110 | class InterswitchPaymentRequestWebservicePageApplicant(UtilityView, grok.View): |
---|
| 111 | """ Request webservice view for the CollegePAY gateway |
---|
| 112 | """ |
---|
| 113 | grok.context(INigeriaApplicantOnlinePayment) |
---|
| 114 | grok.name('request_webservice') |
---|
| 115 | grok.require('waeup.payApplicant') |
---|
| 116 | |
---|
| 117 | product_id = None |
---|
| 118 | gateway_host = None |
---|
| 119 | gateway_url = None |
---|
[11915] | 120 | https = False |
---|
[9781] | 121 | |
---|
| 122 | def update(self): |
---|
| 123 | if self.context.p_state == 'paid': |
---|
[11642] | 124 | self.flash(_('This ticket has already been paid.'), type='danger') |
---|
[9781] | 125 | return |
---|
| 126 | applicant = self.context.__parent__ |
---|
| 127 | success, msg, log = query_interswitch( |
---|
[11915] | 128 | self.context, self.product_id, |
---|
| 129 | self.gateway_host, self.gateway_url, |
---|
| 130 | self.https) |
---|
[9781] | 131 | applicant.writeLogMessage(self, log) |
---|
| 132 | if not success: |
---|
[11579] | 133 | self.flash(msg, type='danger') |
---|
[9781] | 134 | return |
---|
| 135 | write_payments_log(applicant.applicant_id, self.context) |
---|
[11581] | 136 | flashtype, msg, log = self.context.doAfterApplicantPayment() |
---|
[9781] | 137 | if log is not None: |
---|
| 138 | applicant.writeLogMessage(self, log) |
---|
[11581] | 139 | self.flash(msg, type=flashtype) |
---|
[9781] | 140 | return |
---|
| 141 | |
---|
| 142 | def render(self): |
---|
| 143 | self.redirect(self.url(self.context, '@@index')) |
---|
| 144 | return |
---|
[11630] | 145 | |
---|
| 146 | class InterswitchPageStudent(KofaPage): |
---|
| 147 | """ View which sends a POST request to the Interswitch |
---|
| 148 | CollegePAY payment gateway. |
---|
| 149 | """ |
---|
| 150 | grok.context(INigeriaOnlinePayment) |
---|
| 151 | grok.name('goto_interswitch') |
---|
| 152 | grok.template('student_goto_interswitch') |
---|
| 153 | grok.require('waeup.payStudent') |
---|
| 154 | label = _('Submit data to CollegePAY (Interswitch Payment Gateway)') |
---|
| 155 | submit_button = _('Submit') |
---|
| 156 | |
---|
[11639] | 157 | action = None |
---|
| 158 | site_name = None |
---|
| 159 | currency = None |
---|
| 160 | pay_item_id = None |
---|
| 161 | product_id = None |
---|
| 162 | xml_data = None |
---|
[12477] | 163 | hashvalue = None |
---|
[11639] | 164 | |
---|
[11642] | 165 | def init_update(self): |
---|
[11630] | 166 | if self.context.p_state == 'paid': |
---|
[11642] | 167 | return _("Payment ticket can't be re-sent to CollegePAY.") |
---|
[12973] | 168 | tz = getUtility(IKofaUtils).tzinfo |
---|
| 169 | time_delta = datetime.utcnow() - self.context.creation_date |
---|
[13007] | 170 | if self.context.created_online and time_delta.seconds > 3600: |
---|
[12973] | 171 | return _("This payment ticket is too old. Please create a new ticket.") |
---|
[11642] | 172 | student = self.context.student |
---|
[11630] | 173 | certificate = getattr(student['studycourse'],'certificate',None) |
---|
| 174 | if certificate is None: |
---|
[11642] | 175 | return _("Study course data are incomplete.") |
---|
[11639] | 176 | kofa_utils = getUtility(IKofaUtils) |
---|
[11642] | 177 | student_utils = getUtility(IStudentsUtils) |
---|
| 178 | if student_utils.samePaymentMade(student, self.context.p_category, |
---|
| 179 | self.context.p_item, self.context.p_session): |
---|
| 180 | return _("This type of payment has already been made.") |
---|
[11767] | 181 | self.amount_auth = int(100 * self.context.amount_auth) |
---|
[11630] | 182 | xmldict = {} |
---|
| 183 | if certificate is not None: |
---|
| 184 | xmldict['department'] = certificate.__parent__.__parent__.code |
---|
| 185 | xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code |
---|
| 186 | else: |
---|
| 187 | xmldict['department'] = None |
---|
| 188 | xmldict['faculty'] = None |
---|
[12509] | 189 | self.category = self.context.category |
---|
[11639] | 190 | tz = kofa_utils.tzinfo |
---|
[11630] | 191 | self.local_date_time = to_timezone( |
---|
| 192 | self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
| 193 | self.site_redirect_url = self.url(self.context, 'request_webservice') |
---|
[11642] | 194 | self.student = student |
---|
| 195 | self.xmldict = xmldict |
---|
| 196 | return |
---|
[11630] | 197 | |
---|
[11642] | 198 | def update(self): |
---|
| 199 | error = self.init_update() |
---|
| 200 | if error: |
---|
| 201 | self.flash(error, type='danger') |
---|
| 202 | self.redirect(self.url(self.context, '@@index')) |
---|
| 203 | return |
---|
| 204 | |
---|
[11630] | 205 | class InterswitchPageApplicant(KofaPage): |
---|
| 206 | """ View which sends a POST request to the Interswitch |
---|
| 207 | CollegePAY payment gateway. |
---|
| 208 | """ |
---|
| 209 | grok.context(INigeriaApplicantOnlinePayment) |
---|
| 210 | grok.require('waeup.payApplicant') |
---|
| 211 | grok.template('applicant_goto_interswitch') |
---|
| 212 | grok.name('goto_interswitch') |
---|
| 213 | label = _('Submit data to CollegePAY (Interswitch Payment Gateway)') |
---|
| 214 | submit_button = _('Submit') |
---|
[12477] | 215 | hashvalue = None |
---|
[11630] | 216 | |
---|
[11639] | 217 | action = None |
---|
| 218 | site_name = None |
---|
| 219 | currency = None |
---|
| 220 | pay_item_id = None |
---|
| 221 | product_id = None |
---|
| 222 | xml_data = None |
---|
| 223 | |
---|
[11642] | 224 | def init_update(self): |
---|
[11630] | 225 | if self.context.p_state != 'unpaid': |
---|
[11642] | 226 | return _("Payment ticket can't be re-sent to CollegePAY.") |
---|
[11630] | 227 | if self.context.__parent__.__parent__.expired \ |
---|
| 228 | and self.context.__parent__.__parent__.strict_deadline: |
---|
[11642] | 229 | return _("Payment ticket can't be send to CollegePAY. " |
---|
| 230 | "Application period has expired.") |
---|
[12973] | 231 | tz = getUtility(IKofaUtils).tzinfo |
---|
| 232 | time_delta = datetime.utcnow() - self.context.creation_date |
---|
| 233 | if time_delta.seconds > 3600: |
---|
| 234 | return _("This payment ticket is too old. Please create a new ticket.") |
---|
[11630] | 235 | self.applicant = self.context.__parent__ |
---|
[11767] | 236 | self.amount_auth = int(100 * self.context.amount_auth) |
---|
[12509] | 237 | self.category = self.context.category |
---|
[11630] | 238 | tz = getUtility(IKofaUtils).tzinfo |
---|
| 239 | self.local_date_time = to_timezone( |
---|
| 240 | self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
| 241 | self.site_redirect_url = self.url(self.context, 'request_webservice') |
---|
[11642] | 242 | return |
---|
| 243 | |
---|
| 244 | def update(self): |
---|
| 245 | error = self.init_update() |
---|
| 246 | if error: |
---|
| 247 | self.flash(error, type='danger') |
---|
| 248 | self.redirect(self.url(self.context, '@@index')) |
---|
[11630] | 249 | return |
---|