diff --git a/src/components/bitcoinConnect/CoursePaymentButton.js b/src/components/bitcoinConnect/CoursePaymentButton.js index a8040d3..fdef804 100644 --- a/src/components/bitcoinConnect/CoursePaymentButton.js +++ b/src/components/bitcoinConnect/CoursePaymentButton.js @@ -10,12 +10,15 @@ import axios from 'axios'; import GenericButton from '@/components/buttons/GenericButton'; import { useRouter } from 'next/router'; import useWindowWidth from '@/hooks/useWindowWidth'; +import { InputText } from 'primereact/inputtext'; const Payment = dynamic( () => import('@getalby/bitcoin-connect-react').then((mod) => mod.Payment), { ssr: false } ); +const DISCOUNT_CODE = process.env.NEXT_PUBLIC_DISCOUNT_CODE; + const CoursePaymentButton = ({ lnAddress, amount, onSuccess, onError, courseId }) => { const [invoice, setInvoice] = useState(null); const [isLoading, setIsLoading] = useState(false); @@ -25,6 +28,9 @@ const CoursePaymentButton = ({ lnAddress, amount, onSuccess, onError, courseId } const router = useRouter(); const windowWidth = useWindowWidth(); const isMobile = windowWidth < 768; + const [discountCode, setDiscountCode] = useState(''); + const [discountApplied, setDiscountApplied] = useState(false); + const [showDiscountInput, setShowDiscountInput] = useState(false); useEffect(() => { let intervalId; @@ -49,12 +55,21 @@ const CoursePaymentButton = ({ lnAddress, amount, onSuccess, onError, courseId } }; }, [invoice]); + const calculateDiscount = (originalAmount) => { + if (discountCode === DISCOUNT_CODE) { + const discountedAmount = 21000; + const savedPercentage = Math.round(((originalAmount - discountedAmount) / originalAmount) * 100); + return { discountedAmount, savedPercentage }; + } + return { discountedAmount: originalAmount, savedPercentage: 0 }; + }; + const fetchInvoice = async () => { setIsLoading(true); try { const ln = new LightningAddress(lnAddress); await ln.fetch(); - const invoice = await ln.requestInvoice({ satoshi: amount }); + const invoice = await ln.requestInvoice({ satoshi: discountApplied ? calculateDiscount(amount).discountedAmount : amount }); setInvoice(invoice); setDialogVisible(true); } catch (error) { @@ -70,7 +85,7 @@ const CoursePaymentButton = ({ lnAddress, amount, onSuccess, onError, courseId } const purchaseData = { userId: session.user.id, courseId: courseId, - amountPaid: parseInt(amount, 10) + amountPaid: discountApplied ? calculateDiscount(amount).discountedAmount : parseInt(amount, 10) }; const result = await axios.post('/api/purchase/course', purchaseData); @@ -89,10 +104,64 @@ const CoursePaymentButton = ({ lnAddress, amount, onSuccess, onError, courseId } setDialogVisible(false); }; + const handleDiscountCode = (value) => { + setDiscountCode(value); + if (value.toLowerCase() === DISCOUNT_CODE.toLowerCase()) { + setDiscountApplied(true); + showToast('success', 'Discount Applied', `${calculateDiscount(amount).savedPercentage}% discount applied!`); + } else if (value && value.toLowerCase() !== DISCOUNT_CODE.toLowerCase()) { + setDiscountApplied(false); + } + }; + return ( - <> +
Loading payment details...
)} - > +