Skip to content

Instantly share code, notes, and snippets.

@GaborVxxx
Forked from paulwababu/paypal.py
Created December 20, 2022 19:41
Show Gist options
  • Save GaborVxxx/21505e21afa960d7868bd0c6174b20b5 to your computer and use it in GitHub Desktop.
Save GaborVxxx/21505e21afa960d7868bd0c6174b20b5 to your computer and use it in GitHub Desktop.
class PayPalView(APIView):
#permission_classes = [HasAPIKey]
def PaypalToken(self, client_id, client_secret):
url = 'https://api.sandbox.paypal.com/v1/oauth2/token'
data = {
"client_id": client_id,
"client_secret": client_secret,
"grant_type": "client_credentials"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic {0}".format(base64.b64encode((client_id + ":" + client_secret).encode()).decode())
}
token = requests.post(url, data=data, headers=headers)
return token
def get(self, request):
client_id = request.data.get('client_id')#MF7R-SMUVvx-7G7mS'
client_secret = request.data.get('client_secret')#'LQsmQoNDXsSv0u'
token = self.PaypalToken(client_id, client_secret)
return Response(token.json())
class CreateOrderView(APIView):
#permission_classes = [HasAPIKey]
def get(self, request):
token = request.data.get('token')
headers = {"Content-Type": "application/json", "Authorization": "Bearer "+token}
url = 'https://api-m.sandbox.paypal.com/v2/checkout/orders'
json_data = {
"intent": "CAPTURE",
"application_context": {
"notify_url": "https:..",
"return_url": "https:..",#change to your domain
"cancel_url": "https:..", #change to your domain
"brand_name": "SANDBOX",
"landing_page": "BILLING",
"shipping_preference": "SET_PROVIDED_ADDRESS",
"user_action": "CONTINUE"
},
"purchase_units": [
{
"reference_id": "294375635",
"description": "Sporting Goods",
"custom_id": "CUST-HighFashions",
"soft_descriptor": "HighFashions",
"amount": {
"currency_code": "USD",
"value": "220",
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": "180"
},
"shipping": {
"currency_code": "USD",
"value": "20.00"
},
"handling": {
"currency_code": "USD",
"value": "10.00"
},
"tax_total": {
"currency_code": "USD",
"value": "20.00"
},
"shipping_discount": {
"currency_code": "USD",
"value": "10"
}
}
},
"shipping": {
"method": "United States Postal Service",
"name": {
"full_name":"Pesapedia"
},
"address": {
"address_line_1": "123 Townsend St",
"address_line_2": "Floor 6",
"admin_area_2": "San Francisco",
"admin_area_1": "CA",
"postal_code": "94107",
"country_code": "US"
}
}
}
]
}
response = requests.post(url, headers=headers, json=json_data)
return Response(response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment