PayPal Server, Create PayPal class to charge PayPal using Django/PayPal - 27

The next step we are going to take would be to create the PayPal class that I showed you in the previous class, the implementation for that, here we are going to need to complete the rest of the configurations that would be the secret key, we already have the public one.

Custom configurations in Django

Here we need to enter the secret key. Go to developer.paypal.com and copy your secret key and enter it here. I hide it in the configuration file:

mystore\mystore\settings.py

PAYPAL_CLIENT_ID = '<YOURCLIENTID>'
PAYPAL_SECRET =  '<YOURSECRETID>'
PAYPAL_BASE_URL = 'https://api-m.sandbox.paypal.com' #'https://api-m.paypal.com'

Throughout the course we are going to work in a sandbox environment, that is, a development environment, yes, a test development environment, that would be the production environment. When you want to move it to production, there you have it. Later on, we will make some more configurations here to make it a little more automatic, but at least for now, we are going to leave it like this with fixed values.

PayPal class to handle payments

We are going to create that class elsewhere in another file but at least for now for ease I prefer to do it in the same views file as the PayPal class we are simply going to create it:

mystore\elements\views.py

from django.conf import settings
***
def detail(request, pk):

   element = Element.objects.get(id=pk)
   return render(request,'elements/detail.html',
                 {'element': element, 'paypal_client_id': 
                  settings.PAYPAL_CLIENT_ID})

In this way, we can separate the logic between a payment platform and any other that you want to implement, and the logic of a payment gateway is stopped as much as possible, in addition to being a modular, reusable and scalable structure.

I agree to receive announcements of interest about this Blog.

We created the PayPal class to handle payments.

- Andrés Cruz

En español

This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y Libro desarrollo web con Django 5 y Python 3 + integración con Vue 3, Bootstrap y Alpine.js.

) )