PayPal SDK JavaScript Client: onApprove, approve order - 24 #Django

We're going to finish here or at least get ahead of the implementation as much as possible here and the next functionality that we're going to implement would be onapprove:

***
<script>
    paypal.Buttons(
        {
           ***
            onApprove: function(data, actions){
                console.log(data);
            }
        }
    ).render('#buttonPayPal');
</script>

Of course, once the amount is configured, it receives the same parameters in this case as if we were using the order, since we do not want to create anything with an action here, since it was simply approved with the order ID, we have to pass it to the server in any case through an axios through a fetch through a redirection, but we pass that blessed ID to finally be able to collect:

onApprove: function(data, actions){
   console.log(data);
   window.location.href = '/store/capture-payment/' + data.orderID;
}

For the purchase to be authorized, we have to do it on the server, and finally, approve the purchase here and, so to speak, close the loop. Then, give the product to the customer, or whatever. But we've reached this point. Here you can see a little bit of the meaning of all this, since, obviously, this can't be done on the server. Again, this can't be done on the server, since it's a client-side operation. The customer has to come here, enter their credentials—in this case, not our application, but PayPal, through PayPal. This is all PayPal—and be able to make the request to purchase the product here.

But, obviously, for obvious reasons, we don't do this on the server. That is, this request. Because, if it could be done on the server, it means you could do anything without the customer's authorization, which wouldn't be correct. So, on the server, it makes perfect sense to finish processing this, using the Secret ID that, remember, we configured earlier, and finally complete the purchase.

But hey, there are still a few things missing to get there. I'll let you know, and we'll leave it at that. Obviously, at this point, again, you still don't have the deposit; you don't have the money in your PayPal account until it's authorized on the server.

I agree to receive announcements of interest about this Blog.

We are going to implement the function to approve the order on the client.

- 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.