简体   繁体   中英

include metadata in stripe checkout session

I am using the pre-built stripe checkout session in node.js. here's my function:

app.get('/create-checkout-session', async (req, res) => {
    let customer = {
        price: req.query.price,
        quantity: req.query.quantity,
        page: req.query.page,
        email: req.query.email,
        name: req.query.name
    }

    // insertIntoDbPreCheckout(customer, req, res)

    let successurl = 'http://localhost:1111/' + customer.page + ''
    let failedurl = 'http://localhost:1111/' + customer.page + ''
  const session = await stripe.checkout.sessions.create({
    payment_method_types: ['card'],
    metadata: {
      'description': customer.page
    },
    customer_email: customer.email,
    line_items: [
      {
        price_data: {
          currency: 'cad',
          product_data: {
            name: customer.page,
          },
          unit_amount: customer.price,
        },
        quantity: customer.quantity,
      },
    ],
    mode: 'payment',
    success_url: successurl,
    cancel_url: failedurl,
  })

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

i am trying to pass METADATA through, but it isn't letting me.

I cannot find anything online that shows me how to do this, and it is not working no matter what I try. How can i fix this?

edit:

when i log the customers, it shows like this:

     email: 'uuu@u.com',
  invoice_prefix: '9FC11B6D',
  invoice_settings: { custom_fields: null, default_payment_method: null, footer: null },
  livemode: false,
  metadata: {},

as you can see, metadata is empty

Based on the updated question, this is expected. The metadata you've provided is on the Checkout session itself. You're examining the Customer object created by the Checkout session, which does not have metadata (yet).

You can add customer metadata if you want to, by updating the customer:

const customer = await stripe.customers.update(
  'cus_123',
  {metadata: {order_id: '6735'}}
);

Existing metadata would be on the Checkout session object .

You should include metadata in stripe.checkout.sessions.create as

const session = await stripe.checkout.sessions.create({
  payment_method_types: ['card'],
  line_items: [
    {
      name: 'name',
      amount: 10 * 100,
      currency: 'usd',
      quantity: 1,
    },
  ],
  payment_in
tent_data: {
        metadata: {
          userId: req.user.userId,
          nftId: body.nftId,
          price: body.price,
          email: req.user.email,
          ownerId: body.ownerId,
        },
      },
      client_reference_id: req.user.email,
      mode: 'payment',
      success_url: 'success_url',
      cancel_url: 'cancel_url',
    });

in webhook you can get the meta data as req.body.data.object.metadata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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