1 | ## $Id$ |
---|
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 | ## |
---|
18 | import ConfigParser |
---|
19 | import paypalrestsdk |
---|
20 | from zope.component import queryUtility |
---|
21 | from waeup.ikoba.interfaces import IPayPalConfig |
---|
22 | |
---|
23 | |
---|
24 | def parse_paypal_config(path): |
---|
25 | """Get paypal credentials from config file. |
---|
26 | """ |
---|
27 | parser = ConfigParser.SafeConfigParser( |
---|
28 | {'id': None, 'secret': None, 'mode': 'sandbox', } |
---|
29 | ) |
---|
30 | parser.readfp(open(path)) |
---|
31 | return { |
---|
32 | 'client_id': parser.get('rest-client', 'id', None), |
---|
33 | 'client_secret': parser.get('rest-client', 'secret', None), |
---|
34 | 'mode': parser.get('rest-client', 'mode', 'sandbox') |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | def get_paypal_config_file_path(): |
---|
39 | """Get the path of the configuration file (if any). |
---|
40 | |
---|
41 | If no such file is registered, we get `None`. |
---|
42 | """ |
---|
43 | util = queryUtility(IPayPalConfig) |
---|
44 | if util is None: |
---|
45 | return None |
---|
46 | return util['path'] |
---|
47 | |
---|
48 | |
---|
49 | def get_access_token(): |
---|
50 | """Get an access token for further calls. |
---|
51 | """ |
---|
52 | conf = parse_paypal_config(get_paypal_config_file_path()) |
---|
53 | api = paypalrestsdk.set_config( |
---|
54 | mode=conf['mode'], # sandbox or live |
---|
55 | client_id=conf['client_id'], |
---|
56 | client_secret=conf['client_secret'], |
---|
57 | ) |
---|
58 | return api.get_access_token() |
---|