source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/remita/applicantsbrowser.py @ 15505

Last change on this file since 15505 was 15456, checked in by Henrik Bettermann, 5 years ago

Add verdict.

File size: 10.4 KB
Line 
1## $Id: browser.py 14759 2017-08-03 09:09:54Z 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##
18import grok
19import hashlib
20from datetime import datetime, timedelta
21from zope.component import getUtility
22from zope.security import checkPermission
23from waeup.kofa.interfaces import IKofaUtils
24from waeup.kofa.utils.helpers import to_timezone
25from waeup.kofa.browser.layout import UtilityView, KofaPage
26from waeup.kofa.browser.viewlets import ManageActionButton
27from waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant
28from kofacustom.nigeria.remita.helpers import (
29    get_JSON_POST_response, query_remita, write_payments_log)
30from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
31from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
32from kofacustom.nigeria.interfaces import MessageFactory as _
33from kofacustom.nigeria.remita.studentsbrowser import module_activated
34
35from kofacustom.nigeria.remita.tests import (
36    MERCHANTID, HOST, HTTPS, API_KEY, SERVICETYPEID)
37
38grok.templatedir('browser_templates')
39
40# Buttons
41
42class RemitaActionButtonApplicant(ManageActionButton):
43    grok.order(1)
44    grok.context(INigeriaOnlinePayment)
45    grok.view(OPDPApplicant)
46    grok.require('waeup.payApplicant')
47    icon = 'actionicon_pay.png'
48    text = _('Pay via Remita')
49    target = 'goto_remita'
50
51    @property
52    def target_url(self):
53        if not module_activated(self.context.__parent__.__parent__.year):
54            return ''
55        if self.context.p_state != 'unpaid':
56            return ''
57        return self.view.url(self.view.context, self.target)
58
59class RemitaRequestPaymentStatusActionButtonApplicant(ManageActionButton):
60    grok.order(2)
61    grok.context(INigeriaOnlinePayment)
62    grok.view(OPDPApplicant)
63    grok.require('waeup.payApplicant')
64    icon = 'actionicon_call.png'
65    text = _('Requery Remita Payment Status')
66    target = 'request_payment_status'
67
68    @property
69    def target_url(self):
70        if not module_activated(self.context.__parent__.__parent__.year):
71            return ''
72        if self.context.p_state in ('paid', 'waived'):
73            return ''
74        return self.view.url(self.view.context, self.target)
75
76class RemitaVerifyPaymentStatusActionButtonApplicant(ManageActionButton):
77    grok.order(3)
78    grok.context(INigeriaOnlinePayment)
79    grok.view(OPDPApplicant)
80    grok.require('waeup.manageApplication')
81    icon = 'actionicon_call.png'
82    text = _('Verify Remita Payment Status')
83    target = 'verify_payment_status'
84
85    @property
86    def target_url(self):
87        if not module_activated(self.context.__parent__.__parent__.year):
88            return ''
89        if self.context.p_state != 'paid' \
90            or self.context.r_company != u'remita':
91            return ''
92        return self.view.url(self.view.context, self.target)
93
94# Webservice request views
95
96class RemitaRequestPaymentStatusPageApplicant(UtilityView, grok.View):
97    """ Request webservice view for the Remita gateway.
98    """
99    grok.context(INigeriaApplicantOnlinePayment)
100    grok.name('request_payment_status')
101    grok.require('waeup.payApplicant')
102
103    merchantId = MERCHANTID
104    host = HOST
105    https = HTTPS
106    api_key = API_KEY
107
108    def update(self):
109        if not module_activated(self.context.__parent__.__parent__.year):
110            return
111        if self.context.p_state in ('paid', 'waived'):
112            self.flash(_('This ticket has already been paid.'), type='danger')
113            return
114        applicant = self.context.__parent__
115        RRR = self.context.r_pay_reference
116        if not RRR:
117            self.flash(_('Remita Retrieval Reference not found.'), type='danger')
118            return
119        # Remita sends a POST request which may contain more information
120        # if a payment was not successful.
121        resp = self.request.form
122        if resp and resp.get('statuscode') not in (None, '025', '00', '01'):
123            self.flash('Transaction status message from Remita: %s'
124                % resp.get('status'), type='warning')
125        success, msg, log = query_remita(
126            self.context,
127            self.merchantId,
128            self.api_key,
129            RRR,
130            self.host,
131            self.https,
132            False)
133        applicant.writeLogMessage(self, log)
134        if not success:
135            self.flash(msg, type='danger')
136            return
137        write_payments_log(applicant.applicant_id, self.context)
138        flashtype, msg, log = self.context.doAfterApplicantPayment()
139        if log is not None:
140            applicant.writeLogMessage(self, log)
141        self.flash(msg, type=flashtype)
142        return
143
144    def render(self):
145        if self.context.p_state == 'paid':
146            self.redirect(self.url(self.context.__parent__))
147            return
148        self.redirect(self.url(self.context))
149        return
150
151class RemitaVerifyPaymentStatusPageApplicant(UtilityView, grok.View):
152    """ Request webservice view for the Remita gateway.
153    """
154    grok.context(INigeriaApplicantOnlinePayment)
155    grok.name('verify_payment_status')
156    grok.require('waeup.manageApplication')
157
158    merchantId = MERCHANTID
159    host = HOST
160    https = HTTPS
161    api_key = API_KEY
162
163    def update(self):
164        if not module_activated(self.context.__parent__.__parent__.year):
165            return
166        if self.context.p_state  != 'paid' \
167            or self.context.r_company != u'remita':
168            self.flash(_('This ticket has not been paid.'), type='danger')
169            return
170        applicant = self.context.__parent__
171        RRR = self.context.r_pay_reference
172        if not RRR:
173            self.flash(_('Remita Retrieval Reference not found.'), type='danger')
174            return
175        # Remita sends a POST request which may contain more information
176        # if a payment was not successful.
177        resp = self.request.form
178        if resp and resp.get('statuscode') not in (None, '025', '00', '01'):
179            self.flash('Transaction status message from Remita: %s'
180                % resp.get('status'), type='warning')
181        success, msg, log = query_remita(
182            self.context,
183            self.merchantId,
184            self.api_key,
185            RRR,
186            self.host,
187            self.https,
188            True)
189        applicant.writeLogMessage(self, log)
190        if not success:
191            self.flash(msg, type='danger')
192            return
193        self.flash(msg)
194        return
195
196    def render(self):
197        self.redirect(self.url(self.context))
198        return
199
200
201# Forwarding pages
202
203class RemitaPageApplicant(KofaPage):
204    """ View which sends a POST request to the Remita payment gateway.
205    """
206    grok.context(INigeriaApplicantOnlinePayment)
207    grok.name('goto_remita')
208    grok.template('goto_remita')
209    grok.require('waeup.payApplicant')
210    label = _('Pay via Remita')
211    submit_button = _('Pay now')
212
213    merchantId = MERCHANTID
214    host = HOST
215    https = HTTPS
216    api_key = API_KEY
217    serviceTypeId = SERVICETYPEID
218
219    #orderId = '3456346346'
220    init_url = '/remita/ecomm/split/init.reg'
221    amount='1000'
222    lineitems = (
223                  {"lineItemsId":"itemid1","beneficiaryName":"Klaus Mueller",
224                  "beneficiaryAccount":"6020067886","bankCode":"011",
225                  "beneficiaryAmount":"500","deductFeeFrom":"1"},
226                  {"lineItemsId":"itemid2","beneficiaryName":"Werner Rumm",
227                  "beneficiaryAccount":"0360883515","bankCode":"050",
228                  "beneficiaryAmount":"500","deductFeeFrom":"0"}
229                )
230
231    @property
232    def action(self):
233        if self.https:
234            return 'https://' + self.host + '/remita/ecomm/finalize.reg'
235        return 'http://' + self.host + '/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        self.responseurl = self.url(self.context, 'request_payment_status')
248        resp = get_JSON_POST_response(
249            merchantId=self.merchantId,
250            serviceTypeId=self.serviceTypeId,
251            api_key=self.api_key,
252            orderId=self.orderId,
253            amount=self.amount,
254            responseurl=self.responseurl,
255            host=self.host,
256            url=self.init_url,
257            https=self.https,
258            fullname=self.context.__parent__.display_fullname,
259            email=self.context.__parent__.email,
260            lineitems=self.lineitems)
261        if resp.get('error'):
262            return resp.get('error')
263        if resp.get('statuscode') not in ('021', '025', '055'):
264            return 'RRR generation message from Remita: ' + resp.get('status')
265        # Already now it becomes a Remita payment
266        self.context.r_company = u'remita'
267        self.rrr = self.context.r_pay_reference = resp['RRR'].rstrip()
268        hashargs =      self.merchantId + self.rrr + self.api_key
269        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
270        self.customer = self.context.__parent__
271        self.customer.writeLogMessage(self,
272            'RRR retrieved: %s, ServiceTypeId: %s'
273            % (self.rrr, self.serviceTypeId))
274
275        return
276
277    def update(self):
278        if not module_activated(self.context.__parent__.__parent__.year):
279            return
280        self.orderId = self.context.p_id
281        error = self.init_update()
282        if error:
283            self.flash(error, type='danger')
284            self.redirect(self.url(self.context, '@@index'))
285            return
286        return
Note: See TracBrowser for help on using the repository browser.