pomoworko.com
pomoworko.com

Reputation: 1016

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:

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:

Specific Question:

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

Upvotes: 0

Views: 7748

Answers (1)

rvanlaarhoven
rvanlaarhoven

Reputation: 865

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`, ...

Upvotes: 3

Related Questions