I’m working on the integration with PayPal using the Smart Payment Buttons, and I need to catch the trxId generated when the user presses the “Pay With PayPal”, when I print the info on the console, I can see the value I need, but when debugging or trying to get the value of the var, I’m getting undefined all the times, what I’m missing?
Below there’s an image that explains the issue
My only guess so far is that there’s some sort of security that prevents from getting this value?
Advertisement
Answer
There is no transaction at the time createOrder is called. The buyer hasn’t even signed in at that point, much less given their approval, much less there have been a successful capture.
A transaction is only created after a successful capture.
onApprove: function(data, actions) { return actions.order.capture().then(function(details) { console.log(details); //Transaction's ID will be within this object }); }
The above captures on the client side — but you should not capture on the client side and then send data to a server.
If you need to do anything important with the transaction ID (such as store it in a database), you ought to be using a server-side integration instead. For this create two routes, one for ‘Create Order’ and one for ‘Capture Order’, documented here. These routes should return JSON data.
Pair your two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server
Reading your question again, though, you mention an ID “at the time the button is clicked”, so perhaps you meant to ask about the order ID rather than the transaction ID. Well the most obvious and best way to know the order ID is to directly create it on your own server.