diff --git a/frontend/style/components/checkout-form.tsx b/frontend/style/components/checkout-form.tsx index 8283c678..5caff257 100644 --- a/frontend/style/components/checkout-form.tsx +++ b/frontend/style/components/checkout-form.tsx @@ -1,61 +1,91 @@ "use client" +import type React from "react" + import { useState } from "react" import { useRouter } from "next/navigation" import { useCart } from "@/contexts/cart-context" import { Button } from "@/components/ui/button" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" -import { CheckCircle2 } from "lucide-react" +import { CheckCircle2, ExternalLink } from "lucide-react" -// Mock function for YooMoney payment -const processYooMoneyPayment = async (amount: number): Promise => { - // Simulate API call +// Mock function for generating YooMoney payment link +const generateYooMoneyPaymentLink = async (amount: number): Promise => { + // Simulate API call to generate payment link + await new Promise((resolve) => setTimeout(resolve, 1000)) + // In a real application, this would be an actual API call to YooMoney + return `https://yoomoney.ru/checkout/payments/v2/contract?orderId=${Date.now()}&amount=${amount}` +} + +// Mock function for verifying YooMoney payment +const verifyYooMoneyPayment = async (): Promise => { + // Simulate API call to verify payment await new Promise((resolve) => setTimeout(resolve, 2000)) - // Simulate successful payment (in real app, this would be the actual API response) + // In a real application, this would check the actual payment status return true } export function CheckoutForm() { - const [step, setStep] = useState(1) const [address, setAddress] = useState("") const [isProcessing, setIsProcessing] = useState(false) const [isOrderPlaced, setIsOrderPlaced] = useState(false) + const [step, setStep] = useState(0) + const [paymentLink, setPaymentLink] = useState("") - const { items, clearCart } = useCart() const router = useRouter() + const { items, clearCart, getTotalItems } = useCart() - // Log the cart items to make sure they are correct - console.log("Cart items:", items) - - // Define the getTotalPrice function inside the component - const getTotalPrice = () => { - return items.reduce((total, item) => total + item.price * item.quantity, 0).toFixed(2) - } + const totalPrice = items.reduce((sum, item) => sum + item.price * item.quantity, 0) const handleAddressSubmit = (e: React.FormEvent) => { e.preventDefault() if (address.trim()) { - setStep(2) + setStep(1) } } - const handlePayment = async () => { + const handleInitiatePayment = async () => { setIsProcessing(true) - const totalPrice = getTotalPrice() // Calculate total price here - console.log("Total Price:", totalPrice) // Log the total price to check - - const paymentSuccess = await processYooMoneyPayment(Number(totalPrice)) - - if (paymentSuccess) { - setIsOrderPlaced(true) - clearCart() - } else { - alert("Оплата не прошла. Пожалуйста, попробуйте еще раз.") + try { + const link = await generateYooMoneyPaymentLink(totalPrice) + setPaymentLink(link) + setStep(2) + } catch (error) { + console.error("Error generating payment link:", error) + alert("Не удалось создать ссылку для оплаты. Пожалуйста, попробуйте еще раз.") } setIsProcessing(false) } + const handlePaymentConfirmation = async () => { + setIsProcessing(true) + try { + const paymentSuccess = await verifyYooMoneyPayment() + if (paymentSuccess) { + clearCart() // Clear the cart after successful payment + setIsOrderPlaced(true) + } else { + alert("Оплата не подтверждена. Пожалуйста, убедитесь, что вы завершили оплату.") + } + } catch (error) { + console.error("Error verifying payment:", error) + alert("Не удалось проверить статус оплаты. Пожалуйста, попробуйте еще раз.") + } + setIsProcessing(false) + } + + if (getTotalItems() === 0) { + return ( +
+

Ваша корзина пуста. Добавьте товары перед оформлением заказа.

+ +
+ ) + } + if (isOrderPlaced) { return (
@@ -68,10 +98,10 @@ export function CheckoutForm() { } return ( -
- {step === 1 && ( +
+ {step === 0 && (
-

Шаг 1: Адрес доставки

+

Адрес доставки