Search code examples
node.jsstripe-payments

How can I pass custom data to Stripe webhook?


I am creating and processing a stripe payment like this:

    // Create payment intent
    const { data } = await axios.post('/api/payments/get-payment-intent', { slug: post.slug })

    // Use payment intent to charge the card
    const result = await stripe.confirmCardPayment(data.paymentIntentSecert, {
      payment_method: {
        card: elements.getElement(CardElement),
      },
    })

To be able to fulfill the order, I need to be able to pass some data (id of the product and username of the buyer) to the webhook that gets executed after the payment has been successfully completed (payment_intent.succeeded event).

How can I do that?

I've tried adding a metadata key to the confirmCardPayment() like this:

    const result = await stripe.confirmCardPayment(data.paymentIntentSecert, {
      payment_method: {
        card: elements.getElement(CardElement),
        metadata: {
          username: user.username,
          postId: post.id
        }
      },
    })

But the metadata doesn't show up on the object received by the webhook.


Solution

  • It is not possible to update the PaymentIntent metadata using confirmCardPayment().

    You would first want to pass the username and postId to your backend server.

    Example

    const { data } = await axios.post('/api/payments/get-payment-intent', { 
        username:user.username, 
        postId: post.Id 
    });
    

    And subsequently create the PaymentIntent with the metadata.

    Node.js example to create a PaymentIntent

    const paymentIntent = await stripe.paymentIntents.create({
      amount: 2000,
      currency: 'usd',
      metadata: {
          username,
          postId
      },
    });