gpt4 book ai didi

javascript - 使用 Node js 运行 Paypal API 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:44 26 4
gpt4 key购买 nike

我在名为 pp.js 的脚本中运行以下代码。我在我的 index.html 上运行它来加载测试页面。我正在使用最新的 webpack 模块捆绑每个源文件。

    var paypal = require('paypal-rest-sdk');

var user_config ={
'mode': 'sandbox', //sandbox or live
'client_id': 'xxxxxxx',
'client_secret': 'xxxxxxx'
};

paypal.configure(user_config);

var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://return.url",
"cancel_url": "http://cancel.url"
},
"transactions": [{
"item_list": {
"items": [{
"name": "item",
"sku": "item",
"price": "1.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "1.00"
},
"description": "This is the payment description."
}]
};

// TILL HERE THERE IS NO ERROR

paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
console.log("There seems to be some error... I hope it can be corrected.");
throw error;
} else {
console.log("Create Payment Response");
console.log(payment);
}
});

当代码的最后一位运行时,即 paypal.payment.create(create_ ... 我在浏览器的控制台中收到以下错误。

Error thrown into the console

我该如何纠正这个问题?

最佳答案

此代码应在服务器端运行。 PayPal SDK 跟踪器中的相同问题:https://github.com/paypal/PayPal-node-SDK/issues/220

引用 1(来自链接的工单)

Please see #149 (comment). It seems that you are trying to run this code in the browser which may be a security problem. If the user has access to your credentials or have an access token, they can do anything that you can do as a merchant (e.g. create more payments, refund people money, etc.).

引述 2(来自 #149 ticket):

I did some more investigation. Apparently, this request.js:54 Uncaught Error: Invalid value for opts.mode. error comes from a version of stream-http/request.js. I'm guessing that you are trying to use browserify and using stream-http in the browser to simulate node.js's built-in http module.

Are you trying to run this SDK code in the browser (instead of in a server-side node.js process)?

This node.js SDK should only be used on a secured server. I have very limited experience using browserify. Unless I'm mistaken, you need to give this SDK access to your client ID and client secret to make this SDK work, and if your code runs in the browser, you will be exposing the credentials to any browser client. This will allow any customer to be able to do anything to your account (e.g. refund all of your payments).

If it's just that part of your code is intended to be run in the browser and another part is in the server but the code is all in one project, I recommend separating out the code into 2 different projects with different package.json files so you can have separate dependencies.

关于javascript - 使用 Node js 运行 Paypal API 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45864364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com