129 lines
3.5 KiB
TypeScript
129 lines
3.5 KiB
TypeScript
import { calculateArticleSplit } from './platformCommissions'
|
|
import { lightningAddressService } from './lightningAddress'
|
|
import { automaticTransferService } from './automaticTransfer'
|
|
|
|
export function createTrackingData(
|
|
articleId: string,
|
|
authorPubkey: string,
|
|
recipientPubkey: string,
|
|
messageEventId: string,
|
|
amount: number,
|
|
verified: boolean,
|
|
zapReceiptId?: string
|
|
): import('./platformTracking').ContentDeliveryTracking {
|
|
const expectedSplit = calculateArticleSplit()
|
|
const timestamp = Math.floor(Date.now() / 1000)
|
|
|
|
const trackingData: import('./platformTracking').ContentDeliveryTracking = {
|
|
articleId,
|
|
articlePubkey: authorPubkey,
|
|
recipientPubkey,
|
|
messageEventId,
|
|
amount,
|
|
authorAmount: expectedSplit.author,
|
|
platformCommission: expectedSplit.platform,
|
|
timestamp,
|
|
verified,
|
|
}
|
|
|
|
if (zapReceiptId) {
|
|
trackingData.zapReceiptId = zapReceiptId
|
|
}
|
|
|
|
return trackingData
|
|
}
|
|
|
|
export async function triggerAutomaticTransfer(
|
|
authorPubkey: string,
|
|
articleId: string,
|
|
amount: number
|
|
): Promise<void> {
|
|
try {
|
|
const authorLightningAddress = await lightningAddressService.getLightningAddress(authorPubkey)
|
|
|
|
if (authorLightningAddress) {
|
|
const transferResult = await automaticTransferService.transferAuthorPortion(
|
|
authorLightningAddress,
|
|
articleId,
|
|
authorPubkey,
|
|
amount
|
|
)
|
|
|
|
if (!transferResult.success) {
|
|
console.warn('Automatic transfer failed, will be retried later', {
|
|
articleId,
|
|
authorPubkey,
|
|
error: transferResult.error,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
}
|
|
} else {
|
|
console.warn('Author Lightning address not available for automatic transfer', {
|
|
articleId,
|
|
authorPubkey,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
}
|
|
} catch (error) {
|
|
console.error('Error triggering automatic transfer', {
|
|
articleId,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
}
|
|
}
|
|
|
|
export function logPaymentSuccess(
|
|
articleId: string,
|
|
recipientPubkey: string,
|
|
amount: number,
|
|
messageEventId: string,
|
|
verified: boolean
|
|
): void {
|
|
const expectedSplit = calculateArticleSplit()
|
|
console.log('Article payment processed with commission', {
|
|
articleId,
|
|
totalAmount: amount,
|
|
authorPortion: expectedSplit.author,
|
|
platformCommission: expectedSplit.platform,
|
|
recipientPubkey,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
|
|
if (verified) {
|
|
console.log('Private content sent and verified on relay', {
|
|
articleId,
|
|
recipientPubkey,
|
|
messageEventId,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
} else {
|
|
console.warn('Private content sent but not yet verified on relay', {
|
|
articleId,
|
|
recipientPubkey,
|
|
messageEventId,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
}
|
|
}
|
|
|
|
export function logPaymentResult(
|
|
result: { success: boolean; messageEventId?: string; verified?: boolean; error?: string },
|
|
articleId: string,
|
|
recipientPubkey: string,
|
|
amount: number
|
|
): boolean {
|
|
if (result.success && result.messageEventId) {
|
|
logPaymentSuccess(articleId, recipientPubkey, amount, result.messageEventId, result.verified ?? false)
|
|
return true
|
|
} else {
|
|
console.error('Failed to send private content, but payment was confirmed', {
|
|
articleId,
|
|
recipientPubkey,
|
|
error: result.error,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
return false
|
|
}
|
|
}
|