Skip to content

Commit

Permalink
update stripe webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Spoutnikrs committed May 27, 2024
1 parent 54961bb commit 3225764
Show file tree
Hide file tree
Showing 8 changed files with 815 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .env.exemple
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ KINDE_POST_LOGIN_REDIRECT_URL=
UPLOADTHING_SECRET=
UPLOADTHING_APP_ID=

STRIPE_SECRET_KEY=
STRIPE_CONNECT_WEBHOOK_SECRET=

43 changes: 43 additions & 0 deletions app/api/stripe/connect/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import db from "@/lib/ts/db";

Check failure on line 1 in app/api/stripe/connect/route.ts

View workflow job for this annotation

GitHub Actions / Qodana for JS

ESLint

ESLint: Install the 'eslint' package
import { stripe } from "@/lib/ts/stripe";
import { headers } from "next/headers";

export async function POST(req: Request) {
const body = await req.text();

const signature = headers().get("Stripe-Signature") as string;

let event;

try {
event = stripe.webhooks.constructEvent(
body,
signature,
process.env.STRIPE_CONNECT_WEBHOOK_SECRET as string
);
} catch (error: unknown) {
return new Response("webhook error", { status: 400 });
}

switch (event.type) {
case "account.updated": {
const account = event.data.object;

const data = await db.user.update({

Check warning on line 26 in app/api/stripe/connect/route.ts

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused constant data
where: {
connectedAccountId: account.id,
},
data: {
stripeConnectedLinked:
!(account.capabilities?.transfers === "pending" || account.capabilities?.transfers === "inactive"),
},
});
break;
}
default: {
console.log("default event");
}
}

return new Response(null, { status: 200 });
}
46 changes: 46 additions & 0 deletions app/api/stripe/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Email from "@/components/forms/ReturnEmail";

Check failure on line 1 in app/api/stripe/route.ts

View workflow job for this annotation

GitHub Actions / Qodana for JS

ESLint

ESLint: Install the 'eslint' package
import { stripe } from "@/lib/ts/stripe";
import { headers } from "next/headers";
import { Resend } from "resend";

const resend = new Resend(process.env.RESEND_API_KEY);

export async function POST(req: Request) {
const body = await req.text();

const signature = headers().get("Stripe-Signature") as string;

let event;

try {
event = stripe.webhooks.constructEvent(
body,
signature,
process.env.STRIPE_SECRET_WEBHOOK as string
);
} catch (error: unknown) {
return new Response("webhook error", { status: 400 });
}

switch (event.type) {
case "checkout.session.completed": {
const session = event.data.object;
const link = session.metadata?.link;
const { data, error } = await resend.emails.send({

Check warning on line 29 in app/api/stripe/route.ts

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused constant error

Check warning on line 29 in app/api/stripe/route.ts

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused constant data
from: "PixelCart",
to: [`${session.customer_email}`],
subject: "Your Product from PixelCart is here!",
react: Email({
link: link as string,
}),
});

break;
}
default: {
console.log("unhandled event");
}
}

return new Response(null, { status: 200 });
}
26 changes: 23 additions & 3 deletions app/inventory/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import React from "react";

Check failure on line 1 in app/inventory/page.tsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

ESLint

ESLint: Install the 'eslint' package
import db from "@/lib/ts/db";
import { redirect } from "next/navigation";
import { Card } from "@/components/ui/card";
import React from "react";
import { unstable_noStore as noStore } from "next/cache";
import InventoryForm from "@/components/forms/InventoryForm";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import { unstable_noStore as noStore } from "next/cache";


async function CheckStripeLink(userId: string) {
const data = await db.user.findUnique({
where: {
id: userId,
},
select: {
stripeConnectedLinked: true,
},
});

if (data?.stripeConnectedLinked === false) {
return redirect("/billing");
}

return null;
}


export default async function InventoryRoute() {
Expand All @@ -14,9 +33,10 @@ export default async function InventoryRoute() {

if (!user) {
return redirect("/");
// throw new Error("Unauthorized");
}

const data = await CheckStripeLink(user.id);

Check warning on line 38 in app/inventory/page.tsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused constant data

return (
<section className="max-w-7xl mx-auto px-4 md:px-8 mb-14">
<Card>
Expand Down
30 changes: 30 additions & 0 deletions app/return/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Button } from "@/components/ui/button";

Check failure on line 1 in app/return/[id]/page.tsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

ESLint

ESLint: Install the 'eslint' package
import { Card } from "@/components/ui/card";
import { Check } from "lucide-react";
import Link from "next/link";

export default function ReturnUrlStripe() {
return (
<section className="w-full min-h-[80vh] flex items-center justify-center">
<Card className="w-[350px]">
<div className="p-6">
<div className="w-full flex justify-center">
<Check className="w-12 h-12 rounded-full bg-green-500/30 text-green-500 p-2" />
</div>
<div className="mt-3 text-center sm:mt-5 w-full">
<h3 className="text-lg leading-6 font-medium">
Linking was Successful
</h3>
<p className="mt-2 text-sm text-muted-foreground">
Your Stripe account has been successfully linked.
</p>

<Button className="mt-5 sm:mt-6 w-full" asChild>
<Link href="/">Back to Homepage</Link>
</Button>
</div>
</div>
</Card>
</section>
);
}
46 changes: 46 additions & 0 deletions components/forms/ReturnEmail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {

Check failure on line 1 in components/forms/ReturnEmail.tsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

ESLint

ESLint: Install the 'eslint' package
Body,
Button,
Container,
Head,
Html,
Preview,
Section,
Tailwind,
Text,
} from "@react-email/components";

export default function Email({ link }: { link: string }) {
return (
<Html>
<Head />
<Preview>Your product is here...</Preview>
<Tailwind>
<Body className="bg-white font-sans">
<Container style={container}>
<Text className="text-2xl font-semibold">Hi Friend,</Text>
<Text className="text-lg text-gray-600">
Thank you for buying your product at MarshalUI
</Text>
<Section className="w-full flex justify-center mt-7">
<Button
href={link}
className="text-white bg-blue-500 rounded-lg px-10 py-4"
>
Your Download Link
</Button>
</Section>
<Text className="text-lg">
Best, <br /> MarshalUI Team
</Text>
</Container>
</Body>
</Tailwind>
</Html>
);
}

const container = {
margin: "0 auto",
padding: "20px 0 48px",
};
Loading

0 comments on commit 3225764

Please sign in to comment.