Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I am using Firebase cloud functions as backend and Angular framework as front,this is my code:

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

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)
});
like image 447
Sergio Manrique Avatar asked Oct 21 '25 09:10

Sergio Manrique


1 Answers

Each object in Stripe will have different metadata. It sounds like metadata was added to the Product object which will not be copied to the 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.

  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);
like image 164
cjav_dev Avatar answered Oct 22 '25 23:10

cjav_dev