paypal.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. return auth_url
  42. def get_access_token(self, code):
  43. '''
  44. Obtains access token from authorization code.
  45. Parameters:
  46. code: String
  47. - The code is retrieved from the authorization url parameter
  48. to obtain access_token.
  49. '''
  50. headers = {
  51. 'Accept': 'application/json',
  52. 'Accept-Language': 'en_US',
  53. 'content-type': 'application/x-www-form-urlencoded'
  54. }
  55. data = { 'grant_type': 'authorization_code',
  56. 'code': code,
  57. 'redirect_uri':'http://localhost:8000/hackathon/paypal'}
  58. req = requests.post(access_token_url, data=data, headers=headers, auth=(self.client_id, self.client_secret))
  59. if req.status_code != 200:
  60. raise Exception("Invalid response %s." % req.status_code)
  61. content = unicodedata.normalize('NFKD', req.text).encode('ascii','ignore')
  62. jsonlist = json2.loads(content)
  63. #print jsonlist
  64. self.access_token = jsonlist['access_token']
  65. self.token_type = jsonlist['token_type']
  66. self.refresh_token = jsonlist['refresh_token']
  67. return self.refresh_token
  68. def get_refresh_token(self, refresh_token):
  69. '''
  70. This methods obtain new access token when current access_token expires.
  71. '''
  72. headers = {
  73. 'Accept': 'application/json',
  74. 'Accept-Language': 'en_US',
  75. 'content-type': 'application/x-www-form-urlencoded'
  76. }
  77. data = { 'grant_type': 'refresh_token',
  78. 'refresh_token': refresh_token}
  79. req = requests.post(access_token_url, data=data, headers=headers, auth=(self.client_id, self.client_secret))
  80. if req.status_code != 200:
  81. raise Exception("Invalid response %s." % req.status_code)
  82. content = unicodedata.normalize('NFKD', req.text).encode('ascii','ignore')
  83. jsonlist = json2.loads(content)
  84. print jsonlist
  85. self.access_token = jsonlist['access_token']
  86. self.token_type = jsonlist['token_type']
  87. return self.access_token
  88. def test(self):
  89. #link ='https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/identity/openidconnect/userinfo' #/?schema=openid'
  90. link = 'https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice'
  91. header = { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type':'application/json'}
  92. body = { 'schema':'openid', 'access_token': self.access_token}
  93. #req = requests.post(link, headers={'Content-Type':'application/json', 'Authorization': self.token_type+' '+self.access_token})
  94. req = requests.post(link, headers=header, data=body)
  95. if req.status_code != 200:
  96. raise Exception("Invalid response %s." % req.status_code)
  97. content = unicodedata.normalize('NFKD', req.text).encode('ascii','ignore')
  98. print content