如何解决PayPal API集成中的AxiosError“connect ECONNREFUSED 127.0.0.1:XX”?

问题描述 投票:0回答:1

我正在尝试将 PayPal API 集成到我的 Node.js 应用程序中,但在使用 Axios 时遇到以下错误:

Axios错误:连接ECONNREFUSED 127.0.0.1:XX

解释和故障排除:

错误消息表明与

127.0.0.1:XX
(本地主机,端口 xx(xx = 任意数字))的连接被拒绝。这表明以下任一方面存在问题:

  • PayPal API 端点: PayPal 方面可能存在不正确的配置或更改。
  • 本地服务器: 服务器可能未运行,或者防火墙规则可能阻止它。

以下是错误:

节点:内部/流程/承诺:279 triggerUncaughtException(err, true /* fromPromise */); ^ AxiosError:连接 ECONNREFUSED 127.0.0.1:xx 错误:错误:连接 ECONNREFUSED 127.0.0.1:xx 在 TCPConnectWrap.afterConnect [作为未完成] (节点:net:1187:16)

代码:

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",
 
  },
 
}

我尝试过的:

  • 我已验证我的 PayPal API 凭据(客户端 ID 和密钥)。
  • 我尝试访问端点
    http://localhost:3000/create-order
    http://localhost:4000/create-order
    ,但都不起作用。
  • 我已经检查过,似乎没有服务器在端口 xx 上运行(xx = 任意数字)。

具体问题:

如何调试并修复此连接问题,以成功将 PayPal API 集成到我的 Node.js 应用程序中?

javascript node.js rest axios paypal
1个回答
3
投票

您将 paypal url 定义为

'${PAYPAL_API}/v2/checkout/orders'
,它将完全按原样使用,并且由于单引号,实际上不会替换
PAYPAL_API
变量。您应该使用反引号代替模板文字(在此处查看更多信息),如下所示:

axios.post(`${PAYPAL_API}/v2/checkout/orders`, ...
© www.soinside.com 2019 - 2024. All rights reserved.