0

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?

currentIssue

Camilo Casadiego
  • 907
  • 3
  • 9
  • 33

1 Answers1

1

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.

Preston PHX
  • 27,642
  • 4
  • 24
  • 44
  • Im confused about this, checking this post https://stackoverflow.com/questions/17546953/cant-access-object-property-even-though-it-shows-up-in-a-console-log now I understand that there can be differences betwen the actual object and the console log, but why does it show the same value that im getting in the later response..? – Camilo Casadiego Feb 19 '21 at 19:39
  • Lol just read your update and although you clarify most of my doubts, still one thing I don't get, as you see on my sample, there im printing the code that its inside the object, and when debugging, there an actual call to the PayPal API during the function call, that returns this id, which is the same im getting on the confirm operation is called on my backend – Camilo Casadiego Feb 19 '21 at 19:43
  • 1
    The JavaScript Promise doesn't have a value when you immediately log it, as the request to create the order hasn't finished at that point. It does have a value later when you're inspecting it. – Preston PHX Feb 19 '21 at 20:32
  • 1
    If you really wanted to do something after a client-side create, it would need to be in a `.then()` -- but again, you should just be doing all this from a server – Preston PHX Feb 19 '21 at 20:35