-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3780cd1
commit c3c72d0
Showing
8 changed files
with
163 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
// USER | ||
export * from './user' | ||
|
||
// IMAGE | ||
export * from './image' | ||
|
||
// PRODUCT | ||
export * from './product' | ||
|
||
// BUG-REPORT | ||
export * from './bug-report' | ||
|
||
export * from './stock' | ||
|
||
export * from './push-notification' | ||
|
||
export * from './customer' | ||
|
||
export * from './stripe' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { stripeCheckoutSessionTable, type InsertCheckoutSession } from '.' | ||
import { db } from '$db' | ||
|
||
import { and, eq } from 'drizzle-orm' | ||
|
||
async function processStripeOrder(sessionId: string) { | ||
const [sessionDB] = await getStripeOrderFromID(sessionId) | ||
if (!sessionDB) { | ||
return | ||
} | ||
|
||
if (sessionDB.credited) { | ||
return | ||
} | ||
|
||
await db.update(stripeCheckoutSessionTable).set({ | ||
credited: true, | ||
}) | ||
} | ||
|
||
function insertCheckoutSession(session: InsertCheckoutSession) { | ||
return db.insert(stripeCheckoutSessionTable).values(session) | ||
} | ||
|
||
function getStripeOrderFromID(sessionId: string) { | ||
return db | ||
.select() | ||
.from(stripeCheckoutSessionTable) | ||
.where(eq(stripeCheckoutSessionTable.id, sessionId)) | ||
.limit(1) | ||
} | ||
|
||
function getPendingCheckoutSessionFromUserID(userId: string) { | ||
return db | ||
.select() | ||
.from(stripeCheckoutSessionTable) | ||
.where( | ||
and( | ||
eq(stripeCheckoutSessionTable.userId, userId), | ||
eq(stripeCheckoutSessionTable.credited, false), | ||
eq(stripeCheckoutSessionTable.expired, false), | ||
), | ||
) | ||
} | ||
|
||
export const stripeController = { | ||
processStripeOrder, | ||
insertCheckoutSession, | ||
getStripeOrderFromID, | ||
getPendingCheckoutSessionFromUserID, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
sqliteTable, | ||
text, | ||
integer, | ||
|
||
// customType, | ||
} from 'drizzle-orm/sqlite-core' | ||
import { userTable } from '../user' | ||
|
||
export const stripeCheckoutSessionTable = sqliteTable( | ||
'stripe_checkout_session', | ||
{ | ||
id: text('id').notNull().primaryKey(), | ||
userId: text('user_id') | ||
.notNull() | ||
.references(() => userTable.id, { | ||
onDelete: 'set null', | ||
}), | ||
|
||
geopoints: integer('geopoints').notNull(), | ||
stripe_json: text('stripe_json', { mode: 'json' }).notNull(), | ||
credited: integer('credited', { mode: 'boolean' }).notNull().default(false), | ||
expiresAt: integer('expires_at', { mode: 'timestamp' }).notNull(), | ||
expired: integer('expired', { mode: 'boolean' }).notNull().default(false), | ||
}, | ||
) | ||
|
||
export type InsertCheckoutSession = | ||
typeof stripeCheckoutSessionTable.$inferInsert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { RequestHandler } from './$types' | ||
|
||
export const GET: RequestHandler = async () => { | ||
return new Response() | ||
} | ||
import { error, json } from '@sveltejs/kit' | ||
import { env } from '$env/dynamic/private' | ||
import { stripe } from '$lib/server/stripe' | ||
|
||
// endpoint to handle incoming webhooks | ||
export const POST: RequestHandler = async ({ request }) => { | ||
// extract body | ||
const body = await request.text() | ||
|
||
// get the signature from the header | ||
const signature = request.headers.get('stripe-signature') | ||
|
||
if (!signature) { | ||
// signature is missing | ||
console.warn('⚠️ Webhook signature missing.') | ||
|
||
// return, because it's a bad request | ||
throw error(400, 'Invalid request') | ||
} | ||
|
||
if (!env.STRIPE_WEBHOOK_SECRET) { | ||
// webhook secret is missing | ||
console.warn('⚠️ Webhook secret missing.') | ||
|
||
// return, because it's a bad request | ||
throw error(400, 'Invalid request') | ||
} | ||
|
||
// var to hold event data | ||
let event | ||
|
||
// verify it | ||
try { | ||
event = stripe.webhooks.constructEvent( | ||
body, | ||
signature, | ||
env.STRIPE_WEBHOOK_SECRET, | ||
) | ||
} catch (err) { | ||
// signature is invalid! | ||
console.warn('⚠️ Webhook signature verification failed.', err) | ||
|
||
// return, because it's a bad request | ||
throw error(400, 'Invalid request') | ||
} | ||
|
||
// signature has been verified, so we can process events | ||
// full list of events: https://stripe.com/docs/api/events/list | ||
|
||
switch (event.type) { | ||
case 'charge.succeeded': { | ||
const charge = event.data.object | ||
console.log(`✅ Charge succeeded ${charge.id}`) | ||
break | ||
} | ||
case 'checkout.session.completed': { | ||
const session = event.data.object | ||
console.log(`✅ Checkout session completed ${session.id}`) | ||
break | ||
} | ||
|
||
default: | ||
// unhandled event | ||
console.log(`🤷♂️ Unhandled event type: ${event.type}`) | ||
break | ||
} | ||
// return a 200 with an empty JSON response | ||
return json({}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters