2024-08-09 19:00:31 -05:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import dynamic from 'next/dynamic';
|
2024-08-17 16:29:37 -05:00
|
|
|
import { Dialog } from 'primereact/dialog';
|
2024-08-09 19:00:31 -05:00
|
|
|
import { initializeBitcoinConnect } from './BitcoinConnect';
|
|
|
|
import { LightningAddress } from '@getalby/lightning-tools';
|
|
|
|
import { useToast } from '@/hooks/useToast';
|
2024-08-12 17:27:47 -05:00
|
|
|
import { useSession } from 'next-auth/react';
|
2024-08-17 16:29:37 -05:00
|
|
|
import axios from 'axios';
|
2024-09-10 15:44:08 -05:00
|
|
|
import GenericButton from '@/components/buttons/GenericButton';
|
2024-08-09 19:00:31 -05:00
|
|
|
|
2024-08-17 16:29:37 -05:00
|
|
|
const Payment = dynamic(
|
|
|
|
() => import('@getalby/bitcoin-connect-react').then((mod) => mod.Payment),
|
|
|
|
{ ssr: false }
|
2024-08-09 19:00:31 -05:00
|
|
|
);
|
|
|
|
|
2024-08-12 17:27:47 -05:00
|
|
|
const ResourcePaymentButton = ({ lnAddress, amount, onSuccess, onError, resourceId }) => {
|
2024-08-09 19:00:31 -05:00
|
|
|
const [invoice, setInvoice] = useState(null);
|
2024-08-12 17:27:47 -05:00
|
|
|
const [userId, setUserId] = useState(null);
|
2024-08-09 19:00:31 -05:00
|
|
|
const { showToast } = useToast();
|
2024-08-12 17:27:47 -05:00
|
|
|
const { data: session } = useSession();
|
2024-08-17 16:29:37 -05:00
|
|
|
const [dialogVisible, setDialogVisible] = useState(false);
|
2024-08-12 17:27:47 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (session?.user) {
|
|
|
|
setUserId(session.user.id);
|
|
|
|
}
|
|
|
|
}, [session]);
|
2024-08-09 19:00:31 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
initializeBitcoinConnect();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchInvoice = async () => {
|
|
|
|
try {
|
|
|
|
const ln = new LightningAddress(lnAddress);
|
|
|
|
await ln.fetch();
|
|
|
|
const invoice = await ln.requestInvoice({ satoshi: amount });
|
|
|
|
setInvoice(invoice);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching invoice:', error);
|
|
|
|
showToast('error', 'Invoice Error', 'Failed to fetch the invoice.');
|
|
|
|
if (onError) onError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchInvoice();
|
|
|
|
}, [lnAddress, amount, onError, showToast]);
|
|
|
|
|
|
|
|
const handlePaymentSuccess = async (response) => {
|
2024-08-11 19:10:08 -05:00
|
|
|
try {
|
|
|
|
const purchaseData = {
|
|
|
|
userId: userId,
|
|
|
|
resourceId: resourceId,
|
2024-08-17 16:29:37 -05:00
|
|
|
amountPaid: parseInt(amount, 10)
|
2024-08-11 19:10:08 -05:00
|
|
|
};
|
|
|
|
|
2024-08-12 17:27:47 -05:00
|
|
|
const result = await axios.post('/api/purchase/resource', purchaseData);
|
2024-08-11 19:10:08 -05:00
|
|
|
|
|
|
|
if (result.status === 200) {
|
|
|
|
showToast('success', 'Payment Successful', `Paid ${amount} sats and updated user purchases`);
|
|
|
|
if (onSuccess) onSuccess(response);
|
|
|
|
} else {
|
|
|
|
throw new Error('Failed to update user purchases');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error updating user purchases:', error);
|
|
|
|
showToast('error', 'Purchase Update Failed', 'Payment was successful, but failed to update user purchases.');
|
|
|
|
if (onError) onError(error);
|
|
|
|
}
|
2024-08-17 16:29:37 -05:00
|
|
|
setDialogVisible(false);
|
2024-08-09 19:00:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-08-17 16:29:37 -05:00
|
|
|
<>
|
2024-09-10 15:44:08 -05:00
|
|
|
<GenericButton
|
2024-08-18 14:45:51 -05:00
|
|
|
label={`${amount} sats`}
|
2024-08-17 16:29:37 -05:00
|
|
|
icon="pi pi-wallet"
|
|
|
|
onClick={() => setDialogVisible(true)}
|
|
|
|
disabled={!invoice}
|
2024-08-18 14:45:51 -05:00
|
|
|
severity='primary'
|
|
|
|
rounded
|
|
|
|
className="text-[#f8f8ff] text-sm"
|
2024-08-17 16:29:37 -05:00
|
|
|
/>
|
|
|
|
<Dialog
|
|
|
|
visible={dialogVisible}
|
|
|
|
onHide={() => setDialogVisible(false)}
|
|
|
|
header="Make Payment"
|
|
|
|
style={{ width: '50vw' }}
|
|
|
|
>
|
|
|
|
{invoice ? (
|
|
|
|
<Payment
|
|
|
|
invoice={invoice.paymentRequest}
|
|
|
|
onPaid={handlePaymentSuccess}
|
|
|
|
paymentMethods='all'
|
|
|
|
title={`Pay ${amount} sats`}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<p>Loading payment details...</p>
|
|
|
|
)}
|
|
|
|
</Dialog>
|
|
|
|
</>
|
2024-08-09 19:00:31 -05:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-08-17 16:29:37 -05:00
|
|
|
export default ResourcePaymentButton;
|