PayPal Server, Package for making http requests in Django/Python - 26
Next step is to have a mechanism with which to make http requests since as I showed you I'm going to show you here a little bit of the implementation, this is what we're going to do in case you can also bring it from the repository if you have some problems but it's something like this here you can see that we're creating here a class x name we call PayPal payment to have it modular we're leaving it not here in settings but in views at least for now and it's in charge of doing all the operations initializing the parameters that we're going to require here we're also missing the base URL I'll talk about this later and here we generate the token and finally make the request to PayPal here several little things the first thing is that it's the nice thing about the integration with PayPal is that at least on the server we're not going to need a specific package for python or dango to communicate with PayPal we can simply install a package which is what we're going to do in a few moments to make http requests and the only thing we have to do is given the ID order we simply do it we append it here in a URL that we'll see and we make a request to PayPal:
import requests
from django.conf import settings
from django.http import JsonResponse
class PayPalPayment:
def __init__(self):
self.client_id = settings.PAYPAL_CLIENT_ID
self.secret = settings.PAYPAL_SECRET
self.base_url = settings.PAYPAL_BASE_URL
And automatically the charge would be made once all this is completed since again to carry out the order operation we have to have the token And we have to do that previously and once this is done we can already do the authorization of the order and with this make the charge but again the beauty of this is that it is simply an Api that exists we call it through http and little else we do not require again a specific package to do this communication it is simply making http requests and a little more the disadvantage that it brings us is that well we have to configure many little things like hiders Data and all that but x does not happen much so well to make the http requests we require a package and in this case we can use one called requests:
$ pip install requests
So it would be this to get to this page you can put here request PP or python so that you enter here here you can see that this package what it does is or allow to make http requests pass the authorization and everything else is to say something similar to what we can do with axios or fetch we can do it now from the server side which is exactly what we require based on what was explained above so nothing we are going to install it would be this one that we have here we go back here control c and paste we wait here 10,000 years and there we have it we lift to see that everything is correct by the way notice that at the beginning I had told you For example right now I have the virtual environment active but it does not place the parenthesis name of the virtual environment here but that is how it is active it is a trick that Visual Studio code has I do not know why it does it but since I created it through Visual Studio code it is configured that way Nothing happens if I activate it again but there it is already active the application continues to work correctly so with this we can move on to the next class