javascript - Paypal SDK 问题得到错误 [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
问题描述
我得到状态码 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', intent: 'sale', state: 'created', payer: { payment_method: 'paypal' }, transactions: [ { amount: [Object], description: '这是付款说明。 ', item_list: [Object], related_resources: [] } ], create_time: '2020-05-17T20:03:57Z', 链接: [ { href: ' https://api.sandbox.paypal.com/v1/付款/付款/PAYID-L3AZRLQ4LA763246G079113E ', rel: 'self', 方法: 'GET' }, { href: ' https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&', rel: 'approval_url', 方法: 'REDIRECT' }, { href: ' https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L3AZRLQ4LA763246G079113E/execute ', rel: '执行',方法:'POST'}],httpStatusCode:201} FOUND https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-2C992495UH4895047 _http_outgoing.js:526 throw new ERR_HTTP_HEADERS_SENT('放'); ^
错误 [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。位置(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_readable.js:1204:
解决方案
一些指示:
如果 HTTP 标头已在您的应用程序响应请求的较早时间发送到某个位置,则无法使用 HTTP 标头“位置: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/
推荐阅读
- r - 将 dplyr 管道转换为 SQL 字符串
- html - 您如何设置 HTML5 音频标签以仅显示播放/暂停按钮图像?
- javascript - 需要客户端 JavaScript 中的 PayPal PDT 示例代码
- c++ - 使用 opencv 4 和 VS 2019 的错误
- neo4j - 计算一对节点之间的关系数量,并将其设置为 Neo4J 中的参数
- python - 如何在 anaconda 提示符下激活 virtualenv 虚拟环境?
- sql - What exactly differs fuzzy search from Full Text Search?
- sockets - Using Flutter App to run SocketServer and communicate with other phone via Socket
- java - 使用 JAXB 生成类时出现问题 - 没有 @XmlRootElement
- php - 过海关字段不保存值