87 lines
3.1 KiB
TypeScript
87 lines
3.1 KiB
TypeScript
import { getStoredPrivateContent } from './articleStorage'
|
|
import { nostrService } from './nostr'
|
|
import { articlePublisher } from './articlePublisher'
|
|
import { validatePaymentData, verifyPaymentAmount } from './paymentPollingValidation'
|
|
import { createTrackingData, triggerAutomaticTransfer, logPaymentResult } from './paymentPollingTracking'
|
|
import { platformTracking } from './platformTracking'
|
|
import { publishPurchaseNote } from './paymentNotes'
|
|
|
|
/**
|
|
* Send private content to user after payment confirmation
|
|
* Returns true if content was successfully sent and verified
|
|
*/
|
|
export async function sendPrivateContentAfterPayment(
|
|
articleId: string,
|
|
recipientPubkey: string,
|
|
amount: number,
|
|
zapReceiptId?: string
|
|
): Promise<boolean> {
|
|
const storedContent = await getStoredPrivateContent(articleId)
|
|
const authorPrivateKey = nostrService.getPrivateKey()
|
|
|
|
const validation = validatePaymentData(storedContent, authorPrivateKey, articleId, recipientPubkey)
|
|
if (!validation.valid || !validation.storedContent || !validation.authorPrivateKey) {
|
|
return false
|
|
}
|
|
|
|
const result = await articlePublisher.sendPrivateContent(articleId, recipientPubkey, validation.authorPrivateKey)
|
|
|
|
if (result.success && result.messageEventId) {
|
|
verifyPaymentAmount(amount, articleId)
|
|
|
|
const trackingData = createTrackingData(
|
|
articleId,
|
|
validation.storedContent.authorPubkey,
|
|
recipientPubkey,
|
|
result.messageEventId,
|
|
amount,
|
|
result.verified ?? false,
|
|
zapReceiptId
|
|
)
|
|
|
|
await platformTracking.trackContentDelivery(trackingData, validation.authorPrivateKey)
|
|
await triggerAutomaticTransfer(validation.storedContent.authorPubkey, articleId, amount)
|
|
|
|
// Publish explicit payment note (kind 1) with project tags
|
|
try {
|
|
const article = await nostrService.getArticleById(articleId)
|
|
if (article) {
|
|
const payerPrivateKey = nostrService.getPrivateKey()
|
|
if (payerPrivateKey) {
|
|
// Get zap receipt to extract payment hash and other info
|
|
let paymentHash = article.paymentHash ?? articleId
|
|
if (zapReceiptId) {
|
|
const { getZapReceiptById } = await import('./zapReceiptQueries')
|
|
const zapReceipt = await getZapReceiptById(zapReceiptId)
|
|
if (zapReceipt) {
|
|
const paymentHashTag = zapReceipt.tags.find((tag) => tag[0] === 'payment_hash')?.[1]
|
|
if (paymentHashTag) {
|
|
paymentHash = paymentHashTag
|
|
}
|
|
}
|
|
}
|
|
|
|
await publishPurchaseNote({
|
|
articleId: article.id,
|
|
authorPubkey: article.pubkey,
|
|
payerPubkey: recipientPubkey,
|
|
amount,
|
|
paymentHash,
|
|
zapReceiptId,
|
|
category: article.category,
|
|
seriesId: article.seriesId,
|
|
payerPrivateKey,
|
|
})
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error publishing purchase note:', error)
|
|
// Don't fail the payment if note publication fails
|
|
}
|
|
|
|
return logPaymentResult(result, articleId, recipientPubkey, amount)
|
|
}
|
|
|
|
return logPaymentResult(result, articleId, recipientPubkey, amount)
|
|
}
|