如何解决无法将商品和价格传递到条纹结帐中
我正在为我的自定义电子商务网站使用嵌入式的nodejs / javascript Stripe Checkout。我有一个使用DB2创建的我自己的购物车,并且想要传递客户在购物车中拥有的产品名称以及每个产品的匹配价格。 Stripe不清楚如何做到这一点,我一直在努力寻找正确的方法。我现在正在尝试使用2个数组(产品数组和价格数组),并试图将它们传递给Stripe函数,但是没有运气。这是代码。
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 = ""
//defining arrays
var productsArray = [];
var priceArray = [];
//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=dashdb-txn-sbox-yp-dal09-14.services.dal.bluemix.net;PORT=50000;PROTOCOL=TCPIP;UID=;PWD=",function (err,conn) {
if (err) return console.log(err);
conn.query(productsStripe,async 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))
//inserting items and prices into arrays
productsArray.push(rows[i]['ITEM'])
priceArray.push(rows[i]['PRICE'])
console.log("ARRAY " + productsArray)
console.log("PRICE ARRAY " + priceArray)
}
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 = await stripe.checkout.sessions.create({
shipping_address_collection: {
allowed_countries: ['CA'],},payment_method_types: ['card'],line_items: [
{
price_data: {
currency: 'CAD',product_data: {
name: stringify(productsArray),unit_amount: parseFloat(priceArray),//finalTotal * 100
},quantity: 1,],mode: 'payment',success_url: 'https://floralfashionboutique.com/successPg',cancel_url: 'https://floralfashionboutique.com/catalogue',customer_email: email,});
// console.log(session)
res.json({ id: session.id });
//console.log("customer id" + customer.id)
console.log("totalNewPriceTest " + totalNewPriceTest)
})
})
});
请让我知道之前是否有人尝试过这样做,在线上没有太多关于此的内容……也很抱歉,凌乱的代码...
解决方法
这里有几件事:
-
如果要动态内联创建产品,请使用
-
product_data
。通常,您需要事先在仪表板上创建这些文件。如果仍然需要动态创建它们,则应遵循此处描述的对象形状:https://stripe.com/docs/api/checkout/sessions/create?lang=node#create_checkout_session-line_items-price_data-product_data -
unit_amount
必须为整数。parseFloat(priceArray)
将解析priceArray
的第一个值,而忽略其余的值,这大概不是您要在此处执行的操作 -
如果您要创建多个价格,每个价格都有自己的商品和
unit_amount
,则需要在line_items
中为每个价格创建一个条目。像这样:
// loop over your products array to construct the line_items
const items = productsArray.map((product,i) => {
return {
price_data: {
currency: 'CAD',product_data: {
name: product,},unit_amount: parseInt(priceArray[i],10),quantity: 1,};
});
const session = await stripe.checkout.sessions.create({
shipping_address_collection: {
allowed_countries: ['CA'],payment_method_types: ['card'],line_items: items,mode: 'payment',success_url: 'https://floralfashionboutique.com/successPg',cancel_url: 'https://floralfashionboutique.com/catalogue',customer_email: email,});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。