mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 19:03:23 +00:00
Use Symbol.for() instead of Symbol() for plugin singleton storage to ensure
plugin state persists across different module loading contexts (admin panel,
API routes, server components).
This fixes the "Billing plugin not initialized" error that occurred when
calling payload.create() from Next.js API routes, server components, or
server actions.
Changes:
- Plugin singleton now uses Symbol.for('@xtr-dev/payload-billing')
- Provider singletons (stripe, mollie, test) use global symbols
- Enhanced error message with troubleshooting guidance
Fixes #1
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
22 lines
1.0 KiB
TypeScript
22 lines
1.0 KiB
TypeScript
import type { Payment } from '../plugin/types/index'
|
|
import type { Payload } from 'payload'
|
|
import { useBillingPlugin } from '../plugin/index'
|
|
|
|
export const initProviderPayment = async (payload: Payload, payment: Partial<Payment>): Promise<Partial<Payment>> => {
|
|
const billing = useBillingPlugin(payload)
|
|
|
|
if (!billing) {
|
|
throw new Error(
|
|
'Billing plugin not initialized. Make sure the billingPlugin is properly configured in your Payload config and that Payload has finished initializing. ' +
|
|
'If you are calling this from a Next.js API route or Server Component, ensure you are using getPayload() with the same config instance used in your Payload configuration.'
|
|
)
|
|
}
|
|
|
|
if (!payment.provider || !billing.providerConfig[payment.provider]) {
|
|
throw new Error(`Provider ${payment.provider} not found.`)
|
|
}
|
|
// Handle both async and non-async initPayment functions
|
|
const result = billing.providerConfig[payment.provider].initPayment(payload, payment)
|
|
return await Promise.resolve(result)
|
|
}
|