我得到状态码201,但是当我尝试重定向到rovaling_url时得到
错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头
当我手动单击rovaling_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:[Object], related_resources:[] } ], create_time:'2020-05-17T20:03:57Z', 链接:[ { href:'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L3AZRLQ4LA763246G079113E', rel:“自我”, 方法:“获取” }, { 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” } ], httpStatusCode: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 在IncomingMessage。 (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_visible.js:1204:12) 在processTicksAndRejections(internal / process / task_queues.js:84:21){ 代码:“ ERR_HTTP_HEADERS_SENT” }
一些指针:
如果HTTP标头已在应用程序对请求的响应中的较早位置发送,则无法完成HTTP标头'Location:newurl'的重定向。但是,花时间解决这个问题是不值得的-改为参阅以下内容。
重定向是旧的处理方式。不要使用任何重定向。完全没有完全没有。相反,请使用现代的“上下文”流程:将您的网站加载到后台并使用此前端:https://developer.paypal.com/demo/checkout/#/pattern/server(这是服务器集成模式;有关UI外观的演示,请单击至客户端模式,因为该模式是交互式可点击的)
PAYID来自PayPal的旧v1 / payments REST API。不要在后端使用v1 / payments。而是将服务器端API调用更改为v2 / checkout / orders。如果您需要SDK,请使用以下最新版本:https://developer.paypal.com/docs/api/rest-sdks/