Search code examples
javascriptpaypal-sandbox

paypal: How to get order id and pass to transaction details page


I am new to PayPal integration. I need to get the order Id for getting the details of the transaction and wants to display the transaction details to the customer on another page.

Following is the javascript code. The following code shows OrderId = 0.

     <script>
 paypal.Buttons({
     createOrder: function (data, actions) {
         // This function sets up the details of the transaction, including the amount and line item details.
         return actions.order.create({
            application_context: {
                return_url: 'https://example.com',
                cancel_url: 'https://example.com'
            },
             purchase_units: [{
                 amount: {
                     currency_code: 'USD',
                     value: '<%=bookingAmount%>'
                 }
             }]
         });
     },

    onApprove: function (data, actions) {
        // Capture the transaction funds
        return actions.order.capture().then(function () {
            // Show a confirmation to the buyer
            alert('Transaction complete!' & data.orderID);
        });
    }

}).render('#paypal-button-container');   //This function displays Smart Payment Buttons on your web page. 
    </script>

Question: The above code does not work as required. How to resolve this?


Solution

  • Using the following line of code, I was able to retrieve details of the transaction. -

    "return actions.order.capture().then(function (details)"

    Following is the complete running code -

    paypal.Buttons({
        createOrder: function (data, actions) {
            // This function sets up the details of the transaction, including the amount and line item details.
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        currency_code: 'USD',
                        value: '<%=bookingAmount%>'
                    }
                }]
            });
        },
        onApprove: function (data, actions) {
            // Capture the transaction funds
            return actions.order.capture().then(function (details) {
                console.log(JSON.stringify(details));
            });
        }
    }).render('#paypal-button-container');