我正在尝试将 PayPal API 集成到我的 Node.js 应用程序中,但在使用 Axios 时遇到以下错误:
Axios错误:连接ECONNREFUSED 127.0.0.1:XX
解释和故障排除:
错误消息表明与
127.0.0.1:XX
(本地主机,端口 xx(xx = 任意数字))的连接被拒绝。这表明以下任一方面存在问题:
以下是错误:
节点:内部/流程/承诺: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",
},
}
我尝试过的:
http://localhost:3000/create-order
和 http://localhost:4000/create-order
,但都不起作用。具体问题:
如何调试并修复此连接问题,以成功将 PayPal API 集成到我的 Node.js 应用程序中?
您将 paypal url 定义为
'${PAYPAL_API}/v2/checkout/orders'
,它将完全按原样使用,并且由于单引号,实际上不会替换 PAYPAL_API
变量。您应该使用反引号代替模板文字(在此处查看更多信息),如下所示:
axios.post(`${PAYPAL_API}/v2/checkout/orders`, ...