最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 onplete] (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?

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 onplete] (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?

Share Improve this question edited Mar 18, 2024 at 18:53 pomoworko. asked Jul 19, 2022 at 22:28 pomoworko.pomoworko. 1,0062 gold badges15 silver badges43 bronze badges 4
  • 1 First; It's probably not so smart to expose the api keys on Stackoverflow even though its for a sandbox environment. And second; Can you include the routes/payment.routes file? – rvanlaarhoven Commented Jul 19, 2022 at 22:35
  • Hello, I deleted my API keys thanks for warning me and I included routes/payment.routes – pomoworko. Commented Jul 19, 2022 at 22:40
  • 1 The username & password are still visible in the logs :) – rvanlaarhoven Commented Jul 19, 2022 at 22:51
  • I think now I deleted all the evidence :) (I think) – pomoworko. Commented Jul 19, 2022 at 22:58
Add a ment  | 

1 Answer 1

Reset to default 3

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论