-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add button to get more cshots (#19)
- Loading branch information
Showing
12 changed files
with
271 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- CreateTable | ||
CREATE TABLE "payments" ( | ||
"id" TEXT NOT NULL, | ||
"type" TEXT NOT NULL, | ||
"status" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"projectId" TEXT, | ||
|
||
CONSTRAINT "payments_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "payments" ADD CONSTRAINT "payments_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20221216182142_stripe_session/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,8 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `stripeSessionId` to the `payments` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "payments" ADD COLUMN "stripeSessionId" TEXT NOT NULL; |
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import { | |
HStack, | ||
Icon, | ||
IconButton, | ||
Popover, | ||
Text, | ||
Tooltip, | ||
} from "@chakra-ui/react"; | ||
|
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,104 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { | ||
Menu, | ||
MenuButton, | ||
MenuList, | ||
MenuItem, | ||
Button, | ||
HStack, | ||
Text, | ||
} from "@chakra-ui/react"; | ||
import { useRouter } from "next/router"; | ||
import { useQuery } from "react-query"; | ||
import axios from "axios"; | ||
import { BsChevronDown } from "react-icons/bs"; | ||
import { IoIosFlash } from "react-icons/io"; | ||
|
||
const BuyShotButton = ({ | ||
credits, | ||
onPaymentSuccess, | ||
}: { | ||
credits: number; | ||
onPaymentSuccess: (credits: number) => void; | ||
}) => { | ||
const { push, query } = useRouter(); | ||
const [waitingPayment, setWaitingPayment] = useState(false); | ||
|
||
const { isLoading } = useQuery( | ||
"check-shot-payment", | ||
() => | ||
axios.get(`/api/checkout/check/${query.ppi}/${query.session_id}/shot`), | ||
{ | ||
cacheTime: 0, | ||
refetchInterval: 4, | ||
retry: 0, | ||
enabled: waitingPayment, | ||
onSuccess: (response) => { | ||
onPaymentSuccess(response.data.credits); | ||
}, | ||
onSettled: () => { | ||
setWaitingPayment(false); | ||
}, | ||
} | ||
); | ||
|
||
useEffect(() => { | ||
setWaitingPayment(query.ppi === query.id); | ||
}, [query]); | ||
|
||
const handleShotPayment = (quantity: number) => { | ||
push(`/api/checkout/shots?quantity=${quantity}&ppi=${query.id}`); | ||
}; | ||
|
||
return ( | ||
<Menu> | ||
<MenuButton | ||
rightIcon={<BsChevronDown />} | ||
isLoading={isLoading} | ||
size="xs" | ||
shadow="none" | ||
variant="brand" | ||
as={Button} | ||
> | ||
<HStack spacing={0}> | ||
<IoIosFlash /> | ||
{credits === 0 ? ( | ||
<Text>Buy more shots</Text> | ||
) : ( | ||
<Text> | ||
{credits} Shot{credits > 1 && "s"} left | ||
</Text> | ||
)} | ||
</HStack> | ||
</MenuButton> | ||
<MenuList fontSize="sm"> | ||
<MenuItem | ||
command="$4" | ||
onClick={() => { | ||
handleShotPayment(100); | ||
}} | ||
> | ||
Add <b>100 shots</b> | ||
</MenuItem> | ||
<MenuItem | ||
command="$7" | ||
onClick={() => { | ||
handleShotPayment(200); | ||
}} | ||
> | ||
Add <b>200 shots</b> | ||
</MenuItem> | ||
<MenuItem | ||
command="$9" | ||
onClick={() => { | ||
handleShotPayment(300); | ||
}} | ||
> | ||
Add <b>300 shots</b> | ||
</MenuItem> | ||
</MenuList> | ||
</Menu> | ||
); | ||
}; | ||
|
||
export default BuyShotButton; |
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,56 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import Stripe from "stripe"; | ||
import db from "@/core/db"; | ||
|
||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { | ||
apiVersion: "2022-11-15", | ||
}); | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const sessionId = req.query.sessionId as string; | ||
const ppi = req.query.ppi as string; | ||
|
||
const session = await stripe.checkout.sessions.retrieve(sessionId); | ||
|
||
const payments = await db.payment.findMany({ | ||
where: { | ||
stripeSessionId: sessionId, | ||
projectId: ppi, | ||
status: "paid", | ||
type: "credits", | ||
}, | ||
}); | ||
|
||
if (payments.length > 0) { | ||
return res | ||
.status(400) | ||
.json({ success: false, error: "payment_already_processed" }); | ||
} | ||
|
||
if ( | ||
session.payment_status === "paid" && | ||
session.metadata?.projectId === ppi | ||
) { | ||
const quantity = Number(session.metadata?.quantity); | ||
const project = await db.project.update({ | ||
where: { id: ppi }, | ||
data: { credits: { increment: quantity } }, | ||
}); | ||
|
||
await db.payment.create({ | ||
data: { | ||
status: "paid", | ||
projectId: ppi, | ||
type: "credits", | ||
stripeSessionId: sessionId, | ||
}, | ||
}); | ||
|
||
return res.status(200).json({ success: true, credits: project.credits }); | ||
} | ||
|
||
return res.status(400).json({ success: false }); | ||
} |
File renamed without changes.
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,49 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import Stripe from "stripe"; | ||
|
||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { | ||
apiVersion: "2022-11-15", | ||
}); | ||
|
||
const PRICES = { 100: 400, 200: 700, 300: 900 }; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const quantity = Number(req.query.quantity); | ||
const ppi = req.query.ppi; | ||
|
||
if (quantity !== 100 && quantity !== 200 && quantity !== 300) { | ||
return res.status(400).json("invalid_quantity"); | ||
} | ||
|
||
try { | ||
const session = await stripe.checkout.sessions.create({ | ||
allow_promotion_codes: true, | ||
metadata: { | ||
projectId: req.query.ppi as string, | ||
quantity, | ||
}, | ||
line_items: [ | ||
{ | ||
price_data: { | ||
currency: "usd", | ||
unit_amount: PRICES[quantity], | ||
product_data: { | ||
name: `⚡️ Refill +${quantity} shots`, | ||
}, | ||
}, | ||
quantity: 1, | ||
}, | ||
], | ||
mode: "payment", | ||
success_url: `${process.env.NEXTAUTH_URL}/studio/${ppi}/?session_id={CHECKOUT_SESSION_ID}&ppi=${ppi}`, | ||
cancel_url: `${process.env.NEXTAUTH_URL}/studio/${ppi}`, | ||
}); | ||
|
||
return res.redirect(303, session.url!); | ||
} catch (err: any) { | ||
return res.status(400).json(err.message); | ||
} | ||
} |
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.
c3b6853
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
photoshot – ./
photoshot-git-main-shinework.vercel.app
photoshot-shinework.vercel.app
photoshot.app
www.photoshot.app
photoshot.vercel.app