Преглед на файлове

add script for paypal with get_authorize_url, get_access_token

mk200789 преди 10 години
родител
ревизия
ec67e8b385
променени са 2 файла, в които са добавени 104 реда и са изтрити 0 реда
  1. 98 0
      hackathon_starter/hackathon/scripts/paypal.py
  2. 6 0
      hackathon_starter/hackathon/views.py

+ 98 - 0
hackathon_starter/hackathon/scripts/paypal.py

@@ -0,0 +1,98 @@
+
+'''
+paypal.py contains a handful of methods for interacting
+with Paypal data and returning the responses as JSON.
+'''
+
+import requests
+import urllib
+import urllib2
+import json
+import simplejson as json2
+import googlemaps
+from django.conf import settings
+from datetime import datetime
+from time import strftime
+import unicodedata
+
+authorization_url = 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id='
+access_token_url  = 'https://api.sandbox.paypal.com/v1/oauth2/token'
+
+#password:testing123
+#cafe.mui-buyer@gmail.com
+#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/
+
+
+class PaypalOauthClient(object):
+	'''
+	Python Client for Paypal API.
+	'''
+
+	access_token = None
+	user_data = None
+
+	def __init__(self, client_id, client_secret):
+		'''
+		Parameters:
+			client_id: String
+				- The client_id from registering application
+				  on Instagram.
+			client_secret: String
+				- The client_secret from registering application
+				  on Instagram.
+		'''
+
+		self.client_id 		= client_id
+		self.client_secret 	= client_secret
+
+
+	def get_authorize_url(self):
+		''' 
+		Obtains authorize url link with given client_id.
+
+		Returns:
+			auth_url: String
+				- The authorization url.
+		'''
+
+		auth_url = authorization_url + self.client_id + '&response_type=code&scope=openid&redirect_uri=http://localhost:8000/hackathon/paypal/'
+		return auth_url
+
+
+	def get_access_token(self):
+		''' 
+		Obtains access token.
+
+		Parameters:
+			code: String
+				- The code is retrieved from the authorization url parameter
+				  to obtain access_token.
+		'''
+
+		headers = {
+					'Accept': 'application/json',
+					'Accept-Language': 'en_US',
+					'content-type': 'application/x-www-form-urlencoded'
+				  }
+		data = { 'grant_type': 'client_credentials'}
+
+
+		req = requests.post(access_token_url, data=data, headers= headers, auth=(self.client_id, self.client_secret))
+		
+		if req.status_code != 200:
+			raise Exception("Invalid response %s." %  req.status_code)
+		
+		content = unicodedata.normalize('NFKD', req.text).encode('ascii','ignore')
+		jsonlist = json2.loads(content)
+
+
+		self.access_token = jsonlist['access_token']
+
+
+	def test(self):
+		link ='https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/identity/openidconnect/userinfo/?schema=openid'
+		req = requests.get(link, headers={'Content-Type':'application/json', 'Authorization': self.access_token})
+		print req
+
+
+

+ 6 - 0
hackathon_starter/hackathon/views.py

@@ -19,6 +19,7 @@ from scripts.twilioapi import *
 from scripts.instagram import InstagramOauthClient
 from scripts.scraper import steamDiscounts
 from scripts.quandl import *
+from scripts.paypal import PaypalOauthClient
 
 # Python
 import oauth2 as oauth
@@ -33,6 +34,7 @@ from hackathon.forms import UserForm
 
 getTumblr = TumblrOauthClient(settings.TUMBLR_CONSUMER_KEY, settings.TUMBLR_CONSUMER_SECRET)
 getInstagram = InstagramOauthClient(settings.INSTAGRAM_CLIENT_ID, settings.INSTAGRAM_CLIENT_SECRET)
+getPaypal = PaypalOauthClient(settings.PAYPAL_CLIENT_ID, settings.PAYPAL_CLIENT_SECRET)
 
 def index(request):
     context = {'hello': 'world'}
@@ -251,6 +253,10 @@ def instagramMediaByLocation(request):
 #    PAYPAL API    #
 ####################
 def paypal(request):
+    #code = request.GET['code']
+    getPaypal.get_access_token()
+    getPaypal.test()
+
     context = {'title':'paypal'}
     return render(request, 'hackathon/paypal.html', context)