diff --git a/src/app/(storefront)/checkout/payment/stripe.tsx b/src/app/(storefront)/checkout/payment/stripe.tsx deleted file mode 100644 index 610c4f9..0000000 --- a/src/app/(storefront)/checkout/payment/stripe.tsx +++ /dev/null @@ -1,101 +0,0 @@ -'use client'; // include with Next.js 13+ - -import { CardElement, Elements, useElements, useStripe } from '@stripe/react-stripe-js'; -import { loadStripe } from '@stripe/stripe-js'; -import { useState } from 'react'; - -import { useCart } from '../../providers/cart'; - -const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PK || 'temp'); - -export default function StripePayment() { - const { cart } = useCart(); - const clientSecret = cart?.payment_collection?.payment_sessions?.[0].data.client_secret as string; - - return ( -
- - - -
- ); -} - -const StripeForm = ({ clientSecret }: { clientSecret: string | undefined }) => { - const { cart, refreshCart } = useCart(); - const [loading, setLoading] = useState(false); - - const stripe = useStripe(); - const elements = useElements(); - - async function handlePayment(e: React.MouseEvent) { - e.preventDefault(); - const card = elements?.getElement(CardElement); - - if (!stripe || !elements || !card || !cart || !clientSecret) { - return; - } - - setLoading(true); - stripe - ?.confirmCardPayment(clientSecret, { - payment_method: { - card, - billing_details: { - name: cart.billing_address?.first_name, - email: cart.email, - phone: cart.billing_address?.phone, - address: { - city: cart.billing_address?.city, - country: cart.billing_address?.country_code, - line1: cart.billing_address?.address_1, - line2: cart.billing_address?.address_2, - postal_code: cart.billing_address?.postal_code, - }, - }, - }, - }) - .then(({ error }) => { - if (error) { - // TODO handle errors - console.error(error); - return; - } - - fetch(`http://localhost:9000/store/carts/${cart.id}/complete`, { - credentials: 'include', - headers: { - 'x-publishable-api-key': process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || 'temp', - }, - method: 'POST', - }) - .then((res) => res.json()) - .then(({ type, cart, order, error }) => { - if (type === 'cart' && cart) { - // an error occured - console.error(error); - } else if (type === 'order' && order) { - // TODO redirect to order success page - alert('Order placed.'); - console.log(order); - refreshCart(); - } - }); - }) - .finally(() => setLoading(false)); - } - - return ( -
- - - - ); -}; diff --git a/src/hooks/useInView.tsx b/src/hooks/useInView.tsx index 320fd9a..0a3b88c 100644 --- a/src/hooks/useInView.tsx +++ b/src/hooks/useInView.tsx @@ -7,7 +7,7 @@ interface UseInViewOptions extends IntersectionObserverInit { triggerOnce?: boolean; } -const useInView = (options: UseInViewOptions): [RefObject, boolean] => { +const useInView = (options: UseInViewOptions): [RefObject, boolean] => { const [isInView, setIsInView] = useState(false); const elementRef = useRef(null); const observerManager = useSharedIntersectionObserver(options);