@@ -88,7 +88,6 @@ export default async function handler(
88
88
. update ( admins )
89
89
. set ( {
90
90
stripeSubscriptionId : newSubscription . id ,
91
- serversQuantity : 0 ,
92
91
stripeCustomerId : newSubscription . customer as string ,
93
92
} )
94
93
. where ( eq ( admins . stripeCustomerId , newSubscription . customer as string ) )
@@ -121,12 +120,6 @@ export default async function handler(
121
120
}
122
121
case "customer.subscription.updated" : {
123
122
const newSubscription = event . data . object as Stripe . Subscription ;
124
- await db
125
- . update ( admins )
126
- . set ( {
127
- serversQuantity : newSubscription ?. items ?. data ?. [ 0 ] ?. quantity ?? 0 ,
128
- } )
129
- . where ( eq ( admins . stripeCustomerId , newSubscription . customer as string ) ) ;
130
123
131
124
const admin = await findAdminByStripeCustomerId (
132
125
newSubscription . customer as string ,
@@ -136,8 +129,27 @@ export default async function handler(
136
129
return res . status ( 400 ) . send ( "Webhook Error: Admin not found" ) ;
137
130
}
138
131
139
- const newServersQuantity = admin . serversQuantity ;
140
- await updateServersBasedOnQuantity ( admin . adminId , newServersQuantity ) ;
132
+ if ( newSubscription . status === "active" ) {
133
+ await db
134
+ . update ( admins )
135
+ . set ( {
136
+ serversQuantity : newSubscription ?. items ?. data ?. [ 0 ] ?. quantity ?? 0 ,
137
+ } )
138
+ . where (
139
+ eq ( admins . stripeCustomerId , newSubscription . customer as string ) ,
140
+ ) ;
141
+
142
+ const newServersQuantity = admin . serversQuantity ;
143
+ await updateServersBasedOnQuantity ( admin . adminId , newServersQuantity ) ;
144
+ } else {
145
+ await disableServers ( admin . adminId ) ;
146
+ await db
147
+ . update ( admins )
148
+ . set ( { serversQuantity : 0 } )
149
+ . where (
150
+ eq ( admins . stripeCustomerId , newSubscription . customer as string ) ,
151
+ ) ;
152
+ }
141
153
142
154
break ;
143
155
}
@@ -148,6 +160,13 @@ export default async function handler(
148
160
newInvoice . subscription as string ,
149
161
) ;
150
162
163
+ if ( suscription . status !== "active" ) {
164
+ console . log (
165
+ `Skipping invoice.payment_succeeded for subscription ${ suscription . id } with status ${ suscription . status } ` ,
166
+ ) ;
167
+ break ;
168
+ }
169
+
151
170
await db
152
171
. update ( admins )
153
172
. set ( {
@@ -168,22 +187,29 @@ export default async function handler(
168
187
}
169
188
case "invoice.payment_failed" : {
170
189
const newInvoice = event . data . object as Stripe . Invoice ;
171
- await db
172
- . update ( admins )
173
- . set ( {
174
- serversQuantity : 0 ,
175
- } )
176
- . where ( eq ( admins . stripeCustomerId , newInvoice . customer as string ) ) ;
177
190
178
- const admin = await findAdminByStripeCustomerId (
179
- newInvoice . customer as string ,
191
+ const subscription = await stripe . subscriptions . retrieve (
192
+ newInvoice . subscription as string ,
180
193
) ;
181
194
182
- if ( ! admin ) {
183
- return res . status ( 400 ) . send ( "Webhook Error: Admin not found" ) ;
195
+ if ( subscription . status !== "active" ) {
196
+ const admin = await findAdminByStripeCustomerId (
197
+ newInvoice . customer as string ,
198
+ ) ;
199
+
200
+ if ( ! admin ) {
201
+ return res . status ( 400 ) . send ( "Webhook Error: Admin not found" ) ;
202
+ }
203
+ await db
204
+ . update ( admins )
205
+ . set ( {
206
+ serversQuantity : 0 ,
207
+ } )
208
+ . where ( eq ( admins . stripeCustomerId , newInvoice . customer as string ) ) ;
209
+
210
+ await disableServers ( admin . adminId ) ;
184
211
}
185
212
186
- await disableServers ( admin . adminId ) ;
187
213
break ;
188
214
}
189
215
0 commit comments