【发布时间】:2020-09-04 03:50:22
【问题描述】:
我得到状态码 201,但是当我尝试重定向到approval_url 时,我得到了
错误 [ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标头
当我手动单击approval_url时,我尝试不重定向并且一切正常。
这是我的代码:
var express = require("express");
var router = express.Router();
var middleAware = require("../middleware");
var paypal = require("paypal-rest-sdk");
var User = require("../models/user");
var Shoe = require("../models/shoe");
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': 'AUN6mxOAFtJS5hrlUGdyd-Fe1VOE6zu63W6dYztXhOXOpeT0ps9JbF9N3lII-f3EP1o7G2MHs9flc3Ho',
'client_secret': 'someSecretId'
});
//pay with paypal
router.post("/cart/:id/pay", function(req, res){
User.findById(req.params.id).populate("cart").exec(function(err, foundUser){
if(err){
console.log(err);
res.redirect("back")
}else{
//find all shoes to checkout
var newItems = [];
for(var i=0;i < foundUser.cart.length;i++){
itemToPush = {
"name": foundUser.cart[i].name,
"sku": foundUser.cart[i].length,
"price": foundUser.cart[i].price,
"currency": "USD",
"quantity": 1
}
newItems.push(itemToPush);
}
//totalprice
var totalPrice = 0;
foundUser.cart.forEach(function(item){
totalPrice += item.price;
return totalPrice;
});
res.redirect("back");
// create paypal payment JSON
var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://127.0.0.1:3000/shop",
"cancel_url": "http://127.0.0.1:3000/shop"
},
"transactions": [{
"item_list": {
"items": newItems
},
"amount": {
"currency": "USD",
"total": totalPrice
},
"description": "This is the payment description."
}]
};
// console.log(JSON.stringify(create_payment_json));
paypal.payment.create(create_payment_json, function(err, payment){
if(err){
throw err;
}else{
console.log(payment);
for(var i=0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
console.log("FOUND " + payment.links[i].href);
res.redirect(payment.links[i].href);
}
}
}
});
}
});
});
module.exports = router;
这里的问题是我得到了很好的状态码,但随后出现了这个错误:
(node:14648) DeprecationWarning:当前的 URL 字符串解析器已被弃用,并将在未来的版本中删除。要使用新的解析器,请将选项 { useNewUrlParser: true } 传递给 MongoClient.connect。
(node:14648) DeprecationWarning:当前的服务器发现和监控引擎已被弃用,并将在未来的版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。
服务器启动! { id: 'PAYID-L3AZRLQ4LA763246G079113E', 意图:'销售', 状态:'创建', 付款人:{ payment_method: 'paypal' }, 交易:[ { 数量:[对象], description: '这是付款说明。', item_list:[对象], 相关资源:[] } ], create_time: '2020-05-17T20:03:57Z', 链接:[ { href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L3AZRLQ4LA763246G079113E', 相对:'自我', 方法:“获取” }, { href: 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-2C992495UH4895047', rel: 'approval_url', 方法:“重定向” }, { href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L3AZRLQ4LA763246G079113E/execute', rel: '执行', 方法:'POST' } ], http状态码:201 } 找到https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-2C992495UH4895047 _http_outgoing.js:526 抛出新的 ERR_HTTP_HEADERS_SENT('set'); ^
错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头
在 ServerResponse.setHeader (_http_outgoing.js:526:11) 在 ServerResponse.header (C:\Users\Zohar Banai\Desktop\personal projects\ShopProject\node_modules\express\lib\response.js:771:10) 在 ServerResponse.location (C:\Users\Zohar Banai\Desktop\personal projects\ShopProject\node_modules\express\lib\response.js:888:15) 在 ServerResponse.redirect (C:\Users\Zohar Banai\Desktop\personal projects\ShopProject\node_modules\express\lib\response.js:926:18) 在 C:\Users\Zohar Banai\Desktop\personal projects\ShopProject\routes\cart.js:116:33 在传入消息。 (C:\Users\Zohar Banai\Desktop\personal projects\ShopProject\node_modules\paypal-rest-sdk\lib\client.js:140:13) 在 IncomingMessage.emit (events.js:323:22) 在 endReadableNT (_stream_readable.js:1204:12) 在 processTicksAndRejections (internal/process/task_queues.js:84:21) { 代码:'ERR_HTTP_HEADERS_SENT' }
【问题讨论】:
标签: javascript node.js mongodb paypal ejs