无法向Stripe在线支付添加元数据。

huangapple go评论60阅读模式
英文:

Cannot add metadata to stripe online payments

问题

我正在尝试在对象创建过程中创建元数据,但无法成功。以下是我的代码:

router.post("/create-checkout-session", async (req, res) => {
  let line_items = req.body.cart.map((e)=>{
    return {
      price_data: {
        currency: "usd",
        product_data: {
          name: e.title,
          description: e.desc,
          images: [e.images],
          metadata: {
            id: e.id,
          },
        },
        unit_amount: e.price,
      },
      quantity: e.quantity,
    };
  })
  const session = await stripe.checkout.sessions.create({
    line_items,
    mode: "payment",
    success_url: `${YOUR_DOMAIN}/`,
    cancel_url: `${YOUR_DOMAIN}/cart`,
  });
  res.send({ url: session.url });
});

我可以在Stripe仪表板上看到除元数据之外的所有数据。

英文:

I am trying to create metadata during the object creation process but could not do it. Here is my code

router.post("/create-checkout-session", async (req, res) => {
  let line_items = req.body.cart.map((e)=>{
    return {
      price_data: {
        currency: "usd",
        product_data: {
          name: e.title,
          description: e.desc,
          images: [e.images],
          metadata: {
            id: e.id,
          },
        },
        unit_amount: e.price,
      },
      quantity: e.quantity,
    };
  })
  const session = await stripe.checkout.sessions.create({
    line_items,
    mode: "payment",
    success_url: `${YOUR_DOMAIN}/`,
    cancel_url: `${YOUR_DOMAIN}/cart`,
  });
  res.send({ url: session.url });
});

I can see all my data on the stripe dashboard other than metadata.

答案1

得分: 0

在您的请求中,元数据设置在 product_data 字段下,该字段仅存储在 Product 对象下。Ad-hoc Product 对象上的元数据不会显示在仪表板上,但可以通过 Retrieve Checkout Session API扩展 line_items.data.price.product 字段来检索。例如,

const session = await stripe.checkout.sessions.retrieve('cs_xxx', {
  expand: ['line_items.data.price.product',],
});

您用于检查付款的仪表板是 Payment Intent 对象。元数据不会从一个对象传递到另一个对象,即 Product 对象上的元数据不会传递到 Payment Intent 对象。如果您希望在 Payment Intent 对象中显示元数据,元数据应设置在 payment_intent_data.metadata 字段下。例如,

const session = await stripe.checkout.sessions.create({
  line_items: [
    {
      price_data: {
        currency: 'usd',
        product_data: {
          name: 'T-shirt',
        },
        unit_amount: 2000,
      },
      quantity: 1,
    },
  ],
  payment_intent_data: {
    metadata: {
      order_id: 'order_123',
    },
  },
  mode: 'payment',
  success_url: 'http://localhost:4242/success',
  cancel_url: 'http://localhost:4242/cancel',
});
英文:

In your request, the metadata is set under product_data field which will only be stored under Product object. Metadata on ad-hoc Product object isn't shown in the Dashboard, but it can be retrieved via Retrieve Checkout Session API with expanding line_items.data.price.product field. For example,

const session = await stripe.checkout.sessions.retrieve('cs_xxx', {
  expand: ['line_items.data.price.product',],
});

The dashboard you check the payment is Payment Intent object. Metadata doesn't get populated from one object to another, i.e. metadata on Product object won't be populated to Payment Intent object. If you wish to have metadata shown in Payment Intent object, the metadata should be set under payment_intent_data.metadata field. For example,

const session = await stripe.checkout.sessions.create({
  line_items: [
    {
      price_data: {
        currency: 'usd',
        product_data: {
          name: 'T-shirt',
        },
        unit_amount: 2000,
      },
      quantity: 1,
    },
  ],
  payment_intent_data: {
    metadata: {
      order_id: 'order_123',
    },
  },
  mode: 'payment',
  success_url: 'http://localhost:4242/success',
  cancel_url: 'http://localhost:4242/cancel',
});

huangapple
  • 本文由 发表于 2023年5月21日 04:39:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定