Skip to content

Commit ee687b7

Browse files
committed
fixed stripe
1 parent f703cea commit ee687b7

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "User" ADD COLUMN "stripe_id" TEXT;

prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ model User {
2020
sessions Session[]
2121
username String
2222
google_id String?
23+
stripe_id String?
2324
bridgeUserAndSpecification BridgeUserAndSpecfication[]
2425
SpecificationSnapshot SpecificationSnapshot[]
2526
Workspaces BridgeWorkspaceAndUser[]

src/lib/server/services/data/db/dbService.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
encodeHexLowerCase,
77
} from "@oslojs/encoding";
88
import { sha256 } from "@oslojs/crypto/sha2";
9+
import { stripeClient } from "$lib/server/services/data/stripeClient";
910

1011
const ONE_MONTH = 1000 * 60 * 60 * 24 * 30;
1112
const HALF_MONTH = ONE_MONTH / 2;
@@ -41,6 +42,7 @@ export type SessionValidationResult = {
4142
email: string;
4243
uuid: string;
4344
isSubscribed: boolean;
45+
stripeId: string;
4446
};
4547
};
4648

@@ -103,6 +105,7 @@ export async function validateSessionToken(
103105
email: user.email,
104106
username: user.username,
105107
isSubscribed: userHasSubscription,
108+
stripeId: user.stripe_id ?? "",
106109
},
107110
};
108111

@@ -140,12 +143,20 @@ export async function getOrCreateUser(
140143
...user,
141144
};
142145
} else {
146+
const uuid = crypto.randomUUID();
147+
const customer = await stripeClient.customers.create({
148+
email: email,
149+
metadata: {
150+
userId: uuid,
151+
},
152+
});
143153
const res: User = await prisma.user.create({
144154
data: {
145155
username,
146156
email,
147-
uuid: crypto.randomUUID(),
157+
uuid: uuid,
148158
google_id: googleId,
159+
stripe_id: customer.id,
149160
},
150161
});
151162
return {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@ export async function POST(
88
if (!locals.authState?.user.email) {
99
return new Response(undefined, { status: 401 });
1010
}
11-
const customerEmail = locals.authState?.user.email;
12-
const customer = await stripeClient.customers.create({
13-
email: customerEmail,
14-
metadata: {
15-
userId: locals.authState.user.uuid,
16-
},
17-
});
1811
const session = await stripeClient.checkout.sessions.create({
19-
customer: customer.id,
12+
customer: locals.authState.user.stripeId,
2013
billing_address_collection: "auto",
2114
line_items: [
2215
{
@@ -28,7 +21,6 @@ export async function POST(
2821
success_url: DOMAIN + `/upgrade?session_id={CHECKOUT_SESSION_ID}`,
2922
cancel_url: DOMAIN + `/stripe/cancel`,
3023
client_reference_id: "1",
31-
customer_email: customerEmail,
3224
metadata: {
3325
userId: locals.authState.user.uuid,
3426
},

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ export async function POST(
3434

3535
let subscription: Stripe.Subscription;
3636
let checkoutSession: Stripe.Checkout.Session;
37-
const customerId = event?.data?.object as { customer: string };
37+
const customerObj = event?.data?.object as { customer: string };
3838
let subscriptionId = "";
3939
let yizyUserId = "";
4040

41-
if (typeof customerId !== "string") {
41+
if (typeof customerObj.customer !== "string") {
4242
throw new Error(
4343
`[STRIPE HOOK] ID isn't string.\nEvent type: ${event.type}`,
4444
);
4545
}
46+
const customerId = customerObj.customer;
4647

4748
switch (event.type) {
4849
case "checkout.session.completed":

vite.config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { sveltekit } from '@sveltejs/kit/vite';
2-
import { defineConfig } from 'vite';
1+
import { sveltekit } from "@sveltejs/kit/vite";
2+
import { defineConfig } from "vite";
33

44
export default defineConfig({
5-
plugins: [sveltekit()]
5+
plugins: [sveltekit()],
6+
server: {
7+
allowedHosts: ["7d90-156-34-178-107.ngrok-free.app"],
8+
},
69
});

0 commit comments

Comments
 (0)