From a37757ffa17cfe28588c34f6b26aba08c11fe8b3 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Tue, 18 Nov 2025 22:31:51 +0100 Subject: [PATCH] fix: add better error handling for uninitialized billing plugin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- README.md | 11 +++++++---- package.json | 2 +- src/collections/hooks.ts | 7 +++++++ src/plugin/index.ts | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 453e31d..54fb0d8 100644 --- a/README.md +++ b/README.md @@ -100,18 +100,21 @@ export default buildConfig({ const payment = await payload.create({ collection: 'payments', data: { - provider: 'stripe', + provider: 'stripe', // or 'mollie' or 'test' amount: 5000, // $50.00 in cents currency: 'USD', description: 'Product purchase', 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 ### Stripe diff --git a/package.json b/package.json index 92d0447..223c3bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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", "license": "MIT", "type": "module", diff --git a/src/collections/hooks.ts b/src/collections/hooks.ts index f461159..831c37f 100644 --- a/src/collections/hooks.ts +++ b/src/collections/hooks.ts @@ -4,6 +4,13 @@ import { useBillingPlugin } from '../plugin/index' export const initProviderPayment = async (payload: Payload, payment: Partial): Promise> => { 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]) { throw new Error(`Provider ${payment.provider} not found.`) } diff --git a/src/plugin/index.ts b/src/plugin/index.ts index a15b47a..d596507 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -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 => { if (pluginConfig.disabled) {