Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions components/Cart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import toast from 'react-hot-toast';

import { useStateContext } from '../context/StateContext';
import { urlFor } from '../lib/client';
import getStripe from '../lib/getStripe';

const Cart = () => {
const cartRef = useRef();
const { totalPrice, totalQuantities, cartItems, setShowCart, toggleCartItemQuanitity, onRemove } = useStateContext();

const handleCheckout = async () => {
const stripe = await getStripe();

const response = await fetch('/api/stripe', {
method: 'POST',
headers: {
Expand All @@ -29,7 +26,7 @@ const Cart = () => {

toast.loading('Redirecting...');

stripe.redirectToCheckout({ sessionId: data.id });
window.location.href = data.url;
}

return (
Expand Down Expand Up @@ -109,4 +106,4 @@ const Cart = () => {
)
}

export default Cart
export default Cart
103 changes: 45 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"@babel/core": "^7.17.9",
"@sanity/client": "^3.2.0",
"@sanity/image-url": "^1.0.1",
"@stripe/stripe-js": "^1.25.0",
"@stripe/stripe-js": "^9.2.0",
"canvas-confetti": "^1.5.1",
"next": "12.1.0",
"next-sanity-image": "^3.2.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-hot-toast": "^2.2.0",
"react-icons": "^4.3.1",
"stripe": "^8.209.0"
"stripe": "^22.0.1"
},
"devDependencies": {
"@babel/preset-react": "^7.16.7",
Expand Down
10 changes: 4 additions & 6 deletions pages/api/stripe.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import Stripe from 'stripe';

const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY);
const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY, {
apiVersion: '2026-03-25.dahlia',
});

export default async function handler(req, res) {
if (req.method === 'POST') {
try {
const params = {
submit_type: 'pay',
mode: 'payment',
payment_method_types: ['card'],
billing_address_collection: 'auto',
shipping_options: [
{ shipping_rate: 'shr_1Kn3IaEnylLNWUqj5rqhg9oV' },
],
line_items: req.body.map((item) => {
const img = item.image[0].asset._ref;
const newImage = img.replace('image-', 'https://cdn.sanity.io/images/vfxfwnaw/production/').replace('-webp', '.webp');
Expand Down Expand Up @@ -48,4 +46,4 @@ export default async function handler(req, res) {
res.setHeader('Allow', 'POST');
res.status(405).end('Method Not Allowed');
}
}
}