source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments/paypal.py @ 12027

Last change on this file since 12027 was 12026, checked in by uli, 10 years ago

Set keywords properties for Id.

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1## $Id: paypal.py 12026 2014-11-21 10:01:04Z uli $
2##
3## Copyright (C) 2014 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 ConfigParser
19import paypalrestsdk
20from zope.component import queryUtility
21from waeup.ikoba.interfaces import IPayPalConfig
22
23
24def parse_paypal_config(path):
25    """Get paypal credentials from config file.
26
27    Returns a dict with following keys set:
28
29      ``client_id``, ``client_secret``, ``mode``.
30
31    Please note, that values might be `None`.
32    """
33    parser = ConfigParser.SafeConfigParser(
34        {'id': None, 'secret': None, 'mode': 'sandbox', }
35        )
36    parser.readfp(open(path))
37    return {
38        'client_id': parser.get('rest-client', 'id', None),
39        'client_secret': parser.get('rest-client', 'secret', None),
40        'mode': parser.get('rest-client', 'mode', 'sandbox')
41        }
42
43
44def get_paypal_config_file_path():
45    """Get the path of the configuration file (if any).
46
47    If no such file is registered, we get `None`.
48    """
49    util = queryUtility(IPayPalConfig)
50    if util is None:
51        return None
52    return util['path']
53
54
55def get_access_token():
56    """Get an access token for further calls.
57    """
58    conf = parse_paypal_config(get_paypal_config_file_path())
59    api = paypalrestsdk.set_config(
60        mode=conf['mode'],  # sandbox or live
61        client_id=conf['client_id'],
62        client_secret=conf['client_secret'],
63        )
64    return api.get_access_token()
Note: See TracBrowser for help on using the repository browser.