#jimmy-help

1 messages ยท Page 1 of 1 (latest)

orchid agate
#

Hi there

#

How can I help?

proven ridge
#

I'm new using webhook stripe. I'm listening to customer.subscription.created event and get subscription data and save in my database.

In the same event, I want update the invoice metadata, have this code:

const updateInvoice = () => {
await stripe?.invoices.update(latest_invoice as string, {
metadata: { organizationId, planId, spannerSubscriptionId },
});
}

After, on invoice.payment_succeeded event want get the metadata, but i received metadata: {} because the invoice.payment_succeeded event fires first and updateInvoice function is delayed

#

Is there any way to go from updating the information of the invoice in the same customer.subscription.created event?

toxic halo
#

Hi there ๐Ÿ‘‹ taking over for @orchid agate

Give me a few minutes to get caught up.

charred zealot
#

sure, thanks for the attention

proven ridge
#

sure, thans @toxic halo

#

Plase, i need for you help

toxic halo
#

Is there any way to go from updating the information of the invoice in the same customer.subscription.created event?
So it sounds like all of those things happen consecutively on the first subscription payment for you. Can you use the final event in the sequence to update your database instead?

#

So rather than using customer.subscription.created you would use invoice.payment_succeeded

proven ridge
#

First the customer.subscription.created event is executed:

In this event, I create a record in my database to the subscription table and it generates a Subscriptionid

I want to pass this Subscriptionid to the invoice metadata. After, in the second event called invoice.payment_succeeded, i want get this metadata: {SubscriptionId: "xxxxxx"}

#

but i received metadata: {}

#
const updateInvoice = (spannerSubscriptionId) => {
await stripe?.invoices.update(latest_invoice as string, {
  metadata: { spannerSubscriptionId },
});
}

switch(event.type){
  case "customer.subscription.created": {
    const { spannerSubscriptionId } = await addSubscription();

    updateInvoice(spannerSubscriptionId)
  }

  case "invoice.payment_succeeded":
    const subscription = (await stripe?.subscriptions.retrieve(
      subscriptionId as string
    )) as Stripe.Subscription;
    const { metadata } = subscription;
    const { spannerSubscriptionId } = metadata;

    // spannerSubscriptionId = null
}

toxic halo
#

Yeah, like i said, these events often happen one right after the other with almost no delay between them. You need to wait until the invoice is paid before you pass the invoice id as metadata to the subscription.

A better question might be: why are you passing that as metadata when the Invoice already has the Subscription ID?

https://stripe.com/docs/api/invoices/object#invoice_object-subscription

proven ridge
#

My spannerSubscriptionId is not the subscriptId of Stripe, my spannerSubscriptionId is a ID like "a8871577-7648-4df8-8c03-f8c8739dd71c"

#

I want pass my spannerSubscriptionId to the invoice metadata

#

I want to do this at the customer.subscription.created event and using this metadata in the next event called invoice.payment_succeeded

toxic halo
#

Ahhh, okay I see what you're saying. In that case, my recommendation still stands. you can't update the Subscription's metadata during the creation event, then get that same metadata during the invoice.payment_succeeded event. That's a race condition and the timing of webhooks is variable, so don't do that.

proven ridge
#

Ohhh ๐Ÿ˜ฆ

#

I understand

#

Don't worry, I will follow your advice