source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/paypal/applicantsbrowser.py @ 17911

Last change on this file since 17911 was 17260, checked in by Henrik Bettermann, 2 years ago

We do not verify payments.

File size: 7.5 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, queryUtility
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
28#from 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.paypal import module_activated, Amount, PurchaseUnit
34from kofacustom.nigeria.paypal.interfaces import IPaypalConfig
35from kofacustom.nigeria.paypal.rest import create_order, capture_order
36
37
38# grok.templatedir('browser_templates')
39
40
41# Buttons
42
43
44class PaypalActionButtonApplicant(ManageActionButton):
45    grok.order(1)
46    grok.context(INigeriaOnlinePayment)
47    grok.view(OPDPApplicant)
48    grok.require('waeup.payApplicant')
49    icon = 'actionicon_paypal.png'
50    text = _('Pay via Paypal')
51    target = 'goto_paypal'
52
53    @property
54    def target_url(self):
55        # import pdb; pdb.set_trace()
56        if not module_activated(
57            self.context.__parent__.__parent__.year, self.context):
58            return ''
59        if self.context.p_state != 'unpaid':
60            return ''
61        return self.view.url(self.view.context, self.target)
62
63
64class PaypalRequestPaymentStatusActionButtonApplicant(ManageActionButton):
65    grok.order(2)
66    grok.context(INigeriaOnlinePayment)
67    grok.view(OPDPApplicant)
68    grok.require('waeup.payApplicant')
69    icon = 'actionicon_call.png'
70    text = _('Requery Paypal Payment Status')
71    target = 'capture_paypal_payment'
72
73    @property
74    def target_url(self):
75        if not module_activated(
76            self.context.__parent__.__parent__.year, self.context):
77            return ''
78        if self.context.p_state in ('paid', 'waived', 'scholarship'):
79            return ''
80        return self.view.url(self.view.context, self.target)
81
82
83# Webservice request views
84
85
86class PaypalRequestPaymentStatusPageApplicant(UtilityView, grok.View):
87    """ Capture order created before
88    """
89    grok.context(INigeriaApplicantOnlinePayment)
90    grok.name('capture_paypal_payment')
91    grok.require('waeup.payApplicant')
92
93
94    def update(self):
95        if not module_activated(
96            self.context.__parent__.__parent__.year, self.context):
97            self.flash(_('Forbidden'), type='danger')
98            self.redirect(self.url(self.context, '@@index'))
99            return
100        if self.context.p_state in ('paid', 'waived', 'scholarship'):
101            self.flash(_('This ticket has already been paid.'), type='danger')
102            return
103        response = capture_order(self.context.r_pay_reference)
104        if response.get("status", None) == "COMPLETED":
105            self.context.p_state = u'paid'
106            self.context.payment_date = datetime.utcnow()
107        if not response:
108            self.flash(_('Paypal Order ID not found.'), type='danger')
109            return
110
111    def render(self):
112        self.redirect(self.url(self.context, '@@index'))
113        return
114
115
116# Forwarding pages
117
118class PaypalPageApplicant(KofaPage):
119    """ Create Paypal Order (and send user to paypal)
120    """
121    grok.context(INigeriaApplicantOnlinePayment)
122    grok.name('goto_paypal')
123    grok.require('waeup.payApplicant')
124    label = _('Pay via Paypal')
125    submit_button = _('Pay now')
126
127    # Nigerian Naira would be "NGN" but is not supported with
128    # most paypal accounts
129    currency_code = 'USD'
130
131    @property
132    def action(self):
133        return self.context.r_payment_link
134
135    def create_paypal_order(self, currency_code):
136        if self.context.r_pay_reference or self.context.p_state == 'paid':
137            return _("Payment ticket can't be re-sent to Paypal.")
138        if self.context.r_company and self.context.r_company != 'paypal':
139            return _("Payment ticket has been used for another payment gateway.")
140        now = datetime.utcnow()
141        if self.context.creation_date.tzinfo is not None:
142            # That's bad. Please store timezone-naive datetimes only!
143            now = self.context.creation_date.tzinfo.localize(now)
144        time_delta = now - self.context.creation_date
145        if time_delta.days > 7:
146            return _("This payment ticket is too old. Please create a new ticket.")
147        # The success url...
148        self.responseurl = self.url(self.context, 'capture_paypal_payment')
149        amount = Amount(value=self.context.amount_auth, currency_code=self.currency_code)
150        purchase_units = [
151                PurchaseUnit(description=self.context.p_id, amount=amount)
152                ]
153        order_id, payment_link, request_id, data = create_order(
154                purchase_units,
155                self.responseurl,         # success/authorized url
156                self.url(self.context),   # cancel/failed url
157                notify_url="https://h9.waeup.org/ipn"
158                )
159        if not order_id:  # an error happened...
160            err_msg = " ".join([x["description"] for x in data["details"]])
161            return _("Paypal order failed: %s") % err_msg
162        self.context.r_payment_link = payment_link
163        self.context.r_pay_reference = order_id
164        self.context.r_company = u'paypal'
165        self.customer = self.context.__parent__
166        self.customer.writeLogMessage(self, "Paypal Order Created: %s" % (
167            order_id))
168        return
169
170    def update(self):
171        if not module_activated(
172            self.context.__parent__.__parent__.year, self.context):
173            self.flash(_('Forbidden'), type='danger')
174            self.redirect(self.url(self.context, '@@index'))
175            return
176        self.paypal_error = u''
177        if not self.context.r_payment_link:
178            self.paypal_error = self.create_paypal_order(self.currency_code)
179        if self.paypal_error:
180            self.flash(error, type='danger')
181            self.redirect(self.url(self.context, '@@index'))
182            return
183        # Payment created - it must now be
184        # - authorized (by user)
185        # - captured (by us, afterwards)
186        if not self.context.r_company:
187            self.context.r_company = u'paypal'
188        return
189
190    def render(self):
191        if self.context.r_payment_link and self.context.p_state != 'paid':
192            self.redirect(self.context.r_payment_link, trusted=True)
193            return
194        self.redirect(self.url(self.context, '@@index'))
195
Note: See TracBrowser for help on using the repository browser.