mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 02:43:24 +00:00
Compare commits
6 Commits
v0.1.22
...
f2ab50214b
| Author | SHA1 | Date | |
|---|---|---|---|
| f2ab50214b | |||
| 20030b435c | |||
| 1eb9d282b3 | |||
| 291ce255b4 | |||
| 2904d30a5c | |||
| bf6f546371 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-billing",
|
"name": "@xtr-dev/payload-billing",
|
||||||
"version": "0.1.22",
|
"version": "0.1.28",
|
||||||
"description": "PayloadCMS plugin for billing and payment provider integrations with tracking and local testing",
|
"description": "PayloadCMS plugin for billing and payment provider integrations with tracking and local testing",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
@@ -85,11 +85,15 @@ export const mollieProvider = (mollieConfig: MollieProviderConfig & {
|
|||||||
const status = mapMollieStatusToPaymentStatus(molliePayment.status)
|
const status = mapMollieStatusToPaymentStatus(molliePayment.status)
|
||||||
|
|
||||||
// Update the payment status and provider data
|
// Update the payment status and provider data
|
||||||
|
// Use toPlainObject if available, otherwise spread the object
|
||||||
|
const providerData = typeof molliePayment.toPlainObject === 'function'
|
||||||
|
? molliePayment.toPlainObject()
|
||||||
|
: { ...molliePayment }
|
||||||
const updateSuccess = await updatePaymentStatus(
|
const updateSuccess = await updatePaymentStatus(
|
||||||
payload,
|
payload,
|
||||||
payment.id,
|
payment.id,
|
||||||
status,
|
status,
|
||||||
molliePayment.toPlainObject(),
|
providerData,
|
||||||
pluginConfig
|
pluginConfig
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -167,7 +171,10 @@ export const mollieProvider = (mollieConfig: MollieProviderConfig & {
|
|||||||
webhookUrl,
|
webhookUrl,
|
||||||
});
|
});
|
||||||
payment.providerId = molliePayment.id
|
payment.providerId = molliePayment.id
|
||||||
payment.providerData = molliePayment.toPlainObject()
|
// Use toPlainObject if available, otherwise spread the object (for compatibility with different Mollie client versions)
|
||||||
|
payment.providerData = typeof molliePayment.toPlainObject === 'function'
|
||||||
|
? molliePayment.toPlainObject()
|
||||||
|
: { ...molliePayment }
|
||||||
payment.checkoutUrl = molliePayment._links?.checkout?.href || null
|
payment.checkoutUrl = molliePayment._links?.checkout?.href || null
|
||||||
return payment
|
return payment
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -424,7 +424,8 @@ export const testProvider = (testConfig: TestProviderConfig) => {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
processTestPayment(payload, session, pluginConfig).catch(async (error) => {
|
processTestPayment(payload, session, pluginConfig).catch(async (error) => {
|
||||||
const logger = createContextLogger(payload, 'Test Provider')
|
const logger = createContextLogger(payload, 'Test Provider')
|
||||||
logger.error('Failed to process payment:', error)
|
const errorMessage = error instanceof Error ? error.message : String(error)
|
||||||
|
logger.error(`Failed to process payment: ${errorMessage}`)
|
||||||
|
|
||||||
// Ensure session status is updated consistently
|
// Ensure session status is updated consistently
|
||||||
session.status = 'failed'
|
session.status = 'failed'
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export const webhookResponses = {
|
|||||||
// Log error internally but don't expose details
|
// Log error internally but don't expose details
|
||||||
if (payload) {
|
if (payload) {
|
||||||
const logger = createContextLogger(payload, 'Webhook')
|
const logger = createContextLogger(payload, 'Webhook')
|
||||||
logger.error('Error:', message)
|
logger.error(`Error: ${message}`)
|
||||||
} else {
|
} else {
|
||||||
console.error('[Webhook] Error:', message)
|
console.error('[Webhook] Error:', message)
|
||||||
}
|
}
|
||||||
@@ -60,6 +60,7 @@ export async function updatePaymentStatus(
|
|||||||
pluginConfig: BillingPluginConfig
|
pluginConfig: BillingPluginConfig
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const paymentsCollection = extractSlug(pluginConfig.collections?.payments, defaults.paymentsCollection)
|
const paymentsCollection = extractSlug(pluginConfig.collections?.payments, defaults.paymentsCollection)
|
||||||
|
const logger = createContextLogger(payload, 'Payment Update')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// First, fetch the current payment to get the current version
|
// First, fetch the current payment to get the current version
|
||||||
@@ -69,41 +70,65 @@ export async function updatePaymentStatus(
|
|||||||
}) as Payment
|
}) as Payment
|
||||||
|
|
||||||
if (!currentPayment) {
|
if (!currentPayment) {
|
||||||
const logger = createContextLogger(payload, 'Payment Update')
|
|
||||||
logger.error(`Payment ${paymentId} not found`)
|
logger.error(`Payment ${paymentId} not found`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentVersion = currentPayment.version || 1
|
const currentVersion = currentPayment.version || 1
|
||||||
|
|
||||||
// Attempt to update with optimistic locking
|
// Try to use transactions if supported by the database adapter
|
||||||
// We'll use a transaction to ensure atomicity
|
let transactionID: string | number | null = null
|
||||||
const transactionID = await payload.db.beginTransaction()
|
try {
|
||||||
|
transactionID = await payload.db.beginTransaction()
|
||||||
if (!transactionID) {
|
} catch (error) {
|
||||||
const logger = createContextLogger(payload, 'Payment Update')
|
// Transaction support may not be available in all database adapters
|
||||||
logger.error('Failed to begin transaction')
|
logger.debug('Transactions not supported, falling back to direct update')
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (transactionID) {
|
||||||
// Re-fetch within transaction to ensure consistency
|
// Use transactional update with optimistic locking
|
||||||
const paymentInTransaction = await payload.findByID({
|
try {
|
||||||
collection: paymentsCollection,
|
// Re-fetch within transaction to ensure consistency
|
||||||
id: toPayloadId(paymentId),
|
const paymentInTransaction = await payload.findByID({
|
||||||
req: { transactionID }
|
collection: paymentsCollection,
|
||||||
}) as Payment
|
id: toPayloadId(paymentId),
|
||||||
|
req: { transactionID }
|
||||||
|
}) as Payment
|
||||||
|
|
||||||
// Check if version still matches
|
// Check if version still matches
|
||||||
if ((paymentInTransaction.version || 1) !== currentVersion) {
|
if ((paymentInTransaction.version || 1) !== currentVersion) {
|
||||||
// Version conflict detected - payment was modified by another process
|
// Version conflict detected - payment was modified by another process
|
||||||
const logger = createContextLogger(payload, 'Payment Update')
|
logger.warn(`Version conflict for payment ${paymentId} (expected version: ${currentVersion}, got: ${paymentInTransaction.version})`)
|
||||||
logger.warn(`Version conflict for payment ${paymentId} (expected version: ${currentVersion}, got: ${paymentInTransaction.version})`)
|
await payload.db.rollbackTransaction(transactionID)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update with new version
|
||||||
|
await payload.update({
|
||||||
|
collection: paymentsCollection,
|
||||||
|
id: toPayloadId(paymentId),
|
||||||
|
data: {
|
||||||
|
status,
|
||||||
|
providerData: {
|
||||||
|
...providerData,
|
||||||
|
webhookProcessedAt: new Date().toISOString()
|
||||||
|
},
|
||||||
|
version: currentVersion + 1
|
||||||
|
},
|
||||||
|
req: { transactionID }
|
||||||
|
})
|
||||||
|
|
||||||
|
await payload.db.commitTransaction(transactionID)
|
||||||
|
return true
|
||||||
|
} catch (error) {
|
||||||
await payload.db.rollbackTransaction(transactionID)
|
await payload.db.rollbackTransaction(transactionID)
|
||||||
return false
|
throw error
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Fallback: Direct update without transaction support
|
||||||
|
// This is less safe but allows payment updates on databases without transaction support
|
||||||
|
logger.debug('Using direct update without transaction')
|
||||||
|
|
||||||
// Update with new version
|
|
||||||
await payload.update({
|
await payload.update({
|
||||||
collection: paymentsCollection,
|
collection: paymentsCollection,
|
||||||
id: toPayloadId(paymentId),
|
id: toPayloadId(paymentId),
|
||||||
@@ -114,19 +139,18 @@ export async function updatePaymentStatus(
|
|||||||
webhookProcessedAt: new Date().toISOString()
|
webhookProcessedAt: new Date().toISOString()
|
||||||
},
|
},
|
||||||
version: currentVersion + 1
|
version: currentVersion + 1
|
||||||
},
|
}
|
||||||
req: { transactionID }
|
|
||||||
})
|
})
|
||||||
|
|
||||||
await payload.db.commitTransaction(transactionID)
|
|
||||||
return true
|
return true
|
||||||
} catch (error) {
|
|
||||||
await payload.db.rollbackTransaction(transactionID)
|
|
||||||
throw error
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const logger = createContextLogger(payload, 'Payment Update')
|
const errorMessage = error instanceof Error ? error.message : String(error)
|
||||||
logger.error(`Failed to update payment ${paymentId}:`, error)
|
const errorStack = error instanceof Error ? error.stack : undefined
|
||||||
|
logger.error(`Failed to update payment ${paymentId}: ${errorMessage}`)
|
||||||
|
if (errorStack) {
|
||||||
|
logger.error(`Stack trace: ${errorStack}`)
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -165,15 +189,22 @@ export function handleWebhookError(
|
|||||||
context?: string,
|
context?: string,
|
||||||
payload?: Payload
|
payload?: Payload
|
||||||
): Response {
|
): Response {
|
||||||
const message = error instanceof Error ? error.message : 'Unknown error'
|
const message = error instanceof Error ? error.message : String(error)
|
||||||
|
const stack = error instanceof Error ? error.stack : undefined
|
||||||
const fullContext = context ? `${provider} Webhook - ${context}` : `${provider} Webhook`
|
const fullContext = context ? `${provider} Webhook - ${context}` : `${provider} Webhook`
|
||||||
|
|
||||||
// Log detailed error internally for debugging
|
// Log detailed error internally for debugging
|
||||||
if (payload) {
|
if (payload) {
|
||||||
const logger = createContextLogger(payload, fullContext)
|
const logger = createContextLogger(payload, fullContext)
|
||||||
logger.error('Error:', error)
|
logger.error(`Error: ${message}`)
|
||||||
|
if (stack) {
|
||||||
|
logger.error(`Stack trace: ${stack}`)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
console.error(`[${fullContext}] Error:`, error)
|
console.error(`[${fullContext}] Error: ${message}`)
|
||||||
|
if (stack) {
|
||||||
|
console.error(`[${fullContext}] Stack trace:`, stack)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return generic response to avoid information disclosure
|
// Return generic response to avoid information disclosure
|
||||||
|
|||||||
Reference in New Issue
Block a user