paypal.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. '''
  2. paypal.py contains a handful of methods for interacting
  3. with Paypal data and returning the responses as JSON.
  4. '''
  5. import requests
  6. import urllib
  7. import urllib2
  8. import json
  9. import simplejson as json2
  10. import googlemaps
  11. from django.conf import settings
  12. from datetime import datetime
  13. from time import strftime
  14. import unicodedata
  15. authorization_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id='
  16. access_token_url = 'https://api.sandbox.paypal.com/v1/oauth2/token'
  17. #password:testing123
  18. #cafe.mui-buyer@gmail.com
  19. #https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=AcOIP0f8NTl3iv5Etw2nakNrgPmjE65v84a2NQD5mm8-z-dTyhMSNHZuxHbHUaTAxLQIE0u-A2DFEY8M&response_type=code&scope=openid&redirect_uri=http://localhost:8000/hackathon/paypal/
  20. class PaypalOauthClient(object):
  21. '''
  22. Python Client for Paypal API.
  23. '''
  24. access_token = None
  25. user_data = None
  26. def __init__(self, client_id, client_secret):
  27. '''
  28. Parameters:
  29. client_id: String
  30. - The client_id from registering application
  31. on Instagram.
  32. client_secret: String
  33. - The client_secret from registering application
  34. on Instagram.
  35. '''
  36. self.client_id = client_id
  37. self.client_secret = client_secret
  38. def get_authorize_url(self):
  39. '''
  40. Obtains authorize url link with given client_id.
  41. Returns:
  42. auth_url: String
  43. - The authorization url.
  44. '''
  45. auth_url = authorization_url + self.client_id + '&response_type=code&scope=openid&redirect_uri=http://localhost:8000/hackathon/paypal/'
  46. return auth_url
  47. def get_access_token(self):
  48. '''
  49. Obtains access token.
  50. Parameters:
  51. code: String
  52. - The code is retrieved from the authorization url parameter
  53. to obtain access_token.
  54. '''
  55. headers = {
  56. 'Accept': 'application/json',
  57. 'Accept-Language': 'en_US',
  58. 'content-type': 'application/x-www-form-urlencoded'
  59. }
  60. data = { 'grant_type': 'client_credentials'}
  61. req = requests.post(access_token_url, data=data, headers= headers, auth=(self.client_id, self.client_secret))
  62. if req.status_code != 200:
  63. raise Exception("Invalid response %s." % req.status_code)
  64. content = unicodedata.normalize('NFKD', req.text).encode('ascii','ignore')
  65. jsonlist = json2.loads(content)
  66. self.access_token = jsonlist['access_token']
  67. def test(self):
  68. link ='https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/identity/openidconnect/userinfo/?schema=openid'
  69. req = requests.get(link, headers={'Content-Type':'application/json', 'Authorization': self.access_token})
  70. print req