paypal.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 simplejson as json2
  7. import unicodedata
  8. import urllib
  9. authorization_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id='
  10. access_token_url = 'https://api.sandbox.paypal.com/v1/oauth2/token'
  11. #password:testing123
  12. #cafe.mui-buyer@gmail.com
  13. #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/
  14. class PaypalOauthClient(object):
  15. '''
  16. Python Client for Paypal API.
  17. '''
  18. access_token = None
  19. user_data = None
  20. token_type = None
  21. refresh_token = None
  22. def __init__(self, client_id, client_secret):
  23. '''
  24. Parameters:
  25. client_id: String
  26. - The client_id from registering application
  27. on Instagram.
  28. client_secret: String
  29. - The client_secret from registering application
  30. on Instagram.
  31. '''
  32. self.client_id = client_id
  33. self.client_secret = client_secret
  34. def get_authorize_url(self):
  35. '''
  36. Obtains authorize url link with given client_id.
  37. Returns:
  38. auth_url: String
  39. - The authorization url.
  40. '''
  41. auth_url = authorization_url + self.client_id + '&response_type=code&scope=openid&redirect_uri=http://localhost:8000/hackathon/paypal/'
  42. return auth_url
  43. def get_access_token(self, code):
  44. '''
  45. Obtains access token from authorization code.
  46. Parameters:
  47. code: String
  48. - The code is retrieved from the authorization url parameter
  49. to obtain access_token.
  50. '''
  51. headers = {
  52. 'Accept': 'application/json',
  53. 'Accept-Language': 'en_US',
  54. 'content-type': 'application/x-www-form-urlencoded'
  55. }
  56. data = { 'grant_type': 'authorization_code',
  57. 'code': code,
  58. 'redirect_uri':'http://localhost:8000/hackathon/paypal'}
  59. req = requests.post(access_token_url, data=data, headers=headers, auth=(self.client_id, self.client_secret))
  60. if req.status_code != 200:
  61. raise Exception("Invalid response %s." % req.status_code)
  62. content = unicodedata.normalize('NFKD', req.text).encode('ascii','ignore')
  63. jsonlist = json2.loads(content)
  64. #print jsonlist
  65. self.access_token = jsonlist['access_token']
  66. self.token_type = jsonlist['token_type']
  67. self.refresh_token = jsonlist['refresh_token']
  68. return self.refresh_token
  69. def get_refresh_token(self, refresh_token):
  70. '''
  71. This methods obtain new access token when current access_token expires.
  72. '''
  73. link = 'https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice'
  74. headers = {
  75. 'Accept': 'application/json',
  76. 'Accept-Language': 'en_US',
  77. 'content-type': 'application/x-www-form-urlencoded'
  78. }
  79. data = { 'grant_type': 'refresh_token',
  80. 'refresh_token': refresh_token}
  81. req = requests.post(link, data=data, headers=headers, auth=(self.client_id, self.client_secret))
  82. if req.status_code != 200:
  83. raise Exception("Invalid response %s." % req.status_code)
  84. content = unicodedata.normalize('NFKD', req.text).encode('ascii','ignore')
  85. jsonlist = json2.loads(content)
  86. #print jsonlist
  87. self.access_token = jsonlist['access_token']
  88. self.token_type = jsonlist['token_type']