139 lines
3.9 KiB
TypeScript
139 lines
3.9 KiB
TypeScript
import { calculateArticleSplit } from './platformCommissions'
|
|
import { lightningAddressService } from './lightningAddress'
|
|
import { automaticTransferService } from './automaticTransfer'
|
|
|
|
export function createTrackingData(
|
|
params: {
|
|
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: params.articleId,
|
|
articlePubkey: params.authorPubkey,
|
|
recipientPubkey: params.recipientPubkey,
|
|
messageEventId: params.messageEventId,
|
|
amount: params.amount,
|
|
authorAmount: expectedSplit.author,
|
|
platformCommission: expectedSplit.platform,
|
|
timestamp,
|
|
verified: params.verified,
|
|
}
|
|
|
|
if (params.zapReceiptId) {
|
|
trackingData.zapReceiptId = params.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(
|
|
params: {
|
|
articleId: string
|
|
recipientPubkey: string
|
|
amount: number
|
|
messageEventId: string
|
|
verified: boolean
|
|
}
|
|
): void {
|
|
const expectedSplit = calculateArticleSplit()
|
|
console.warn('Article payment processed with commission', {
|
|
articleId: params.articleId,
|
|
totalAmount: params.amount,
|
|
authorPortion: expectedSplit.author,
|
|
platformCommission: expectedSplit.platform,
|
|
recipientPubkey: params.recipientPubkey,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
|
|
if (params.verified) {
|
|
console.warn('Private content sent and verified on relay', {
|
|
articleId: params.articleId,
|
|
recipientPubkey: params.recipientPubkey,
|
|
messageEventId: params.messageEventId,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
} else {
|
|
console.warn('Private content sent but not yet verified on relay', {
|
|
articleId: params.articleId,
|
|
recipientPubkey: params.recipientPubkey,
|
|
messageEventId: params.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,
|
|
messageEventId: result.messageEventId,
|
|
verified: result.verified ?? false,
|
|
})
|
|
return true
|
|
}
|
|
console.error('Failed to send private content, but payment was confirmed', {
|
|
articleId,
|
|
recipientPubkey,
|
|
error: result.error,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
return false
|
|
|
|
}
|