source: main/waeup.ikoba/trunk/src/waeup/ikoba/payments/paypal.py @ 12211

Last change on this file since 12211 was 12060, checked in by uli, 10 years ago

Merge changes from uli-payments back into trunk.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1## $Id: paypal.py 12060 2014-11-25 18:44:01Z 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##
18"""Support for PayPal payments.
19"""
20import ConfigParser
21import paypalrestsdk
22from zope.component import queryUtility
23from waeup.ikoba.interfaces import IPayPalConfig
24
25
26def parse_paypal_config(path):
27    """Get paypal credentials from config file.
28
29    Returns a dict with following keys set:
30
31      ``client_id``, ``client_secret``, ``mode``.
32
33    Please note, that values might be `None`.
34    """
35    parser = ConfigParser.SafeConfigParser(
36        {'id': None, 'secret': None, 'mode': 'sandbox', }
37        )
38    parser.readfp(open(path))
39    return {
40        'client_id': parser.get('rest-client', 'id', None),
41        'client_secret': parser.get('rest-client', 'secret', None),
42        'mode': parser.get('rest-client', 'mode', 'sandbox')
43        }
44
45
46def get_paypal_config_file_path():
47    """Get the path of the configuration file (if any).
48
49    If no such file is registered, we get `None`.
50    """
51    util = queryUtility(IPayPalConfig)
52    if util is None:
53        return None
54    return util['path']
55
56
57def configure_sdk(path):
58    """Configure the PayPal SDK with values from config in `path`.
59
60    Parse paypal configuration from file in `path` and set
61    configuration in SDK accordingly.
62
63    Returns a dict with values from config file and path set.
64
65    This function will normally be called during start-up and only
66    this one time.
67
68    It is neccessary to authorize later calls to other part of the
69    PayPal API.
70    """
71    conf = parse_paypal_config(path)
72    paypalrestsdk.configure({
73        'mode': conf['mode'],
74        'client_id': conf['client_id'],
75        'client_secret': conf['client_secret'],
76        })
77    conf['path'] = path
78    return conf
79
80
81def get_access_token():
82    """Get an access token for further calls.
83    """
84    conf = parse_paypal_config(get_paypal_config_file_path())
85    api = paypalrestsdk.set_config(
86        mode=conf['mode'],  # sandbox or live
87        client_id=conf['client_id'],
88        client_secret=conf['client_secret'],
89        )
90    return api.get_access_token()
91
92
93def get_payment():
94    """Construct a payment.
95
96    You have to `create()` the payment yourself.
97
98    XXX: Just some sampledata yet.
99    """
100    payment = paypalrestsdk.Payment(
101        {
102            "intent": "sale",
103            "payer": {
104                "payment_method": "credit_card",
105                "funding_instruments": [
106                    {
107                        "credit_card": {
108                            "type": "visa",
109                            "number": "4417119669820331",
110                            "expire_month": "11",
111                            "expire_year": "2018",
112                            "cvv2": "874",
113                            "first_name": "Joe",
114                            "last_name": "Shopper",
115                            "billing_address": {
116                                "line1": "52 N Main ST",
117                                "city": "Johnstown",
118                                "state": "OH",
119                                "postal_code": "43210",
120                                "country_code": "US"}
121                            }
122                        }
123                    ]},
124            "transactions": [{
125                    "amount": {
126                        "total": "7.47",
127                        "currency": "USD",
128                        "details": {
129                            "subtotal": "7.41",
130                            "tax": "0.03",
131                            "shipping": "0.03"}},
132                    "description": ("This is the payment "
133                                    "transaction description.")
134                    }]
135            }
136        )
137    return payment
Note: See TracBrowser for help on using the repository browser.