mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 10:53:23 +00:00
fix: Address type safety and error handling concerns
🔧 Type Safety Improvements: - Add missing ProviderData import to fix compilation errors - Create toPayloadId utility for safe ID type conversion - Replace all 'as any' casts with typed utility function - Improve type safety while maintaining PayloadCMS compatibility 🛡️ Error Handling Enhancements: - Add try-catch for version check in payment hooks - Handle missing documents gracefully with fallback to version 1 - Add detailed logging for debugging race conditions - Prevent hook failures from blocking payment operations ⚡ Version Logic Improvements: - Distinguish between webhook updates and manual admin updates - Only auto-increment version for manual updates, not webhook updates - Check for webhook-specific fields to determine update source - Reduce race condition risks with explicit update type detection 🔍 Code Quality: - Centralized type casting in utility function - Better error messages and logging context - More explicit logic flow for version handling - Improved maintainability and debugging capabilities 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import type { Payload } from 'payload'
|
||||
import type { Payment } from '@/plugin/types/payments'
|
||||
import type { BillingPluginConfig } from '@/plugin/config'
|
||||
import type { ProviderData } from './types'
|
||||
import { defaults } from '@/plugin/config'
|
||||
import { extractSlug } from '@/plugin/utils'
|
||||
import { extractSlug, toPayloadId } from '@/plugin/utils'
|
||||
|
||||
/**
|
||||
* Common webhook response utilities
|
||||
@@ -57,7 +58,7 @@ export async function updatePaymentStatus(
|
||||
// Get current payment to check version for atomic locking
|
||||
const currentPayment = await payload.findByID({
|
||||
collection: paymentsCollection,
|
||||
id: paymentId as any // Cast to avoid type mismatch between Id and PayloadCMS types
|
||||
id: toPayloadId(paymentId)
|
||||
}) as Payment
|
||||
|
||||
const now = new Date().toISOString()
|
||||
@@ -68,7 +69,7 @@ export async function updatePaymentStatus(
|
||||
const result = await payload.updateMany({
|
||||
collection: paymentsCollection,
|
||||
where: {
|
||||
id: { equals: paymentId as any }, // Cast to avoid type mismatch
|
||||
id: { equals: toPayloadId(paymentId) },
|
||||
version: { equals: currentPayment.version || 1 }
|
||||
},
|
||||
data: {
|
||||
@@ -112,7 +113,7 @@ export async function updateInvoiceOnPaymentSuccess(
|
||||
|
||||
await payload.update({
|
||||
collection: invoicesCollection,
|
||||
id: invoiceId as any, // Cast to avoid type mismatch between Id and PayloadCMS types
|
||||
id: toPayloadId(invoiceId),
|
||||
data: {
|
||||
status: 'paid',
|
||||
payment: payment.id
|
||||
|
||||
Reference in New Issue
Block a user