source: main/kofacustom.nigeria/branches/uli-paypal/src/kofacustom/nigeria/paypal/__init__.py @ 17929

Last change on this file since 17929 was 17239, checked in by uli, 23 months ago

Sketches of paypal :)

File size: 2.3 KB
Line 
1## $Id$
2##
3## Copyright (C) 2022 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# Classes that represent entitites needed when dealing with paypal.
19import datetime
20import grok
21from zope.component import queryUtility
22from kofacustom.nigeria.paypal.interfaces import IPaypalConfig
23
24
25def module_activated(session, payment):
26    if queryUtility(IPaypalConfig) is None:
27        return False
28    if getattr(payment, 'r_company', '') and payment.r_company != 'paypal':
29        return False
30    config = grok.getSite()['configuration']
31    session_conf = config.get(str(session)) or config.get(str(
32        datetime.datetime.now().year))
33    return getattr(session_conf, "paypal_enabled", False)
34
35
36class Amount(object):
37    """Paypal Amount
38
39    A structure that represents amounts of money.
40    """
41
42    def __init__(self, value="0.00", currency_code="USD"):
43        if type(value) in (int, float):
44            value = "%.2f" % round(value, 2)
45        self.value = value
46        self.currency_code = currency_code
47
48    def json(self):
49        return dict(value=self.value, currency_code=self.currency_code)
50
51
52class PurchaseUnit(object):
53    """Paypal purchase unit.
54
55    When creating paypal orders, the single things to be paid are passed
56    formatted as `purchase units`.
57    """
58
59    def __init__(self, description="", amount=Amount(), custom_id=None):
60        self.description = description
61        self.amount = amount
62        self.custom_id = custom_id
63
64    def json(self):
65        result = dict(
66            amount=self.amount.json(),
67            description=self.description,
68        )
69        if self.custom_id is not None:
70            result["custom_id"] = self.custom_id
71        return result
Note: See TracBrowser for help on using the repository browser.