PayPal Implementation in Django Rest Framework |PayPal |Django Rest Framework

Rahul R
3 min readFeb 19, 2020

Hi my name is Rahul R and I’m a Trivandrum based Python Developer.
In this write-up, I’m discussing how we can implement PayPal payment/checkout using the Django Rest Framework. Sounds good..?. I think there is not so much documentation available about PayPal implementation in Google, So I think this write up will be useful to someone who tried to implement PayPal checkout.

Without wasting time let’s discuss how can we implement PayPal in Django Rest Framework.

Step 1: Creating a Sandbox Account

First of all, we need a PayPal Sandbox Account, to create a sandbox account visit PayPal Developer website and click Login or Signup if you do not already have an account set up. You can log in with your existing PayPal account.

After signing in to your account click dashboard and select “Create Account”, after that we can create a business and personal PayPal sandbox accounts.

Dashboard of PayPal Sandbox account

Personal Sandbox account is to pay money through PayPal and the business account used for receiving money (which is a merchant account). Select a merchant account this will open a popup window. Click on the API Credentials tab. You’ll see two API keys for the Test.

Copy those API Credentials which you will have a Client_ID and a Client_Secret.

Note: Remember money shown in sandbox is a dummy value

Step 2: Integrate in Rest Framework

Open up the Django Rest API Framework that you want to implement PayPal checkout.

In your urls.py add this route

re_path(r’^paypal/$’, paypal.views.Payment.as_view(), name=’PayPal Payment API’)

This API Route will call the Payment viewset class in app named PayPal.

In views.py we can create a new viewset class named Payment.
to proceed with the payment we have to call 2 URLs

  1. https://api.sandbox.paypal.com/v1/oauth2/token : This link is used to get the token from PayPal, the token which is used to create order request in PayPal.
  2. https://api.sandbox.paypal.com/v2/checkout/orders : This link is used to create an order in which we want to make payment.

In the viewset class, let's create a function which we want to generate the authorization token.

From this function, we can get the token from PayPal and using this token we will call the 2nd URL using the POST method by passing the product details and price of the product including the currency code (we can add more fields to the data as our need).

We can add return_url and cancel_url into the data, when we cancel the payment it would be redirected to the cancel_url and if the payment is successful it would be redirected to return_url.

as a result, we will get a response to a set of URLs. The output would look like as follows:

{ 
“href”: “https://www.paypal.com/checkoutnowtoken=5O190127TN364715T",
“rel”: “approve”,
“method”: “GET”
},

This is the link that initiates a payment request in PayPal. When visiting this link it would be redirected to your PayPal account and request you to log in. (Remember: Login using personal sandbox account to pay money). After logging in, the amount which we created for the product would be shown and we can proceed with the payment. If the payment is completed it would be redirected to return_url, if the payment canceled it would redirected to cancel_url.

Thank you for your patience in reading this article..!!

Wishing you a bug-free coding.

--

--