Skip to content

Commit 3dce34e

Browse files
committed
fix: small bug in script webhooks to handle canceled subscriptions
1 parent 1a85cb3 commit 3dce34e

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

src/app.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ declare global {
99
// interface Error {}
1010
interface Locals {
1111
supabaseServer: SupabaseClient<Database>
12-
launcherSupabase: SupabaseClient<Database>
1312
safeGetSession: () => Promise<{ session: Session | null; user: User | null }>
1413
session: Session | null
1514
user: User | null

src/hooks.server.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,6 @@ const supabase: Handle = async ({ event, resolve }) => {
3232
}
3333
})
3434

35-
event.locals.launcherSupabase = createServerClient<Database>(
36-
PUBLIC_SUPABASE_URL,
37-
PUBLIC_SUPABASE_ANON_KEY,
38-
{
39-
cookies: {
40-
getAll: () => event.cookies.getAll(),
41-
setAll: (cookiesToSet) => {
42-
cookiesToSet.forEach(({ name, value, options }) => {
43-
event.cookies.set(name, value, { ...options, path: "/" })
44-
})
45-
}
46-
}
47-
}
48-
)
49-
5035
event.locals.safeGetSession = async () => {
5136
let start = performance.now()
5237

src/lib/types/supabase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,17 @@ export type Database = {
140140
Row: {
141141
balance: number
142142
id: string
143-
stripe: string | null
143+
stripe: string
144144
}
145145
Insert: {
146146
balance?: number
147147
id?: string
148-
stripe?: string | null
148+
stripe: string
149149
}
150150
Update: {
151151
balance?: number
152152
id?: string
153-
stripe?: string | null
153+
stripe?: string
154154
}
155155
Relationships: [
156156
{

src/routes/api/stripe/subscriptions/+server.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,25 @@ export const POST = async ({ request }) => {
102102
error(500, "object: " + JSON.stringify(subscriptionDeleted) + "\r\n" + formatError(err))
103103
}
104104

105-
const invoice = subscriptionDeleted.latest_invoice
106-
if (invoice) {
107-
try {
108-
stripe.invoices.voidInvoice(invoice.toString())
109-
} catch (err) {
110-
console.error(err)
111-
error(
112-
404,
113-
"Failed to void invoce: " + invoice + " for sub: " + subscriptionDeleted.id + " Error: " + err
114-
)
105+
const last_invoice = subscriptionDeleted.latest_invoice
106+
if (last_invoice) {
107+
const invoiceId = last_invoice.toString()
108+
const invoice = await stripe.invoices.retrieve(invoiceId)
109+
if (invoice.status != "paid" && invoice.status != "void") {
110+
try {
111+
stripe.invoices.voidInvoice(invoiceId)
112+
} catch (err) {
113+
console.error(err)
114+
error(
115+
404,
116+
"Failed to void invoce: " +
117+
last_invoice +
118+
" for sub: " +
119+
subscriptionDeleted.id +
120+
" Error: " +
121+
err
122+
)
123+
}
115124
}
116125
}
117126
break

0 commit comments

Comments
 (0)