fix: add better error handling for uninitialized billing plugin

- Fix TypeError when accessing providerConfig on undefined billing plugin
- Add proper type safety: useBillingPlugin now returns BillingPlugin | undefined
- Add clear error message when plugin hasn't been initialized
- Update README quickstart with concise provider response examples

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-18 22:31:51 +01:00
parent 1867bb2f96
commit a37757ffa1
4 changed files with 16 additions and 6 deletions

View File

@@ -100,18 +100,21 @@ export default buildConfig({
const payment = await payload.create({ const payment = await payload.create({
collection: 'payments', collection: 'payments',
data: { data: {
provider: 'stripe', provider: 'stripe', // or 'mollie' or 'test'
amount: 5000, // $50.00 in cents amount: 5000, // $50.00 in cents
currency: 'USD', currency: 'USD',
description: 'Product purchase', description: 'Product purchase',
status: 'pending', status: 'pending',
} }
}) })
// Payment is automatically initialized with Stripe
console.log(payment.providerId) // Stripe PaymentIntent ID
``` ```
**What you get back:**
- **Stripe**: `providerId` = PaymentIntent ID, `providerData.raw.client_secret` for Stripe.js
- **Mollie**: `providerId` = Transaction ID, `providerData.raw._links.checkout.href` for redirect URL
- **Test**: `providerId` = Test payment ID, `providerData.raw.paymentUrl` for interactive test UI
## Payment Providers ## Payment Providers
### Stripe ### Stripe

View File

@@ -1,6 +1,6 @@
{ {
"name": "@xtr-dev/payload-billing", "name": "@xtr-dev/payload-billing",
"version": "0.1.14", "version": "0.1.15",
"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",

View File

@@ -4,6 +4,13 @@ import { useBillingPlugin } from '../plugin/index'
export const initProviderPayment = async (payload: Payload, payment: Partial<Payment>): Promise<Partial<Payment>> => { export const initProviderPayment = async (payload: Payload, payment: Partial<Payment>): Promise<Partial<Payment>> => {
const billing = useBillingPlugin(payload) 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 (!payment.provider || !billing.providerConfig[payment.provider]) { if (!payment.provider || !billing.providerConfig[payment.provider]) {
throw new Error(`Provider ${payment.provider} not found.`) throw new Error(`Provider ${payment.provider} not found.`)
} }

View File

@@ -13,7 +13,7 @@ type BillingPlugin = {
} }
} }
export const useBillingPlugin = (payload: Payload) => singleton.get(payload) as BillingPlugin export const useBillingPlugin = (payload: Payload) => singleton.get(payload) as BillingPlugin | undefined
export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config: Config): Config => { export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config: Config): Config => {
if (pluginConfig.disabled) { if (pluginConfig.disabled) {