orykevin
orykevin2w ago

Polar Component

Hi Guys, I am using Polar component to handle the subscriptions, and I want to trigger onSubscriptionUpdated, when I tried it, it's not working this is my code : polar.registerRoutes(http, { path: "/polar/events", onSubscriptionCreated: async (ctx, event) => { console.log("Subscription created", event); }, onSubscriptionUpdated: async (ctx, event) => { console.log("Subscription updated", event); if(event.data.status === "active") { ctx.runMutation(internal.v1.userToken.handleActivedSubscription, { userId: event.data.customer.metadata.userId as Id<"users">, productKey: event.data.product.metadata.key as string, }); } }, }); I tried to log it but it's not triggering either, did anyone have this problem too?
13 Replies
Convex Bot
Convex Bot2w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
erquhart
erquhart2w ago
Make sure you've configured your Polar webhook to send events on subscription update
orykevin
orykevinOP2w ago
Hi @erquhart , I already ticked all for webhook config, subscription, and customer table in polar component are inserted perfectly when I try, but I just can't trigger onSubscriptionUpdated
No description
No description
No description
orykevin
orykevinOP2w ago
this is log when I tried to redeliver the webhook event.
No description
orykevin
orykevinOP2w ago
registerRoutes( http: HttpRouter, { path = "/polar/events", }: { path?: string; onSubscriptionCreated?: ( ctx: RunMutationCtx, event: WebhookSubscriptionCreatedPayload ) => Promise<void>; onSubscriptionUpdated?: ( ctx: RunMutationCtx, event: WebhookSubscriptionUpdatedPayload ) => Promise<void>; onProductCreated?: ( ctx: RunMutationCtx, event: WebhookProductCreatedPayload ) => Promise<void>; onProductUpdated?: ( ctx: RunMutationCtx, event: WebhookProductUpdatedPayload ) => Promise<void>; } = {} ) { http.route({ path, method: "POST", handler: httpActionGeneric(async (ctx, request) => { if (!request.body) { throw new Error("No body"); } const body = await request.text(); const headers = Object.fromEntries(request.headers.entries()); try { const event = validateEvent(body, headers, this.webhookSecret); switch (event.type) { case "subscription.created": { await ctx.runMutation(this.component.lib.createSubscription, { subscription: convertToDatabaseSubscription(event.data), }); break; } case "subscription.updated": { await ctx.runMutation(this.component.lib.updateSubscription, { subscription: convertToDatabaseSubscription(event.data), }); break; } case "product.created": { await ctx.runMutation(this.component.lib.createProduct, { product: convertToDatabaseProduct(event.data), }); break; } case "product.updated": { await ctx.runMutation(this.component.lib.updateProduct, { product: convertToDatabaseProduct(event.data), }); break; } } return new Response("Accepted", { status: 202 }); } catch (error) { if (error instanceof WebhookVerificationError) { console.error(error); return new Response("Forbidden", { status: 403 }); } throw error; } }) I tried to look at the Polar class in the module, please correct me if I'm wrong because I'm not expert on typescript, but when I tried to find 'onSubscriptionUpdated' it's just exist in type definition I'm using the module with version 0.4.4
erquhart
erquhart2w ago
This says the attempt worked and convex/lib.ts updateSubscription mutation succeeded
orykevin
orykevinOP2w ago
Yeah, it's working on adding data to polar component, but I want to add more logic when user subscription active, I want to trigger another mutation, I try to pass my callback here : onSubscriptionUpdated: async (ctx, event) => { console.log("Subscription updated", event); if(event.data.status === "active") { ctx.runMutation(internal.v1.userToken.handleActivedSubscription, { userId: event.data.customer.metadata.userId as Id<"users">, productKey: event.data.product.metadata.key as string, }); } }, In that code, I tried to log it the event data, but no trigger at all
orykevin
orykevinOP2w ago
and my case is like in this example
No description
erquhart
erquhart2w ago
There's a running example here - I just tried it with the latest polar packages (and updated them in the repo), everything works. Hopefully helpful for comparison: https://github.com/get-convex/polar/blob/main/example/convex/http.ts
GitHub
polar/example/convex/http.ts at main · get-convex/polar
Contribute to get-convex/polar development by creating an account on GitHub.
erquhart
erquhart2w ago
Also, obligatory checks to make: - when you're activating webhooks in Polar, are you looking at the correct instance? If you're testing with convex dev, and using sandbox, are you looking at the sandbox instance in Polar? - are you looking at the same Convex dashboard that you're running functions against (dev/prod)
orykevin
orykevinOP2w ago
it's already correct on my side, and the data is updated too. I do not know how to change the module and apply it to convex deployment, So I copied the polar component code to my convex instead, and I changed the Polar class, and it's working so can you check the code that I changed. I tried to trigger the webhook again and my callback is running now.
No description
No description
erquhart
erquhart2w ago
So this was totally a bug in the component :facepalm: , user hooks just weren't being called at all. I was conflating the internal hooks with user hooks in my testing. This is fixed in 0.4.5.
orykevin
orykevinOP2w ago
Okay, Thanks for checking!

Did you find this page helpful?