forked from djyde/cusdis
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
9 changed files
with
353 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
Box, | ||
Container, | ||
Flex, | ||
Link, | ||
Menu, | ||
MenuButton, | ||
MenuItem, | ||
MenuList, | ||
Spacer, | ||
} from "@chakra-ui/react" | ||
import React from "react" | ||
import { signOut } from "next-auth/client" | ||
import { UserSession } from "../service" | ||
|
||
export function Navbar(props: { session: UserSession }) { | ||
return ( | ||
<Box py={4}> | ||
<Container maxWidth={"5xl"}> | ||
<Flex> | ||
<Box> | ||
<Link fontWeight="bold" fontSize="xl" href="/dashboard"> | ||
Cusdis | ||
</Link> | ||
</Box> | ||
<Spacer /> | ||
<Box> | ||
<Menu> | ||
<MenuButton>{props.session.user.name}</MenuButton> | ||
<MenuList> | ||
<MenuItem><Link width="100%" href="/user">Settings</Link></MenuItem> | ||
<MenuItem onClick={() => signOut()}>Logout</MenuItem> | ||
</MenuList> | ||
</Menu> | ||
</Box> | ||
</Flex> | ||
</Container> | ||
</Box> | ||
) | ||
} |
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,88 @@ | ||
import axios from 'axios'; | ||
import * as crypto from 'crypto' | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { Readable } from 'stream'; | ||
import { SubscriptionService } from '../../service/subscription.service'; | ||
import { getSession, prisma, resolvedConfig } from '../../utils.server'; | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; | ||
|
||
// Get raw body as string | ||
async function getRawBody(readable: Readable): Promise<Buffer> { | ||
const chunks = []; | ||
for await (const chunk of readable) { | ||
chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk); | ||
} | ||
return Buffer.concat(chunks); | ||
} | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
|
||
if (req.method === 'POST') { | ||
const rawBody = await getRawBody(req); | ||
const secret = resolvedConfig.checkout.lemonSecret; | ||
console.log(req.headers) | ||
const hmac = crypto.createHmac('sha256', secret); | ||
const digest = Buffer.from(hmac.update(rawBody).digest('hex'), 'utf8'); | ||
const signature = Buffer.from(req.headers['x-signature'] as string, 'utf8'); | ||
|
||
if (!crypto.timingSafeEqual(digest, signature)) { | ||
res.status(401).send('Invalid signature'); | ||
return | ||
} | ||
|
||
const data = JSON.parse(Buffer.from(rawBody).toString('utf8')); | ||
const subscriptionService = new SubscriptionService() | ||
const eventName = req.headers['x-event-name'] as string | ||
|
||
switch (eventName) { | ||
case 'subscription_created': | ||
case 'subscription_updated': { | ||
await subscriptionService.update(data) | ||
break | ||
} | ||
} | ||
|
||
res.json({ | ||
|
||
}) | ||
} else if (req.method === 'DELETE') { | ||
const session = await getSession(req) | ||
|
||
if (!session) { | ||
res.status(401).send('Unauthorized') | ||
return | ||
} | ||
|
||
const subscription = await prisma.subscription.findUnique({ | ||
where: { | ||
userId: session.uid | ||
}, | ||
}) | ||
|
||
if (!subscription) { | ||
res.status(404).send('Subscription not found') | ||
return | ||
} | ||
|
||
try { | ||
const response = await axios.delete(`https://api.lemonsqueezy.com/v1/subscriptions/${subscription.lemonSubscriptionId}`, { | ||
headers: { | ||
'Authorization': `Bearer ${resolvedConfig.checkout.lemonApiKey}`, | ||
'Content-Type': 'application/vnd.api+json', | ||
'Accept': "application/vnd.api+json" | ||
} | ||
}) | ||
} catch (e) { | ||
console.log(e.response.data, e.request) | ||
} | ||
|
||
res.json({ | ||
message: 'success' | ||
}) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
prisma/sqlite/migrations/20230713042856_subscription/migration.sql
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,17 @@ | ||
-- CreateTable | ||
CREATE TABLE "Subscription" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"userId" TEXT NOT NULL, | ||
"order_id" TEXT NOT NULL, | ||
"product_id" TEXT, | ||
"variant_id" TEXT, | ||
"customer_id" TEXT, | ||
"status" TEXT NOT NULL, | ||
"endsAt" DATETIME, | ||
"update_payment_method_url" TEXT, | ||
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
CONSTRAINT "Subscription_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Subscription_userId_key" ON "Subscription"("userId"); |
2 changes: 2 additions & 0 deletions
2
prisma/sqlite/migrations/20230713055831_subscription/migration.sql
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,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "Subscription" ADD COLUMN "lemon_subscription_id" TEXT; |
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
Oops, something went wrong.