无法将价格传递给 Stripe Checkout nodejs
Posted
技术标签:
【中文标题】无法将价格传递给 Stripe Checkout nodejs【英文标题】:Cannot pass price into Stripe Checkout nodejs 【发布时间】:2021-01-24 04:17:59 【问题描述】:我在我的电子商务网站上使用嵌入式 nodejs / javascript 代码进行条带结帐。但是,我正在尝试传递客户将添加到购物车中的产品名称以及价格,以便我可以在 Stripe Checkout 页面上显示商品和价格。
我在连接到 DB2 后遇到了这个问题,我无法获取要传递到条带结帐会话的每件商品的价格。我认为这可能与异步有关,但即使是,我也不知道如何解决。我也收到错误:“(节点:45673)UnhandledPromiseRejectionWarning:错误:无效整数:NaN”
(请原谅代码乱码。还有一些变量没有使用,请忽略)
app.post('/create-checkout-session', (req, res) =>
var amount = stringify(req.body)
console.log(req.body.sessionID)
var userId = req.body.sessionID
console.log("email: " + req.body.customer_email)
var email = req.body.customer_email;
var deliveryTotal = req.body.totalWithDelivery;
var totalVal = amount.split("=");
var totalPrice = parseFloat(totalVal[1]);
//console.log("TOTAL PRICE: " + totalPrice);
var finalPrice = parseFloat(Math.round(totalPrice * 100) / 100);
var finalTotal = parseFloat(Math.round(totalPrice * 100) / 100) + parseFloat(Math.round(deliveryTotal));
console.log("final total: " + finalTotal);
var itemName = ""
var itemPrice = ""
var totalNewPriceTest = ""
//query to database
var productsStripe = "select * from " + userId
console.log(userId)
console.log("query to db for displaying cart on stripe page")
ibmdb.open("DATABASE=BLUDB;HOSTNAME=;PORT=50000;PROTOCOL=TCPIP;UID="";PWD="";", function (err,conn)
if (err) return console.log(err);
conn.query(productsStripe, function (err, rows)
if (err)
console.log(err)
console.log(rows)
for(var i = 0; i < rows.length; i++)
itemName = rows[i]['ITEM']
itemPrice = rows[i]['PRICE']
totalNewPriceTest = parseFloat(rows[i]['PRICE'])
console.log("item name : " + itemName + " " + itemPrice )
totalNewPriceTest = parseFloat(totalNewPriceTest);
console.log("final overall prcie: " + (totalNewPriceTest))
console.log("inside productsStripe function.")
console.log("overall prcie: " + totalNewPriceTest)
)
)
totalNewPriceTest = parseFloat(totalNewPriceTest)
var grandTotal = totalNewPriceTest;
var finalGrandTotal = parseFloat(grandTotal)
console.log(parseFloat(finalGrandTotal))
//stripe
const session = stripe.checkout.sessions.create(
shipping_address_collection:
allowed_countries: ['CA'],
,
payment_method_types: ['card'],
line_items: [
price_data:
currency: 'CAD',
product_data:
name: itemName,
,
unit_amount: finalGrandTotal,
//finalTotal * 100
,
quantity: 1,
,
],
mode: 'payment',
success_url: 'localhost:1001/successPg',
cancel_url: 'localhost:1001/catalogue',
customer_email: email,
);
console.log(session)
res.json( id: session.id );
//console.log("customer id" + customer.id)
console.log("totalNewPriceTest " + totalNewPriceTest)
);
谁能帮忙?在此先感谢您,并为编写糟糕的代码感到抱歉:(
【问题讨论】:
【参考方案1】:您必须在查询回调中编写以下行:-
totalNewPriceTest = parseFloat(totalNewPriceTest)
var grandTotal = totalNewPriceTest;
var finalGrandTotal = parseFloat(grandTotal)
console.log(parseFloat(finalGrandTotal))
在将数据解析为 int 或 float 之前进行错误检查
if(!isNAN(field))
value = parseFloat(field);
【讨论】:
【参考方案2】:我确实按照您所说的进行,没有错误,但是它仍然没有到达条带结帐页面...它在控制台中登录:Promise 。我确实对此进行了研究,它说这再次与异步有关。不知道如何解决,阅读一些关于 .then 的内容也可以吗?
【讨论】:
【参考方案3】:如你所料,这是一个经典的并发问题,首先,来自 MDN 的 this 完整指南很好地解释了异步 javascript。
为了简要回答您的情况,您需要继续在查询块中执行条带代码。为什么?因为您需要等待数据库连接打开,然后执行查询,这两者都是异步的。
当您绕过这些块时,您基本上是在告诉 javascript 并行执行代码,在您的情况下这不是您想要的,您希望等待查询完成。
app.post('/create-checkout-session', (req, res) =>
var amount = stringify(req.body)
console.log(req.body.sessionID)
var userId = req.body.sessionID
console.log("email: " + req.body.customer_email)
var email = req.body.customer_email;
var deliveryTotal = req.body.totalWithDelivery;
var totalVal = amount.split("=");
var totalPrice = parseFloat(totalVal[1]);
//console.log("TOTAL PRICE: " + totalPrice);
var finalPrice = parseFloat(Math.round(totalPrice * 100) / 100);
var finalTotal = parseFloat(Math.round(totalPrice * 100) / 100) + parseFloat(Math.round(deliveryTotal));
console.log("final total: " + finalTotal);
var itemName = ""
var itemPrice = ""
var totalNewPriceTest = ""
//query to database
var productsStripe = "select * from " + userId
console.log(userId)
console.log("query to db for displaying cart on stripe page")
ibmdb.open("DATABASE=BLUDB;HOSTNAME=;PORT=50000;PROTOCOL=TCPIP;UID="";PWD="";", function (err,conn)
if (err) return console.log(err);
conn.query(productsStripe, function (err, rows)
if (err)
console.log(err)
console.log(rows)
for(var i = 0; i < rows.length; i++)
itemName = rows[i]['ITEM']
itemPrice = rows[i]['PRICE']
totalNewPriceTest = parseFloat(rows[i]['PRICE'])
console.log("item name : " + itemName + " " + itemPrice )
totalNewPriceTest = parseFloat(totalNewPriceTest);
console.log("final overall prcie: " + (totalNewPriceTest))
console.log("inside productsStripe function.")
console.log("overall prcie: " + totalNewPriceTest)
totalNewPriceTest = parseFloat(totalNewPriceTest)
var grandTotal = totalNewPriceTest;
var finalGrandTotal = parseFloat(grandTotal)
console.log(parseFloat(finalGrandTotal))
// continue executing here
//stripe
stripe.checkout.sessions.create(
shipping_address_collection:
allowed_countries: ['CA'],
,
payment_method_types: ['card'],
line_items: [
price_data:
currency: 'CAD',
product_data:
name: itemName,
,
unit_amount: finalGrandTotal,
//finalTotal * 100
,
quantity: 1,
,
],
mode: 'payment',
success_url: 'localhost:1001/successPg',
cancel_url: 'localhost:1001/catalogue',
customer_email: email,
).then((session) =>
console.log(session)
res.json( id: session.id );
//console.log("customer id" + customer.id)
console.log("totalNewPriceTest " + totalNewPriceTest)
).catch((err) =>
console.log('stripe err: ', err);
)
)
)
);
其他有用的提示:
不要在路由器内部编写业务逻辑,而是创建一个控制器文件并将逻辑移入其中。 不要在每次请求时都连接到数据库,而是创建一个数据库连接实例并在需要时保持打开和可用,首先创建自己的帮助文件,然后导出连接。【讨论】:
感谢您的回复!我确实按照您所说的进行,没有错误,但是它仍然没有到达条带结帐页面...它登录到控制台:Promisesessions.create
返回了一个承诺,我编辑了我的答案以上是关于无法将价格传递给 Stripe Checkout nodejs的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Stripe Checkout 仅客户端实现中包含元数据?