-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Stripe Payments and Orders dashboard 💵
- Loading branch information
1 parent
e4ae75f
commit e7e1a73
Showing
17 changed files
with
242 additions
and
348 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,27 @@ | ||
"use server"; | ||
|
||
import Order from "@/lib/models/Order"; | ||
import dbConnect from "@/utils/dbConnect"; | ||
|
||
export const createOrder = async (payload) => { | ||
await dbConnect(); | ||
try { | ||
const order = await Order.create(payload); | ||
if (order) { | ||
console.log(order); | ||
return { | ||
status: 200, | ||
message: "Order Created Successfully", | ||
data: order, | ||
}; | ||
} else { | ||
return { | ||
status: 401, | ||
message: "Some Error Occured", | ||
data: order, | ||
}; | ||
} | ||
} catch (error) { | ||
throw new Error(error); | ||
} | ||
}; |
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 @@ | ||
"use server"; | ||
import { auth } from "@/auth"; | ||
import Stripe from "stripe"; | ||
|
||
const stripe = new Stripe(process.env.NEXT_STRIPE_SECRET_KEY); | ||
|
||
export const createCheckout = async (data) => { | ||
const authSession = await auth(); | ||
if (!authSession) { | ||
return { | ||
status: 400, | ||
message: "Unauthorized", | ||
}; | ||
} | ||
const { productId, name, unitPrice, quantity, images } = data; | ||
console.log(data); | ||
const session = await stripe.checkout.sessions.create({ | ||
payment_method_types: ["card"], | ||
shipping_address_collection: { | ||
allowed_countries: ['US', 'CA'], // Add other allowed countries as needed | ||
}, | ||
metadata: { | ||
userId: authSession.user.id, | ||
productId, | ||
}, | ||
line_items: [ | ||
{ | ||
price_data: { | ||
currency: "usd", | ||
|
||
product_data: { | ||
name: name, | ||
images, | ||
}, | ||
unit_amount: unitPrice*100, // Price in cents | ||
}, | ||
quantity: quantity, | ||
}, | ||
], | ||
|
||
mode: "payment", | ||
success_url: `http:https://localhost:3000/success`, | ||
cancel_url: `http:https://localhost:3000/cancel`, | ||
}); | ||
return { | ||
status: 200, | ||
checkoutUrl: session.url, | ||
}; | ||
}; |
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,7 @@ | ||
import React from "react"; | ||
|
||
const CancelPage = () => { | ||
return <div>You Cancelled Product Purchase</div>; | ||
}; | ||
|
||
export default CancelPage; |
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
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,7 @@ | ||
import React from "react"; | ||
|
||
const SuccessPage = () => { | ||
return <div>Product Buyed Successfully</div>; | ||
}; | ||
|
||
export default SuccessPage; |
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 |
---|---|---|
@@ -1,89 +1,42 @@ | ||
"use client"; | ||
import { | ||
Table, | ||
TableBody, | ||
TableCaption, | ||
TableCell, | ||
TableFooter, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from "@/components/ui/table" | ||
|
||
const invoices = [ | ||
{ | ||
invoice: "INV001", | ||
paymentStatus: "Paid", | ||
totalAmount: "$250.00", | ||
paymentMethod: "Credit Card", | ||
}, | ||
{ | ||
invoice: "INV002", | ||
paymentStatus: "Pending", | ||
totalAmount: "$150.00", | ||
paymentMethod: "PayPal", | ||
}, | ||
{ | ||
invoice: "INV003", | ||
paymentStatus: "Unpaid", | ||
totalAmount: "$350.00", | ||
paymentMethod: "Bank Transfer", | ||
}, | ||
{ | ||
invoice: "INV004", | ||
paymentStatus: "Paid", | ||
totalAmount: "$450.00", | ||
paymentMethod: "Credit Card", | ||
}, | ||
{ | ||
invoice: "INV005", | ||
paymentStatus: "Paid", | ||
totalAmount: "$550.00", | ||
paymentMethod: "PayPal", | ||
}, | ||
{ | ||
invoice: "INV006", | ||
paymentStatus: "Pending", | ||
totalAmount: "$200.00", | ||
paymentMethod: "Bank Transfer", | ||
}, | ||
{ | ||
invoice: "INV007", | ||
paymentStatus: "Unpaid", | ||
totalAmount: "$300.00", | ||
paymentMethod: "Credit Card", | ||
}, | ||
] | ||
|
||
export function TableDemo() { | ||
return ( | ||
<Table> | ||
{/* <TableCaption>A list of your recent invoices.</TableCaption> */} | ||
<TableHeader> | ||
<TableRow> | ||
<TableHead className="w-[100px]">Invoice</TableHead> | ||
<TableHead>Status</TableHead> | ||
<TableHead>Method</TableHead> | ||
<TableHead className="text-right">Amount</TableHead> | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableFooter, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from "@/components/ui/table"; | ||
|
||
export function TableDemo({ data }) { | ||
return ( | ||
<Table> | ||
{/* <TableCaption>A list of your recent invoices.</TableCaption> */} | ||
<TableHeader> | ||
<TableRow> | ||
<TableHead className="w-[100px]">Order</TableHead> | ||
<TableHead>Status</TableHead> | ||
<TableHead>Product</TableHead> | ||
<TableHead className="text-right">Amount</TableHead> | ||
</TableRow> | ||
</TableHeader> | ||
<TableBody> | ||
{data.map((order) => ( | ||
<TableRow key={order._id}> | ||
<TableCell className="font-medium">{order._id}</TableCell> | ||
<TableCell> {order.pricePaid ? "YES" : "NO"}</TableCell> | ||
<TableCell>{order.productId}</TableCell> | ||
<TableCell className="text-right">{order.pricePaid}</TableCell> | ||
</TableRow> | ||
</TableHeader> | ||
<TableBody> | ||
{invoices.map((invoice) => ( | ||
<TableRow key={invoice.invoice}> | ||
<TableCell className="font-medium">{invoice.invoice}</TableCell> | ||
<TableCell>{invoice.paymentStatus}</TableCell> | ||
<TableCell>{invoice.paymentMethod}</TableCell> | ||
<TableCell className="text-right">{invoice.totalAmount}</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
<TableFooter> | ||
<TableRow> | ||
<TableCell colSpan={3}>Total</TableCell> | ||
<TableCell className="text-right">$2,500.00</TableCell> | ||
</TableRow> | ||
</TableFooter> | ||
</Table> | ||
) | ||
} | ||
|
||
))} | ||
</TableBody> | ||
<TableFooter> | ||
<TableRow> | ||
<TableCell colSpan={3}>Total</TableCell> | ||
<TableCell className="text-right">$2,500.00 etc..</TableCell> | ||
</TableRow> | ||
</TableFooter> | ||
</Table> | ||
); | ||
} |
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
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,20 @@ | ||
"use client"; | ||
|
||
import { createCheckout } from "@/actions/stripe/sessions.actions"; | ||
|
||
const BuyItemButton = ({ className, data }) => { | ||
const handleClick = async () => { | ||
const checkOutUrl = await createCheckout(data); | ||
window.open(checkOutUrl.checkoutUrl); | ||
}; | ||
return ( | ||
<button | ||
className={className + " px-4 p-3 text-md font-semibold "} | ||
onClick={handleClick} | ||
> | ||
Buy Now | ||
</button> | ||
); | ||
}; | ||
|
||
export default BuyItemButton; |
Oops, something went wrong.