Skip to content
This repository was archived by the owner on Oct 29, 2025. It is now read-only.

Commit f0fb1fa

Browse files
committed
Added a fetch method that is called when a user subscribes
1 parent 40a1a9e commit f0fb1fa

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ import { config } from "dotenv";
66
config();
77

88
export async function POST({ request, locals }) {
9+
console.log("[server] Someone just subscribed to a plan.");
910
if (!process.env.STRIPE_SECRET_KEY || !process.env.STRIPE_WEBHOOK_SECRET) {
1011
console.error("STRIPE_SECRET_KEY or STRIPE_WEBHOOK_SECRET env var is not set.");
1112
return json({msg: "not ok"}, { status: 500 });
1213
}
1314

1415
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
1516
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
16-
console.log("hello");
1717
const body = await request.text();
18-
console.log("yellow");
1918
const signature = request.headers.get('stripe-signature') || "";
2019

2120
let event;
22-
23-
console.log({ body, signature, secret: process.env.STRIPE_WEBHOOK_SECRET})
2421
try {
2522
event = stripe.webhooks.constructEvent(body, signature, webhookSecret);
2623
} catch (err) {
@@ -67,6 +64,24 @@ export async function POST({ request, locals }) {
6764
}
6865
});
6966

67+
const userSubscribedResp = await fetch(process.env.VITE_PB_URL + "/userSubscribed", {
68+
method: "POST",
69+
headers: {
70+
"Content-Type": "application/json"
71+
},
72+
body: JSON.stringify({
73+
"accessToken": user.authToken,
74+
"id": user.id
75+
})
76+
});
77+
78+
if (userSubscribedResp.ok) {
79+
const userSubscribedRespJSON = await userSubscribedResp.json();
80+
console.log(userSubscribedRespJSON);
81+
} else {
82+
console.log(userSubscribedResp.status);
83+
}
84+
7085
break
7186
}
7287
case "customer.subscription.deleted": {
@@ -93,7 +108,7 @@ export async function POST({ request, locals }) {
93108
}
94109
}
95110
} catch (err) {
96-
111+
console.log("[server] Error in the user subscription endpoint.", err);
97112
}
98113

99114
return json({msg: "ok"});

0 commit comments

Comments
 (0)