Search code examples
javascriptnode.jsrestaxiospaypal

How to resolve AxiosError "connect ECONNREFUSED 127.0.0.1:XX" in PayPal API integration?


I'm trying to integrate the PayPal API into my Node.js application, but I'm encountering the following error when using Axios:

AxiosError: connect ECONNREFUSED 127.0.0.1:XX

Explanation and Troubleshooting:

The error message indicates that the connection to 127.0.0.1:XX (localhost, port xx(xx = any number)) is being refused. This suggests a problem with either:

  • PayPal API endpoint: Possible incorrect configuration or a change on PayPal's side.
  • Local server: A server might not be running, or firewall rules might be blocking it.

These are the errors:

node:internal/process/promises:279 triggerUncaughtException(err, true /* fromPromise */); ^ AxiosError: connect ECONNREFUSED 127.0.0.1:xx errored: Error: connect ECONNREFUSED 127.0.0.1:xx at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)

Code:

payment.controllers.js

import axios from "axios";
import {PAYPAL_API, PAYPAL_API_CLIENT, PAYPAL_API_SECRET} from '../config'

export const createOrder = async (req, res) => {
    const order = {
        intent : 'CAPTURE',
        purchase_units: [
            {
                amount: {
                    currency_code:"USD",
                    value: '2'
                },
                description: "suscription"
            },
        ],
        application_context: {
            brand_name: "pomodoro.app",
            landing_page: "LOGIN",
            user_action: "PAY_NOW",
            return_url: 'http://localhost:4000/capture-order',
            cancel_url: 'http://localhost:4000/cancel-order'
        }
    };

    const response = await axios.post('${PAYPAL_API}/v2/checkout/orders', order, {
        auth: {
            username: PAYPAL_API_CLIENT,
            password: PAYPAL_API_SECRET
        }
    });

    console.log(response)

    res.send('creating order');
}

export const captureOrder = (req, res) => {
    res.send('capture an order')
}

export const cancelOrder = (req, res) => {
    res.send('cancel an order')
}

package.json

{
  
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.27.2",
 
  },
 
}

What I've Tried:

  • I've verified my PayPal API credentials (Client ID and Secret).
  • I've tried accessing the endpoints http://localhost:3000/create-order and http://localhost:4000/create-order, but neither is working.
  • I've checked, and there doesn't seem to be a server running on port xx (xx = any number).

Specific Question:

How can I debug and fix this connection issue to successfully integrate the PayPal API into my Node.js application?


Solution

  • You're defining the paypal url as '${PAYPAL_API}/v2/checkout/orders', which will be used exactly as-is and won't actually replace the PAYPAL_API variable due to the single quotes. You should use back ticks instead as template literals (see more info here) like this:

    axios.post(`${PAYPAL_API}/v2/checkout/orders`, ...