简体   繁体   English

将多个产品添加到条带结帐 nodejs

[英]add multiple products to stripe checkout nodejs

I am creating an e-commerce site, where the customer will be able to add products to their cart.我正在创建一个电子商务网站,客户可以在其中将产品添加到他们的购物车中。 on checkout, I need all the items in the cart to be put into the stripe checkout page -- what I mean by this is, in my code, I need to pass the image, name, and price of all the products in my cart, into the stripe checkout page...在结账时,我需要将购物车中的所有商品放入条纹结账页面——我的意思是,在我的代码中,我需要传递购物车中所有产品的图片、名称和价格,进入条纹结帐页面...

here's my code so far!到目前为止,这是我的代码!

const YOUR_DOMAIN = 'http://localhost:1208';
app.post('/create-checkout-session', async (req, res) => {
    var user = "select * from " + tableID + "";
    ibmdb.open(db2ConnString, function(err, conn) {
        if (err) return console.log(err);
        conn.query(user, function(err, rows) {
            if (err) {
                console.log(err);
            }

            var productName = "";
            var image_url = "";
            var price = "";

            for (var i = 0; i < rows.length; i++ ) {
                productName = rows[i].NAME;
                image_url = rows[i].IMAGE_URL;
                price = rows[i].PRICE;
            }

            conn.close(function() {
                console.log("closed function")
            });
        });
    })

  const session = await stripe.checkout.sessions.create({
    payment_method_types: ['card'],
    line_items: [
      {
        price_data: {
          currency: 'cad',
          product_data: {
            name: ProductName,
            images: image_url,
          },
          unit_amount: price,
        },
        quantity: 1,
      },
    ],
    mode: 'payment',
    success_url: `${YOUR_DOMAIN}/success.html`,
    cancel_url: `${YOUR_DOMAIN}/cancel.html`,
  });
  res.json({ id: session.id });
 
});

there will be multiple products in the cart, so how do I fix this?购物车中会有多个产品,那么我该如何解决这个问题?

If I am reading your code right如果我正确阅读您的代码

            for (var i = 0; i < rows.length; i++ ) {
                productName = rows[i].NAME;
                image_url = rows[i].IMAGE_URL;
                price = rows[i].PRICE;
            }

You need to put these values into an array of objects, where each object is formatted like the price_data hash that CheckoutSession is expecting.您需要将这些值放入对象数组中,其中每个 object 的格式类似于 CheckoutSession 所期望的price_data hash。

So after your for-loop, your array eg price_data_array would look like:因此,在您的 for 循环之后,您的数组(例如price_data_array将如下所示:

[
      {
        price_data: {
          currency: 'cad',
          product_data: {
            name: ProductName,
            images: image_url,
          },
          unit_amount: price,
        },
        quantity: 1,
      },
      {
        price_data: {
          currency: 'cad',
          product_data: {
            name: ProductName,
            images: image_url,
          },
          unit_amount: price,
        },
        quantity: 1,
      },
      .....
]

Then when you create your CheckoutSession, just pass that array under line_items: price_data_array .然后,当您创建 CheckoutSession 时,只需在line_items: price_data_array下传递该数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM