From 2aad0d253877f34c08e335feb3878eed7e80a0a2 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Tue, 16 Sep 2025 22:55:30 +0200 Subject: [PATCH] feat: Add support for provider-level configuration in billing plugin - Introduce `onConfig` callback for payment providers - Add dynamic endpoint registration for Mollie webhook handling - Remove unused provider-specific configurations from plugin types - Update initialization to include provider-level configurations --- src/plugin/config.ts | 20 -------------------- src/plugin/index.ts | 17 +++++++++++------ src/providers/mollie.ts | 21 ++++++++++++++++++++- src/providers/types.ts | 6 ++++-- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/plugin/config.ts b/src/plugin/config.ts index d2385d8..73861c1 100644 --- a/src/plugin/config.ts +++ b/src/plugin/config.ts @@ -10,18 +10,6 @@ export const defaults = { } // Provider configurations -export interface StripeConfig { - apiVersion?: string - publishableKey: string - secretKey: string - webhookEndpointSecret: string -} - -export interface MollieConfig { - apiKey: string - testMode?: boolean - webhookUrl: string -} export interface TestProviderConfig { autoComplete?: boolean @@ -65,13 +53,5 @@ export interface BillingPluginConfig { customerRelationSlug?: string // Customer collection slug for relationship disabled?: boolean providers?: PaymentProvider[] - webhooks?: { - basePath?: string - cors?: boolean - } } -// Plugin type -export interface BillingPluginOptions extends BillingPluginConfig { - disabled?: boolean -} diff --git a/src/plugin/index.ts b/src/plugin/index.ts index 3bac540..d1421eb 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -25,7 +25,11 @@ export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config createPaymentsCollection(pluginConfig), createInvoicesCollection(pluginConfig), createRefundsCollection(pluginConfig), - ] + ]; + + (pluginConfig.providers || []) + .filter(provider => provider.onConfig) + .forEach(provider => provider.onConfig!(config, pluginConfig)) const incomingOnInit = config.onInit config.onInit = async (payload) => { @@ -35,15 +39,16 @@ export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config singleton.set(payload, { config: pluginConfig, providerConfig: (pluginConfig.providers || []).reduce( - (acc, val) => { - acc[val.key] = val - return acc + (record, provider) => { + record[provider.key] = provider + return record }, {} as Record ) } satisfies BillingPlugin) - console.log('Billing plugin initialized', singleton.get(payload)) - await Promise.all((pluginConfig.providers || []).map(p => p.onInit(payload))) + await Promise.all((pluginConfig.providers || []) + .filter(provider => provider.onInit) + .map(provider => provider.onInit!(payload))) } return config diff --git a/src/providers/mollie.ts b/src/providers/mollie.ts index 5ef7794..2fae070 100644 --- a/src/providers/mollie.ts +++ b/src/providers/mollie.ts @@ -1,6 +1,6 @@ import type { Payment } from '@/plugin/types/payments' import type { InitPayment, PaymentProvider } from '@/plugin/types' -import type { Payload } from 'payload' +import type { Config, Payload } from 'payload' import { createSingleton } from '@/plugin/singleton' import type { createMollieClient, MollieClient } from '@mollie/api-client' @@ -11,10 +11,29 @@ export const mollieProvider = (config: MollieProviderConfig) => { const singleton = createSingleton(symbol) return { key: 'mollie', + onConfig: config => { + config.endpoints = [ + ...(config.endpoints || []), + { + path: '/payload-billing/mollie/webhook', + method: 'post', + handler: async (req) => { + const payload = req.payload + const mollieClient = singleton.get(payload) + if (!req.text) { + throw new Error('No text body') + } + const molliePaymentId = (await req.text()).slice(3) + + } + } + ] + }, onInit: async (payload: Payload) => { const createMollieClient = (await import('@mollie/api-client')).default const mollieClient = createMollieClient(config) singleton.set(payload, mollieClient) + }, initPayment: async (payload, payment) => { if (!payment.amount) { diff --git a/src/providers/types.ts b/src/providers/types.ts index d15aa84..fd06c44 100644 --- a/src/providers/types.ts +++ b/src/providers/types.ts @@ -1,10 +1,12 @@ import type { Payment } from '@/plugin/types/payments' -import type { Payload } from 'payload' +import type { Config, Payload } from 'payload' +import type { BillingPluginConfig } from '@/plugin/config' export type InitPayment = (payload: Payload, payment: Partial) => Promise> export type PaymentProvider = { key: string - onInit: (payload: Payload) => Promise | void + onConfig?: (config: Config, pluginConfig: BillingPluginConfig) => void + onInit?: (payload: Payload) => Promise | void initPayment: InitPayment }