diff --git a/src/components/bitcoinConnect/PaymentButton.js b/src/components/bitcoinConnect/PaymentButton.js index 67b658d..c5a3c62 100644 --- a/src/components/bitcoinConnect/PaymentButton.js +++ b/src/components/bitcoinConnect/PaymentButton.js @@ -3,6 +3,7 @@ import dynamic from 'next/dynamic'; import { initializeBitcoinConnect } from './BitcoinConnect'; import { LightningAddress } from '@getalby/lightning-tools'; import { useToast } from '@/hooks/useToast'; +import axios from 'axios'; // Import axios for API calls const PayButton = dynamic( () => import('@getalby/bitcoin-connect-react').then((mod) => mod.PayButton), @@ -11,7 +12,7 @@ const PayButton = dynamic( } ); -const PaymentButton = ({ lnAddress, amount, onSuccess, onError }) => { +const PaymentButton = ({ lnAddress, amount, onSuccess, onError, userId, resourceId }) => { const [invoice, setInvoice] = useState(null); const { showToast } = useToast(); const [pollingInterval, setPollingInterval] = useState(null); @@ -64,14 +65,31 @@ const PaymentButton = ({ lnAddress, amount, onSuccess, onError }) => { }; const handlePaymentSuccess = async (response) => { - stopPolling(); // Stop polling after success - - // Close the modal + stopPolling(); await closeModal(); - // After the modal is closed, show the success toast - showToast('success', 'Payment Successful', `Paid ${amount} sats`); - if (onSuccess) onSuccess(response); + try { + // Create a new purchase record + const purchaseData = { + userId: userId, + resourceId: resourceId, + amountPaid: parseInt(amount, 10) // Convert amount to integer + }; + + // Make an API call to add the purchase to the user + const result = await axios.post('/api/purchases', purchaseData); + + 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); + } }; const handlePaymentError = (error) => { diff --git a/src/db/models/userModels.js b/src/db/models/userModels.js index 70d000e..d6238c4 100644 --- a/src/db/models/userModels.js +++ b/src/db/models/userModels.js @@ -44,6 +44,20 @@ export const getUserByPubkey = async (pubkey) => { }); } +export const addPurchaseToUser = async (userId, purchaseData) => { + return await prisma.user.update({ + where: { id: userId }, + data: { + purchased: { + create: purchaseData + } + }, + include: { + purchased: true + } + }); + }; + export const createUser = async (data) => { return await prisma.user.create({ data, diff --git a/src/pages/api/purchases.js b/src/pages/api/purchases.js new file mode 100644 index 0000000..62fbcac --- /dev/null +++ b/src/pages/api/purchases.js @@ -0,0 +1,21 @@ +import { addPurchaseToUser } from "@/db/models/userModels"; + +export default async function handler(req, res) { + if (req.method === 'POST') { + try { + const { userId, resourceId, amountPaid } = req.body; + + const updatedUser = await addPurchaseToUser(userId, { + resourceId, + amountPaid: parseInt(amountPaid, 10) // Ensure amountPaid is an integer + }); + + res.status(200).json(updatedUser); + } catch (error) { + res.status(500).json({ error: error.message }); + } + } else { + res.setHeader('Allow', ['POST']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } +} \ No newline at end of file diff --git a/src/pages/details/[slug]/index.js b/src/pages/details/[slug]/index.js index 8274fbb..c805652 100644 --- a/src/pages/details/[slug]/index.js +++ b/src/pages/details/[slug]/index.js @@ -262,7 +262,15 @@ export default function Details() { className="w-[344px] h-[194px] object-cover object-top rounded-lg" />