简体   繁体   English

无法获得条带支付意图成功元数据

[英]Can't get stripe payment Intent success metadata

I'm new using stripe payments, so I decided to make it easy and implemented redirectToCheckout and stripe webhooks from the stripe documentation, I followed the steps, first I created a product from stripe's dashboard, then I added some metadata key and value, finally I wrote my code on Angular and everything was working great until I realized that i wasn't getting the expected metadata, actually it was empty.我是使用条带支付的新手,所以我决定让它变得简单,并从条带文档中实现了 redirectToCheckout 和条带 webhook,我按照步骤操作,首先我从条带的仪表板创建了一个产品,然后我添加了一些元数据键和值,最后我在 Angular 上编写了我的代码,一切都很好,直到我意识到我没有得到预期的元数据,实际上它是空的。

I am using Firebase cloud functions as backend and Angular framework as front,this is my code:我使用 Firebase 云功能作为后端和 Angular 框架作为前端,这是我的代码:

Angular Angular

stripe.redirectToCheckout({
  lineItems: [{ price: itemSku, quantity: 1}],
  mode: 'payment',
  customerEmail: this.userEmail,
  successUrl: 'http://localhost:4200/purchase/success',
  cancelUrl: 'http://localhost:4200/purchase/failed'
})

Firebase cloud function Firebase 云 function

app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
    const sig = request.headers['stripe-signature'];
    let event;
    try {
        event = stripe.webhooks.constructEvent(request.rawBody, sig, endpointSecret);
    } catch (err) {
        response.status(404).end()
    }

    const intent = event.data.object
    switch (event.type) {
        case constants.INTENT_SUCCESS:
            // it prints the object with empty metadata
            console.log('Success object:', intent);  <- metadata:{}
            break;
        case constants.INTENT_FAILED:
            console.log('Failed:', intent.id);
            break;
    }
    response.json({received: true});
    response.sendStatus(200)
});

Each object in Stripe will have different metadata. Stripe 中的每个 object 都会有不同的元数据。 It sounds like metadata was added to the Product object which will not be copied to the PaymentIntent .听起来元数据已添加到Product object 中,不会复制到PaymentIntent

I would listen for the checkout.session.completed webhook notification event type, then fetch the Checkout Session and expanding it's related line_items , price , and product .我会监听checkout.session.completed webhook 通知事件类型,然后获取 Checkout Session 并扩展它的相关line_itemspriceproduct

  const session = await stripe.checkout.sessions.retrieve(
    "cs_test_xxx", {
      expand: ["line_items.data.price.product"]
    }
  )
  console.log(res.line_items.data[0].price.product);

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

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