pay for music
This commit is contained in:
@@ -6,6 +6,7 @@ const { getStripeClient } = require('../../helpers/stripe')
|
||||
const { REGION } = require('./config')
|
||||
const {
|
||||
paymentsCollection,
|
||||
refsList,
|
||||
resolveStripeWebhookSecret,
|
||||
getServerTimestamp,
|
||||
toFirestoreTimestamp,
|
||||
@@ -149,6 +150,91 @@ const handleCheckoutSessionCompleted = async (session, event, { stripe } = {}) =
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
session.mode === 'payment' &&
|
||||
(session.payment_status === 'paid' || session.payment_status === 'no_payment_required') &&
|
||||
session.metadata?.purchaseType === 'SONG_DOWNLOAD'
|
||||
) {
|
||||
const rawProjectId = session.metadata?.projectId
|
||||
const projectId = typeof rawProjectId === 'string' ? rawProjectId.trim() : ''
|
||||
|
||||
if (projectId && paymentDocRef) {
|
||||
let paymentSnapshot = null
|
||||
try {
|
||||
paymentSnapshot = await paymentDocRef.get()
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
'[subscription-handleCheckoutSessionCompleted] Unable to read payment doc',
|
||||
session.id,
|
||||
error?.message || error
|
||||
)
|
||||
}
|
||||
|
||||
const alreadyGranted = Boolean(
|
||||
paymentSnapshot?.exists && paymentSnapshot.data()?.downloadGrantedAt
|
||||
)
|
||||
|
||||
if (!alreadyGranted) {
|
||||
const projectRef = refsList?.projects?.doc(projectId) || null
|
||||
let shouldGrant = true
|
||||
|
||||
if (projectRef) {
|
||||
try {
|
||||
const projectSnapshot = await projectRef.get()
|
||||
if (!projectSnapshot.exists) {
|
||||
shouldGrant = false
|
||||
} else {
|
||||
const projectData = projectSnapshot.data() || {}
|
||||
const ownerId =
|
||||
typeof projectData?.userId === 'string' ? projectData.userId.trim() : ''
|
||||
const targetUserId = uid || firebaseUid || userRef?.id || null
|
||||
if (ownerId && targetUserId && ownerId !== targetUserId) {
|
||||
console.warn(
|
||||
'[subscription-handleCheckoutSessionCompleted] Project owner mismatch',
|
||||
projectId
|
||||
)
|
||||
shouldGrant = false
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
'[subscription-handleCheckoutSessionCompleted] Unable to read project',
|
||||
projectId,
|
||||
error?.message || error
|
||||
)
|
||||
}
|
||||
} else {
|
||||
shouldGrant = false
|
||||
}
|
||||
|
||||
if (shouldGrant && projectRef) {
|
||||
await projectRef.set(
|
||||
{
|
||||
downloadPurchase: {
|
||||
status: 'paid',
|
||||
paymentId: session.id || null,
|
||||
paymentIntentId:
|
||||
typeof session.payment_intent === 'string' ? session.payment_intent : null,
|
||||
amount: session.amount_total ?? null,
|
||||
currency: session.currency || null,
|
||||
paidAt: getServerTimestamp(),
|
||||
},
|
||||
},
|
||||
{ merge: true }
|
||||
)
|
||||
|
||||
await paymentDocRef.set(
|
||||
{
|
||||
downloadGrantedAt: getServerTimestamp(),
|
||||
downloadProjectId: projectId,
|
||||
},
|
||||
{ merge: true }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleCustomerSubscriptionEvent = async (subscription, event, { stripe } = {}) => {
|
||||
|
||||
Reference in New Issue
Block a user