mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 10:53:23 +00:00
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
This commit is contained in:
@@ -10,18 +10,6 @@ export const defaults = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Provider configurations
|
// 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 {
|
export interface TestProviderConfig {
|
||||||
autoComplete?: boolean
|
autoComplete?: boolean
|
||||||
@@ -65,13 +53,5 @@ export interface BillingPluginConfig {
|
|||||||
customerRelationSlug?: string // Customer collection slug for relationship
|
customerRelationSlug?: string // Customer collection slug for relationship
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
providers?: PaymentProvider[]
|
providers?: PaymentProvider[]
|
||||||
webhooks?: {
|
|
||||||
basePath?: string
|
|
||||||
cors?: boolean
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugin type
|
|
||||||
export interface BillingPluginOptions extends BillingPluginConfig {
|
|
||||||
disabled?: boolean
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -25,7 +25,11 @@ export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config
|
|||||||
createPaymentsCollection(pluginConfig),
|
createPaymentsCollection(pluginConfig),
|
||||||
createInvoicesCollection(pluginConfig),
|
createInvoicesCollection(pluginConfig),
|
||||||
createRefundsCollection(pluginConfig),
|
createRefundsCollection(pluginConfig),
|
||||||
]
|
];
|
||||||
|
|
||||||
|
(pluginConfig.providers || [])
|
||||||
|
.filter(provider => provider.onConfig)
|
||||||
|
.forEach(provider => provider.onConfig!(config, pluginConfig))
|
||||||
|
|
||||||
const incomingOnInit = config.onInit
|
const incomingOnInit = config.onInit
|
||||||
config.onInit = async (payload) => {
|
config.onInit = async (payload) => {
|
||||||
@@ -35,15 +39,16 @@ export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config
|
|||||||
singleton.set(payload, {
|
singleton.set(payload, {
|
||||||
config: pluginConfig,
|
config: pluginConfig,
|
||||||
providerConfig: (pluginConfig.providers || []).reduce(
|
providerConfig: (pluginConfig.providers || []).reduce(
|
||||||
(acc, val) => {
|
(record, provider) => {
|
||||||
acc[val.key] = val
|
record[provider.key] = provider
|
||||||
return acc
|
return record
|
||||||
},
|
},
|
||||||
{} as Record<string, PaymentProvider>
|
{} as Record<string, PaymentProvider>
|
||||||
)
|
)
|
||||||
} satisfies BillingPlugin)
|
} satisfies BillingPlugin)
|
||||||
console.log('Billing plugin initialized', singleton.get(payload))
|
await Promise.all((pluginConfig.providers || [])
|
||||||
await Promise.all((pluginConfig.providers || []).map(p => p.onInit(payload)))
|
.filter(provider => provider.onInit)
|
||||||
|
.map(provider => provider.onInit!(payload)))
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Payment } from '@/plugin/types/payments'
|
import type { Payment } from '@/plugin/types/payments'
|
||||||
import type { InitPayment, PaymentProvider } from '@/plugin/types'
|
import type { InitPayment, PaymentProvider } from '@/plugin/types'
|
||||||
import type { Payload } from 'payload'
|
import type { Config, Payload } from 'payload'
|
||||||
import { createSingleton } from '@/plugin/singleton'
|
import { createSingleton } from '@/plugin/singleton'
|
||||||
import type { createMollieClient, MollieClient } from '@mollie/api-client'
|
import type { createMollieClient, MollieClient } from '@mollie/api-client'
|
||||||
|
|
||||||
@@ -11,10 +11,29 @@ export const mollieProvider = (config: MollieProviderConfig) => {
|
|||||||
const singleton = createSingleton<MollieClient>(symbol)
|
const singleton = createSingleton<MollieClient>(symbol)
|
||||||
return {
|
return {
|
||||||
key: 'mollie',
|
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) => {
|
onInit: async (payload: Payload) => {
|
||||||
const createMollieClient = (await import('@mollie/api-client')).default
|
const createMollieClient = (await import('@mollie/api-client')).default
|
||||||
const mollieClient = createMollieClient(config)
|
const mollieClient = createMollieClient(config)
|
||||||
singleton.set(payload, mollieClient)
|
singleton.set(payload, mollieClient)
|
||||||
|
|
||||||
},
|
},
|
||||||
initPayment: async (payload, payment) => {
|
initPayment: async (payload, payment) => {
|
||||||
if (!payment.amount) {
|
if (!payment.amount) {
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import type { Payment } from '@/plugin/types/payments'
|
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<Payment>) => Promise<Partial<Payment>>
|
export type InitPayment = (payload: Payload, payment: Partial<Payment>) => Promise<Partial<Payment>>
|
||||||
|
|
||||||
export type PaymentProvider = {
|
export type PaymentProvider = {
|
||||||
key: string
|
key: string
|
||||||
onInit: (payload: Payload) => Promise<void> | void
|
onConfig?: (config: Config, pluginConfig: BillingPluginConfig) => void
|
||||||
|
onInit?: (payload: Payload) => Promise<void> | void
|
||||||
initPayment: InitPayment
|
initPayment: InitPayment
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user