49 lines
1.8 KiB
TypeScript
49 lines
1.8 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'
|
|
|
|
/**
|
|
* 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)
|
|
|
|
return logPaymentResult(result, articleId, recipientPubkey, amount)
|
|
}
|
|
|
|
return logPaymentResult(result, articleId, recipientPubkey, amount)
|
|
}
|