import React, { useState, useEffect } from "react"; import { PayPalButtons, PayPalScriptProvider } from "@paypal/react-paypal-js"; import axios from "axios"; import authAxios from "@/utils/axios"; const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours in milliseconds const EXCHANGE_RATE_KEY = "exchange_rate_cache"; const PaymentComponent = ({ amount, onSuccess }) => { const [error, setError] = useState(null); const [isProcessing, setIsProcessing] = useState(false); const [usdAmount, setUsdAmount] = useState(null); useEffect(() => { const fetchExchangeRate = async () => { try { const cachedData = localStorage.getItem(EXCHANGE_RATE_KEY); if (cachedData) { const { rate, timestamp } = JSON.parse(cachedData); if (Date.now() - timestamp < CACHE_DURATION) { setUsdAmount((amount * rate).toFixed(2)); return; } } const response = await axios.get("https://apilayer.net/api/live", { params: { access_key: "9bcb30907dee1cda9866f7b49f0f8def", currencies: "USD", source: "INR", format: 1, }, }); if (response.data.success) { const rate = response.data.quotes.INRUSD; localStorage.setItem( EXCHANGE_RATE_KEY, JSON.stringify({ rate, timestamp: Date.now(), }) ); setUsdAmount((amount * rate).toFixed(2)); } else { throw new Error("Failed to fetch exchange rate"); } } catch (err) { setError("Currency conversion failed. Please try again later."); console.error("Exchange rate error:", err); } }; fetchExchangeRate(); }, [amount]); const handleApprove = async (data, actions) => { try { setIsProcessing(true); const order = await actions.order.capture(); onSuccess?.(order); } catch (err) { setError("Payment failed. Please try again."); console.error("Payment error:", err); } finally { setIsProcessing(false); } }; if (!usdAmount) { return
Note: We only ship to addresses in India, Malaysia, and Nepal.