본문 바로가기
IT기술(코딩)/nodejs

stripe api my doc javascript(js) with nodejs

by 크리에이트매이커 2023. 12. 17.
반응형

https://stripe.com/docs/checkout/quickstart

 

Stripe-hosted page

stripe trigger ▶️ [event]

stripe.com

This link is simple api code with nodejs ↑

 

 

in there

app.post('/create-checkout-session', async (req, res) => {
  const session = await stripe.checkout.sessions.create({
    line_items: [
      {
        // Provide the exact Price ID (for example, pr_1234) of the product you want to sell
        price: '{{PRICE_ID}}',
        quantity: 1,
      },
    ],
    mode: 'payment',
    success_url: `${YOUR_DOMAIN}/success.html`,
    cancel_url: `${YOUR_DOMAIN}/cancel.html`,
  });

  res.redirect(303, session.url);
});

price: '{{PRICE_ID}}' is can select my product with price of my product list at Stripe.

 

When you search Product  (just make one test product and operate search code)

const products = await stripe.products.search({
  query: 'active:\'true\' AND metadata[\'order_id\']:\'6735\'',
});

you can get json data.

but you have to know that "stripe.products.search" this is not contain this price.

 

So if you want to get price each products use this.

 const products = await stripe.products.search({
    query: 'metadata[\'store_index\']:\'' + req.body.storeindex + '\'',
  });

  const prices = []
  for (let i = 0; i < (products.data).length; i++){
    const price = await stripe.prices.search({
      query: 'product:\'' + products.data[i].id + '\'',
    })
    products.data[i].id = products.data[i].id + ","+ price.data[0].id
  }
  
  res.json(products)

for use this code, you need to add metadata at your product.

"stripe.prices.search" this contain product id. so we can find price using product id which we get this at

"stripe.products.search"

 

Using all json's data you can apply this to many code.

 

반응형