#ayushh_webhooks

1 messages ยท Page 1 of 1 (latest)

heavy ginkgoBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1345118956327014451

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

storm turtle
#

Does my question provide much info or you need more?

trim aurora
#

Are you able to try this?

storm turtle
#

Oh, I didn't know this

#

lemme try this

trim aurora
#

Sure

storm turtle
#
case "customer.subscription.created":
            subscription = event.data.object;
            status = subscription.status;
            const metadata = event.data.object.subscription_data.metadata;
            console.log("Event Data", event.data.object);
            console.log("Metadata", metadata);
            console.log("Subscription status : ", subscription.status);
            console.log(`Subscription created. Status: ${status}`);
            // Store the new subscription
            await storage.updateUserSubscription(
              metadata.userId,
              subscription.id,
              status,
            );
            break;
#

Should I handle the metadata like this?

heavy ginkgoBOT
trim aurora
#

No, you said you're using Checkout Sessions right?

storm turtle
#

Yes, I am using checkout session

#

I am creating subscription too

#

I should use the checkout.session.completed event only to handle the subscription change too?

trim aurora
#

What does 'I should use the checkout.session.completed event only to handle the subscription change too' mean? Are you able to reword this?

storm turtle
#

I mean, i have price id for these plans

#

so they are like subscription

#

I can manipulate them through my profile

#

Like cancel the subs or change the plan

#

forgive me if i am using incorrect words/phrases

trim aurora
#

So what do you want to see now and where?

storm turtle
#
async updateUserSubscription(
    userId: string,
    subscriptionId: string,
    subscriptionStatus: "active" | "past_due" | "canceled" | "incomplete" | "incomplete_expired" | "trialing" | "unpaid",
    sessionId?: string
  ): Promise<User> {
    try {
      console.log(':pencil: Updating user subscription:', {
        userId,
        subscriptionId,
        subscriptionStatus,
        sessionId,
        timestamp: new Date().toISOString()
      });

      const updateValues: Partial<User> = {
        subscriptionId,
        subscriptionStatus,
      };

      if (sessionId) {
        updateValues.sessionId = sessionId;
        console.log(':pencil: Including session ID in update:', sessionId);
      }

      const [user] = await db
        .update(users)
        .set(updateValues)
        .where(eq(users.id, userId))
        .returning();

      if (!user) {
        const error = new Error(`No user found with ID ${userId}`);
        console.error(':x: Update failed:', error);
        throw error;
      }

      console.log(':white_check_mark: User subscription updated successfully:', {
        id: user.id,
        subscriptionId: user.subscriptionId,
        subscriptionStatus: user.subscriptionStatus,
        sessionId: user.sessionId,
        timestamp: new Date().toISOString()
      });

      return user;
    } catch (error) {
      console.error(':x: Error updating user subscription:', error);
      throw error;
    }
  }
#

I want to update the subscription of the user

#

in the users table

#

I know I am missing some values here in the webhook for which I need help to handle events and get the correct data

vast saffron
#

Hi! Things starting with whsec_ are secrets, and shouldn't be shared. Just FYI.

storm turtle
#

Sorry, but did I share here?

vast saffron
#

In the form, ya. Just something to think about for next time.

storm turtle
#

oh wait I am so sorry

vast saffron
#

I'm taking over for my colleague; can you just sum up the issue for me if you don't mind?

storm turtle
#

I wanted to share the evt id

storm turtle
vast saffron
#

And what error are you getting?

#

Or what issue are you seeing?

#

Also what's the Event ID? ๐Ÿ™‚

storm turtle
#

evt_1QxZIGKvfF85f0JWXnDuyZYp

storm turtle
#

and fill in the required subscriptionId and the stripe session id

#

maybe I was handling events wrongly

vast saffron
#

What event were you listening to?

#

Ahh I see the code now.

storm turtle
#

noted

#

and which event should I only listen to?

vast saffron
#

There's nothing wrong with the code you've got, it isn't working the way you expect because the Subscription currently has no Metadata.

storm turtle
#

Okay

#

thanks

#

lemme try it