import { nostrService } from './nostr' import { getZapReceiptId } from './paymentPollingZapReceipt' import { sendPrivateContentAfterPayment } from './paymentPollingMain' /** * Poll for payment completion via zap receipt verification * After payment is confirmed, sends private content to the user */ async function pollPaymentUntilDeadline( articleId: string, articlePubkey: string, amount: number, recipientPubkey: string, interval: number, deadline: number ): Promise { try { const zapReceiptExists = await nostrService.checkZapReceipt(articlePubkey, articleId, amount, recipientPubkey) if (zapReceiptExists) { const zapReceiptId = await getZapReceiptId(articlePubkey, articleId, amount, recipientPubkey) await sendPrivateContentAfterPayment(articleId, recipientPubkey, amount, zapReceiptId) return true } } catch (error) { console.error('Error checking zap receipt:', error) } if (Date.now() > deadline) { return false } return new Promise((resolve) => { setTimeout(() => { void pollPaymentUntilDeadline(articleId, articlePubkey, amount, recipientPubkey, interval, deadline) .then(resolve) .catch(() => resolve(false)) }, interval) }) } export async function waitForArticlePayment( _paymentHash: string, articleId: string, articlePubkey: string, amount: number, recipientPubkey: string, timeout: number = 300000 // 5 minutes ): Promise { const interval = 2000 const deadline = Date.now() + timeout try { return await pollPaymentUntilDeadline(articleId, articlePubkey, amount, recipientPubkey, interval, deadline) } catch (error) { console.error('Wait for payment error:', error) return false } }