-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.py
37 lines (29 loc) · 1.06 KB
/
gateway.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import requests
import time
import urllib
import urlparse
from lib.paypal import exceptions
def post(url, params):
"""
Make a POST request to the URL using the key-value pairs. Return
a set of key-value pairs.
:url: URL to post to
:params: Dict of parameters to include in post payload
"""
for k in params.keys():
if type(params[k]) == unicode:
params[k] = params[k].encode('utf-8')
payload = urllib.urlencode(params.items())
start_time = time.time()
response = requests.post(url, payload)
if response.status_code != requests.codes.ok:
raise exceptions.PayPalError("Unable to communicate with PayPal")
# Convert response into a simple key-value format
pairs = {}
for key, values in urlparse.parse_qs(response.content).items():
pairs[key] = values[0]
# Add audit information
pairs['_raw_request'] = payload
pairs['_raw_response'] = response.content
pairs['_response_time'] = (time.time() - start_time) * 1000.0
return pairs