@@ -5,6 +5,51 @@ import { WebhookEvent } from "@clerk/nextjs/server";
55import { api , internal } from "./_generated/api" ;
66const http = httpRouter ( ) ;
77
8+ http . route ( {
9+ path : "/lemon-squeezy-webhook" ,
10+ method : "POST" ,
11+ handler : httpAction ( async ( ctx , req ) => {
12+ const payloadString = await req . json ( ) ;
13+ const signature = req . headers . get ( "X-Signature" ) ;
14+ if ( ! signature ) {
15+ return new Response ( "Missing signature" , { status : 400 } ) ;
16+ }
17+ try {
18+ const payload = await ctx . runAction ( internal . lemonSqueezy . verifyWebhook , {
19+ payload : payloadString ,
20+ signature,
21+ } ) ;
22+
23+ if ( payload . meta . event_name === "order.created" ) {
24+ const { data } = payload ;
25+ const { success} = await ctx . runMutation ( api . users . upgradePro , {
26+ email : data . attributes . use_email ,
27+ lemonSqueezyCustomerId : data . attributes . customer_id . toString ( ) ,
28+ lemonSqueezyOrderId : data . id ,
29+ amount : data . attributes . total ,
30+ } ) ;
31+
32+ if ( success ) {
33+
34+
35+
36+
37+
38+
39+
40+
41+ }
42+ }
43+
44+ return new Response ( "Webhook processed successfully" , { status : 200 } ) ;
45+
46+ } catch ( e ) {
47+ console . log ( "Webhook error:" , e ) ;
48+ return new Response ( "Error processing webhook" , { status : 500 } ) ;
49+ }
50+ } ) ,
51+ } ) ;
52+
853http . route ( {
954 path : "/clerk-webhook" ,
1055 method : "POST" ,
@@ -59,9 +104,7 @@ http.route({
59104 }
60105 }
61106 return new Response ( "Webhook processed successfully" , { status : 200 } ) ;
62-
63107 } ) ,
64108} ) ;
65109
66-
67110export default http ;
0 commit comments