Skip to content
Advertisement

How Do I Correctly Serialize and Send PayPal Transaction ID to Django Backend Doing Standard Client Side Integration

I’m trying to get PayPal’s transaction ID after the payment was approved on the client-side. I’m doing client-side integration of PayPal and Django. I can totally get the Payment ID and order ID and so forth, but these will be discarded by PayPal after the payment got approved. PayPal only recorded Transaction ID which can be used to track the payment with PayPal. When I’m trying to serialize the return actions which capture the Transaction ID – somehow I got a status code of 500 – Internal Server Error. The funny thing is that I can totally do console.log(transaction.id) and get the transaction ID in the console. Anyway, my error prone code is below:

In payment.html I got huge portion of html stuff, but I don’t post it here. I only post where the JavaScript begins:

JavaScript

In my order’s view I got this:

JavaScript

If I take out this in payment.html:

JavaScript

Which I will be left with:

JavaScript

This would totally work – and in my Payment model I could only record Payment ID and Order ID and so forth – but these are useless after payment went through with PayPal – because PayPal only retains the Transaction ID – and I can’t get Transaction ID to be sent to the backend – but I can only print to the console using console.log – and this is frustrating.

If I can get transaction ID to be sent to the backend using fetch – then I can do something like this:

JavaScript

But can this be done even though the first redirection already happened with this code:

JavaScript

So, is it then I need to fetch the redirect_url (such as payment_complete view) and not the previous url (such as payment view)? Basically, the JavaScript stuff really got me confused. Something is wrong with my code? Any help? Thanks…

Advertisement

Answer

An instructor of mine on Udemy solved this issue. The answer is to do this in the onApprove function using the code below:

JavaScript

Here is the completed working code for PayPal Client Side Integration with the ability to record PayPal Transaction ID into the database.

JavaScript

IN the payment view, you can always do something like this:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement