paypal.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. authorization_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id='
  9. access_token_url = 'https://api.sandbox.paypal.com/v1/oauth2/token'
  10. #password:testing123
  11. #cafe.mui-buyer@gmail.com
  12. #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/
  13. class PaypalOauthClient(object):
  14. '''
  15. Python Client for Paypal API.
  16. '''
  17. access_token = None
  18. user_data = None
  19. token_type = None
  20. refresh_token = None
  21. def __init__(self, client_id, client_secret):
  22. '''
  23. Parameters:
  24. client_id: String
  25. - The client_id from registering application
  26. on Instagram.
  27. client_secret: String
  28. - The client_secret from registering application
  29. on Instagram.
  30. '''
  31. self.client_id = client_id
  32. self.client_secret = client_secret
  33. def get_authorize_url(self):
  34. '''
  35. Obtains authorize url link with given client_id.
  36. Returns:
  37. auth_url: String
  38. - The authorization url.
  39. '''
  40. auth_url = authorization_url + self.client_id + '&response_type=code&scope=openid&redirect_uri=http://localhost:8000/hackathon/paypal/'
  41. print auth_url
  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']
  89. return self.access_token
  90. def userinfo(self):
  91. link = 'https://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid'
  92. headers = { 'Content-Type':'application/json',
  93. 'Authorization': self.token_type + ' '+ self.access_token,
  94. 'Accept-Language': 'en_US',
  95. 'content-type': 'application/x-www-form-urlencoded'
  96. }
  97. req = requests.get(link, headers=headers)
  98. #print req.content