运行 Paypal API 使用节点 js 时出错
Error while running Paypal API using node js
我正在 运行 在名为 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_
...我在浏览器的控制台中收到以下错误。
我该如何纠正这个问题?
此代码应 运行 服务器端。 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.
我正在 运行 在名为 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_
...我在浏览器的控制台中收到以下错误。
我该如何纠正这个问题?
此代码应 运行 服务器端。 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 usebrowserify
and usingstream-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.