@@ -5,6 +5,51 @@ import { WebhookEvent } from "@clerk/nextjs/server";
5
5
import { api , internal } from "./_generated/api" ;
6
6
const http = httpRouter ( ) ;
7
7
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
+
8
53
http . route ( {
9
54
path : "/clerk-webhook" ,
10
55
method : "POST" ,
@@ -59,9 +104,7 @@ http.route({
59
104
}
60
105
}
61
106
return new Response ( "Webhook processed successfully" , { status : 200 } ) ;
62
-
63
107
} ) ,
64
108
} ) ;
65
109
66
-
67
110
export default http ;
0 commit comments