PayPal API integration with Django
I am integrating PayPal API with Django back-end (Reactcjs for front-end) in my project with the help of sandbox accounts (@business & @personal account). What I did till now is I have a client_id and a secret used for generating access_token from paypal.
import requests
import base64
def PaypalToken(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, headers=headers)
return token
x = PaypalToken(my_client_ID, my_client_Secret)
print(x.text)
<code>
This gonna provide me a token then I use that token to post json object on paypal.
<pre>
headers = {"Content-Type": "application/json", "Authorization": 'Bearer' + token}
url = https://api.sandbox.paypal.com/v2/checkout/orders
data = '{
"intent": "CAPTURE",
"application_context": {
"return_url": "https://www.example.com",
"cancel_url": "https://www.example.com",
"brand_name": "EXAMPLE INC",
"landing_page": "BILLING",
"shipping_preference": "SET_PROVIDED_ADDRESS",
"user_action": "CONTINUE"
},
"purchase_units": [
{
"reference_id": "PUHF",
"description": "Sporting Goods",
"custom_id": "CUST-HighFashions",
"soft_descriptor": "HighFashions",
"amount": {
"currency_code": "USD",
"value": "220.00",
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": "180.00"
},
"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"
}
}
},
"items": [
{
"name": "T-Shirt",
"description": "Green XL",
"sku": "sku01",
"unit_amount": {
"currency_code": "USD",
"value": "90.00"
},
"tax": {
"currency_code": "USD",
"value": "10.00"
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
},
{
"name": "Shoes",
"description": "Running, Size 10.5",
"sku": "sku02",
"unit_amount": {
"currency_code": "USD",
"value": "45.00"
},
"tax": {
"currency_code": "USD",
"value": "5.00"
},
"quantity": "2",
"category": "PHYSICAL_GOODS"
}
],
"shipping": {
"method": "United States Postal Service",
"name": {`enter code here`
"full_name":"John Doe"
},
"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"
}
}
}
]
}'
result = requests.post(url, data, headers=header)
<code>
this gonna give me 4 links
<pre>
{
"id": "5O190127TN364715T",
"status": "CREATED",
"links": [
{
"href": "https://api.paypal.com/v2/checkout/orders/5O190127TN364715T",
"rel": "self",
"method": "GET"
},
{
"href": "https://www.paypal.com/checkoutnow?token=5O190127TN364715T",
"rel": "approve",
"method": "GET"
},
{
"href": "https://api.paypal.com/v2/checkout/orders/5O190127TN364715T/capture",
"rel": "capture",
"method": "POST"
}
]
}
<code>
What Next ?
First, implement two routes that return only JSON data. One for creating the order and one for capturing it after approval.
For the interim payer approval step, ignore those 4 links which are for old integration patterns that redirect away from your site. In place of that, use the PayPal JS SDK for in-context approval (keeping your site loaded in the background) and have it fetch from those two routes on your server.
Here's the approval flow in pure JS: https://developer.paypal.com/demo/checkout/#/pattern/server
For the same with react, see @paypal/react-papal-js and its storybook examples.