70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
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(
|
|
params: {
|
|
articleId: string
|
|
articlePubkey: string
|
|
amount: number
|
|
recipientPubkey: string
|
|
interval: number
|
|
deadline: number
|
|
}
|
|
): Promise<boolean> {
|
|
try {
|
|
const zapReceiptExists = await nostrService.checkZapReceipt(params.articlePubkey, params.articleId, params.amount, params.recipientPubkey)
|
|
if (zapReceiptExists) {
|
|
const zapReceiptId = await getZapReceiptId(params.articlePubkey, params.articleId, params.amount, params.recipientPubkey)
|
|
await sendPrivateContentAfterPayment(params.articleId, params.recipientPubkey, params.amount, zapReceiptId)
|
|
return true
|
|
}
|
|
} catch (error) {
|
|
console.error('Error checking zap receipt:', error)
|
|
}
|
|
|
|
if (Date.now() > params.deadline) {
|
|
return false
|
|
}
|
|
|
|
return new Promise<boolean>((resolve) => {
|
|
setTimeout(() => {
|
|
void pollPaymentUntilDeadline(params)
|
|
.then(resolve)
|
|
.catch(() => resolve(false))
|
|
}, params.interval)
|
|
})
|
|
}
|
|
|
|
export async function waitForArticlePayment(
|
|
params: {
|
|
paymentHash: string
|
|
articleId: string
|
|
articlePubkey: string
|
|
amount: number
|
|
recipientPubkey: string
|
|
timeout?: number
|
|
}
|
|
): Promise<boolean> {
|
|
const interval = 2000
|
|
const timeout = params.timeout ?? 300000
|
|
const deadline = Date.now() + timeout
|
|
try {
|
|
return pollPaymentUntilDeadline({
|
|
articleId: params.articleId,
|
|
articlePubkey: params.articlePubkey,
|
|
amount: params.amount,
|
|
recipientPubkey: params.recipientPubkey,
|
|
interval,
|
|
deadline,
|
|
})
|
|
} catch (error) {
|
|
console.error('Wait for payment error:', error)
|
|
return false
|
|
}
|
|
}
|