mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 10:53:23 +00:00
refactor: Replace hardcoded billing data seeding with plugin-configurable collection overrides
- Remove `seedBillingData` function for sample data creation - Update refunds, invoices, and payments collections to use pluginConfig for dynamic overrides - Introduce utility functions like `extractSlug` for customizable collection slugs - Streamline customer relation and data extractor logic across collections
This commit is contained in:
@@ -2,12 +2,12 @@ import { sqliteAdapter } from '@payloadcms/db-sqlite'
|
|||||||
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { buildConfig } from 'payload'
|
import { buildConfig } from 'payload'
|
||||||
import { billingPlugin, defaultCustomerInfoExtractor } from '../dist/index.js'
|
|
||||||
import sharp from 'sharp'
|
import sharp from 'sharp'
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
|
|
||||||
import { testEmailAdapter } from './helpers/testEmailAdapter'
|
import { testEmailAdapter } from './helpers/testEmailAdapter'
|
||||||
import { seed } from './seed'
|
import { seed } from './seed'
|
||||||
|
import billingPlugin from '../src/plugin'
|
||||||
|
|
||||||
const filename = fileURLToPath(import.meta.url)
|
const filename = fileURLToPath(import.meta.url)
|
||||||
const dirname = path.dirname(filename)
|
const dirname = path.dirname(filename)
|
||||||
@@ -56,27 +56,29 @@ const buildConfigWithSQLite = () => {
|
|||||||
},
|
},
|
||||||
collections: {
|
collections: {
|
||||||
payments: 'payments',
|
payments: 'payments',
|
||||||
customers: 'customers',
|
|
||||||
invoices: 'invoices',
|
invoices: 'invoices',
|
||||||
refunds: 'refunds',
|
refunds: 'refunds',
|
||||||
// customerRelation: false, // Set to false to disable customer relationship in invoices
|
|
||||||
// customerRelation: 'clients', // Or set to a custom collection slug
|
|
||||||
},
|
},
|
||||||
// Use the default extractor for the built-in customer collection
|
// // Customer relationship configuration
|
||||||
customerInfoExtractor: defaultCustomerInfoExtractor,
|
// customerRelationSlug: 'customers', // Use 'customers' collection for relationship
|
||||||
// Or provide a custom extractor for your own customer collection structure:
|
// // customerRelationSlug: false, // Or set to false to disable customer relationship
|
||||||
|
// // customerRelationSlug: 'clients', // Or use a custom collection slug
|
||||||
|
//
|
||||||
|
// // Provide an extractor for your customer collection structure:
|
||||||
// customerInfoExtractor: (customer) => ({
|
// customerInfoExtractor: (customer) => ({
|
||||||
// name: customer.fullName,
|
// name: customer.name || '',
|
||||||
// email: customer.contactEmail,
|
// email: customer.email || '',
|
||||||
// phone: customer.phoneNumber,
|
// phone: customer.phone,
|
||||||
// company: customer.companyName,
|
// company: customer.company,
|
||||||
// taxId: customer.vatNumber,
|
// taxId: customer.taxId,
|
||||||
// billingAddress: {
|
// billingAddress: customer.address ? {
|
||||||
// line1: customer.billing.street,
|
// line1: customer.address.line1 || '',
|
||||||
// city: customer.billing.city,
|
// line2: customer.address.line2,
|
||||||
// postalCode: customer.billing.zip,
|
// city: customer.address.city || '',
|
||||||
// country: customer.billing.countryCode,
|
// state: customer.address.state,
|
||||||
// }
|
// postalCode: customer.address.postalCode || '',
|
||||||
|
// country: customer.address.country || '',
|
||||||
|
// } : undefined,
|
||||||
// })
|
// })
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
|||||||
120
dev/seed.ts
120
dev/seed.ts
@@ -26,124 +26,4 @@ export const seed = async (payload: Payload) => {
|
|||||||
|
|
||||||
async function seedBillingData(payload: Payload): Promise<void> {
|
async function seedBillingData(payload: Payload): Promise<void> {
|
||||||
payload.logger.info('Seeding billing sample data...')
|
payload.logger.info('Seeding billing sample data...')
|
||||||
|
|
||||||
try {
|
|
||||||
// Check if we already have sample data
|
|
||||||
const existingCustomers = await payload.count({
|
|
||||||
collection: 'customers',
|
|
||||||
where: {
|
|
||||||
email: {
|
|
||||||
equals: 'john.doe@example.com',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if (existingCustomers.totalDocs > 0) {
|
|
||||||
payload.logger.info('Sample billing data already exists, skipping seed')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a sample customer
|
|
||||||
const customer = await payload.create({
|
|
||||||
collection: 'customers',
|
|
||||||
data: {
|
|
||||||
email: 'john.doe@example.com',
|
|
||||||
name: 'John Doe',
|
|
||||||
phone: '+1-555-0123',
|
|
||||||
address: {
|
|
||||||
line1: '123 Main St',
|
|
||||||
city: 'New York',
|
|
||||||
state: 'NY',
|
|
||||||
postal_code: '10001',
|
|
||||||
country: 'US'
|
|
||||||
},
|
|
||||||
metadata: {
|
|
||||||
source: 'seed',
|
|
||||||
created_by: 'system'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
payload.logger.info(`Created sample customer: ${customer.id}`)
|
|
||||||
|
|
||||||
// Create a sample invoice
|
|
||||||
const invoice = await payload.create({
|
|
||||||
collection: 'invoices',
|
|
||||||
data: {
|
|
||||||
number: 'INV-001-SAMPLE',
|
|
||||||
customer: customer.id,
|
|
||||||
currency: 'USD',
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
description: 'Web Development Services',
|
|
||||||
quantity: 10,
|
|
||||||
unitAmount: 5000, // $50.00 per hour
|
|
||||||
totalAmount: 50000 // $500.00 total
|
|
||||||
},
|
|
||||||
{
|
|
||||||
description: 'Design Consultation',
|
|
||||||
quantity: 2,
|
|
||||||
unitAmount: 7500, // $75.00 per hour
|
|
||||||
totalAmount: 15000 // $150.00 total
|
|
||||||
}
|
|
||||||
],
|
|
||||||
subtotal: 65000, // $650.00
|
|
||||||
taxAmount: 5200, // $52.00 (8% tax)
|
|
||||||
amount: 70200, // $702.00 total
|
|
||||||
status: 'open',
|
|
||||||
dueDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(), // 30 days from now
|
|
||||||
notes: 'Payment terms: Net 30 days. This is sample data for development.',
|
|
||||||
metadata: {
|
|
||||||
project: 'website-redesign',
|
|
||||||
billable_hours: 12,
|
|
||||||
sample: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
payload.logger.info(`Created sample invoice: ${invoice.number}`)
|
|
||||||
|
|
||||||
// Create a sample payment using test provider
|
|
||||||
const payment = await payload.create({
|
|
||||||
collection: 'payments',
|
|
||||||
data: {
|
|
||||||
provider: 'test',
|
|
||||||
providerId: `test_pay_sample_${Date.now()}`,
|
|
||||||
status: 'succeeded',
|
|
||||||
amount: 70200, // $702.00
|
|
||||||
currency: 'USD',
|
|
||||||
description: `Sample payment for invoice ${invoice.number}`,
|
|
||||||
customer: customer.id,
|
|
||||||
invoice: invoice.id,
|
|
||||||
metadata: {
|
|
||||||
invoice_number: invoice.number,
|
|
||||||
payment_method: 'test_card',
|
|
||||||
sample: true
|
|
||||||
},
|
|
||||||
providerData: {
|
|
||||||
testMode: true,
|
|
||||||
simulatedPayment: true,
|
|
||||||
autoCompleted: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
payload.logger.info(`Created sample payment: ${payment.id}`)
|
|
||||||
|
|
||||||
// Update invoice status to paid
|
|
||||||
await payload.update({
|
|
||||||
collection: 'invoices',
|
|
||||||
id: invoice.id,
|
|
||||||
data: {
|
|
||||||
status: 'paid',
|
|
||||||
payment: payment.id,
|
|
||||||
paidAt: new Date().toISOString()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
payload.logger.info('Billing sample data seeded successfully!')
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
payload.logger.error('Error seeding billing data:', error)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,30 +3,16 @@ import {
|
|||||||
CollectionAfterChangeHook,
|
CollectionAfterChangeHook,
|
||||||
CollectionBeforeChangeHook,
|
CollectionBeforeChangeHook,
|
||||||
CollectionBeforeValidateHook,
|
CollectionBeforeValidateHook,
|
||||||
CollectionConfig,
|
CollectionConfig, Field,
|
||||||
} from 'payload'
|
} from 'payload'
|
||||||
import { CustomerInfoExtractor } from '@/plugin/config'
|
import { BillingPluginConfig, CustomerInfoExtractor, defaults } from '@/plugin/config'
|
||||||
import { Invoice } from '@/plugin/types'
|
import { Invoice } from '@/plugin/types'
|
||||||
|
import { extractSlug } from '@/plugin/utils'
|
||||||
|
|
||||||
export function createInvoicesCollection(
|
export function createInvoicesCollection(pluginConfig: BillingPluginConfig): CollectionConfig {
|
||||||
slug: string = 'invoices',
|
const {customerRelationSlug, customerInfoExtractor} = pluginConfig
|
||||||
customerCollectionSlug?: string,
|
const overrides = typeof pluginConfig.collections?.invoices === 'object' ? pluginConfig.collections?.invoices : {}
|
||||||
customerInfoExtractor?: CustomerInfoExtractor
|
let fields: Field[] = [
|
||||||
): CollectionConfig {
|
|
||||||
return {
|
|
||||||
slug,
|
|
||||||
access: {
|
|
||||||
create: ({ req: { user } }: AccessArgs) => !!user,
|
|
||||||
delete: ({ req: { user } }: AccessArgs) => !!user,
|
|
||||||
read: ({ req: { user } }: AccessArgs) => !!user,
|
|
||||||
update: ({ req: { user } }: AccessArgs) => !!user,
|
|
||||||
},
|
|
||||||
admin: {
|
|
||||||
defaultColumns: ['number', 'customerInfo.name', 'status', 'amount', 'currency', 'dueDate'],
|
|
||||||
group: 'Billing',
|
|
||||||
useAsTitle: 'number',
|
|
||||||
},
|
|
||||||
fields: [
|
|
||||||
{
|
{
|
||||||
name: 'number',
|
name: 'number',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
@@ -38,14 +24,14 @@ export function createInvoicesCollection(
|
|||||||
unique: true,
|
unique: true,
|
||||||
},
|
},
|
||||||
// Optional customer relationship
|
// Optional customer relationship
|
||||||
...(customerCollectionSlug ? [{
|
...(customerRelationSlug ? [{
|
||||||
name: 'customer',
|
name: 'customer',
|
||||||
type: 'relationship' as const,
|
type: 'relationship' as const,
|
||||||
admin: {
|
admin: {
|
||||||
position: 'sidebar' as const,
|
position: 'sidebar' as const,
|
||||||
description: 'Link to customer record (optional)',
|
description: 'Link to customer record (optional)',
|
||||||
},
|
},
|
||||||
relationTo: customerCollectionSlug as any,
|
relationTo: pluginConfig.customerRelationSlug as never,
|
||||||
required: false,
|
required: false,
|
||||||
}] : []),
|
}] : []),
|
||||||
// Basic customer info fields (embedded)
|
// Basic customer info fields (embedded)
|
||||||
@@ -53,10 +39,10 @@ export function createInvoicesCollection(
|
|||||||
name: 'customerInfo',
|
name: 'customerInfo',
|
||||||
type: 'group',
|
type: 'group',
|
||||||
admin: {
|
admin: {
|
||||||
description: customerCollectionSlug && customerInfoExtractor
|
description: customerRelationSlug && customerInfoExtractor
|
||||||
? 'Customer billing information (auto-populated from customer relationship)'
|
? 'Customer billing information (auto-populated from customer relationship)'
|
||||||
: 'Customer billing information',
|
: 'Customer billing information',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
@@ -64,25 +50,25 @@ export function createInvoicesCollection(
|
|||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Customer name',
|
description: 'Customer name',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
required: !customerCollectionSlug || !customerInfoExtractor,
|
required: !customerRelationSlug || !customerInfoExtractor,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'email',
|
name: 'email',
|
||||||
type: 'email',
|
type: 'email',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Customer email address',
|
description: 'Customer email address',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
required: !customerCollectionSlug || !customerInfoExtractor,
|
required: !customerRelationSlug || !customerInfoExtractor,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'phone',
|
name: 'phone',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Customer phone number',
|
description: 'Customer phone number',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -90,7 +76,7 @@ export function createInvoicesCollection(
|
|||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Company name (optional)',
|
description: 'Company name (optional)',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -98,7 +84,7 @@ export function createInvoicesCollection(
|
|||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Tax ID or VAT number',
|
description: 'Tax ID or VAT number',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@@ -107,10 +93,10 @@ export function createInvoicesCollection(
|
|||||||
name: 'billingAddress',
|
name: 'billingAddress',
|
||||||
type: 'group',
|
type: 'group',
|
||||||
admin: {
|
admin: {
|
||||||
description: customerCollectionSlug && customerInfoExtractor
|
description: customerRelationSlug && customerInfoExtractor
|
||||||
? 'Billing address (auto-populated from customer relationship)'
|
? 'Billing address (auto-populated from customer relationship)'
|
||||||
: 'Billing address',
|
: 'Billing address',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
@@ -118,32 +104,32 @@ export function createInvoicesCollection(
|
|||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Address line 1',
|
description: 'Address line 1',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
required: !customerCollectionSlug || !customerInfoExtractor,
|
required: !customerRelationSlug || !customerInfoExtractor,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'line2',
|
name: 'line2',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Address line 2',
|
description: 'Address line 2',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'city',
|
name: 'city',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
required: !customerCollectionSlug || !customerInfoExtractor,
|
required: !customerRelationSlug || !customerInfoExtractor,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'state',
|
name: 'state',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'State or province',
|
description: 'State or province',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -151,19 +137,19 @@ export function createInvoicesCollection(
|
|||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Postal or ZIP code',
|
description: 'Postal or ZIP code',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
required: !customerCollectionSlug || !customerInfoExtractor,
|
required: !customerRelationSlug || !customerInfoExtractor,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'country',
|
name: 'country',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
description: 'Country code (e.g., US, GB)',
|
description: 'Country code (e.g., US, GB)',
|
||||||
readOnly: customerCollectionSlug && customerInfoExtractor ? true : false,
|
readOnly: !!(customerRelationSlug && customerInfoExtractor),
|
||||||
},
|
},
|
||||||
maxLength: 2,
|
maxLength: 2,
|
||||||
required: !customerCollectionSlug || !customerInfoExtractor,
|
required: !customerRelationSlug || !customerInfoExtractor,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -305,7 +291,24 @@ export function createInvoicesCollection(
|
|||||||
description: 'Additional invoice metadata',
|
description: 'Additional invoice metadata',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
]
|
||||||
|
if (overrides?.fields) {
|
||||||
|
fields = overrides.fields({defaultFields: fields})
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
slug: extractSlug(pluginConfig.collections?.invoices || defaults.invoicesCollection),
|
||||||
|
access: {
|
||||||
|
create: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
|
delete: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
|
read: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
|
update: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
|
},
|
||||||
|
admin: {
|
||||||
|
defaultColumns: ['number', 'customerInfo.name', 'status', 'amount', 'currency', 'dueDate'],
|
||||||
|
group: 'Billing',
|
||||||
|
useAsTitle: 'number',
|
||||||
|
},
|
||||||
|
fields,
|
||||||
hooks: {
|
hooks: {
|
||||||
afterChange: [
|
afterChange: [
|
||||||
({ doc, operation, req }) => {
|
({ doc, operation, req }) => {
|
||||||
@@ -317,7 +320,7 @@ export function createInvoicesCollection(
|
|||||||
beforeChange: [
|
beforeChange: [
|
||||||
async ({ data, operation, req, originalDoc }) => {
|
async ({ data, operation, req, originalDoc }) => {
|
||||||
// Sync customer info from relationship if extractor is provided
|
// Sync customer info from relationship if extractor is provided
|
||||||
if (customerCollectionSlug && customerInfoExtractor && data.customer) {
|
if (customerRelationSlug && customerInfoExtractor && data.customer) {
|
||||||
// Check if customer changed or this is a new invoice
|
// Check if customer changed or this is a new invoice
|
||||||
const customerChanged = operation === 'create' ||
|
const customerChanged = operation === 'create' ||
|
||||||
(originalDoc && originalDoc.customer !== data.customer)
|
(originalDoc && originalDoc.customer !== data.customer)
|
||||||
@@ -326,8 +329,8 @@ export function createInvoicesCollection(
|
|||||||
try {
|
try {
|
||||||
// Fetch the customer data
|
// Fetch the customer data
|
||||||
const customer = await req.payload.findByID({
|
const customer = await req.payload.findByID({
|
||||||
collection: customerCollectionSlug as any,
|
collection: customerRelationSlug as never,
|
||||||
id: data.customer,
|
id: data.customer as never,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Extract customer info using the provided callback
|
// Extract customer info using the provided callback
|
||||||
@@ -386,18 +389,18 @@ export function createInvoicesCollection(
|
|||||||
if (!data) return
|
if (!data) return
|
||||||
|
|
||||||
// If using extractor, customer relationship is required
|
// If using extractor, customer relationship is required
|
||||||
if (customerCollectionSlug && customerInfoExtractor && !data.customer) {
|
if (customerRelationSlug && customerInfoExtractor && !data.customer) {
|
||||||
throw new Error('Please select a customer')
|
throw new Error('Please select a customer')
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not using extractor but have customer collection, either relationship or info is required
|
// If not using extractor but have customer collection, either relationship or info is required
|
||||||
if (customerCollectionSlug && !customerInfoExtractor &&
|
if (customerRelationSlug && !customerInfoExtractor &&
|
||||||
!data.customer && (!data.customerInfo?.name || !data.customerInfo?.email)) {
|
!data.customer && (!data.customerInfo?.name || !data.customerInfo?.email)) {
|
||||||
throw new Error('Either select a customer or provide customer information')
|
throw new Error('Either select a customer or provide customer information')
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no customer collection, ensure customer info is provided
|
// If no customer collection, ensure customer info is provided
|
||||||
if (!customerCollectionSlug && (!data.customerInfo?.name || !data.customerInfo?.email)) {
|
if (!customerRelationSlug && (!data.customerInfo?.name || !data.customerInfo?.email)) {
|
||||||
throw new Error('Customer name and email are required')
|
throw new Error('Customer name and email are required')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,12 @@
|
|||||||
import { AccessArgs, CollectionAfterChangeHook, CollectionBeforeChangeHook, CollectionConfig } from 'payload'
|
import type { AccessArgs, CollectionBeforeChangeHook, CollectionConfig, Field } from 'payload'
|
||||||
import { Payment } from '@/plugin/types'
|
import type { Payment } from '@/plugin/types'
|
||||||
|
import type { BillingPluginConfig} from '@/plugin/config';
|
||||||
|
import { defaults } from '@/plugin/config'
|
||||||
|
import { extractSlug } from '@/plugin/utils'
|
||||||
|
|
||||||
export function createPaymentsCollection(slug: string = 'payments'): CollectionConfig {
|
export function createPaymentsCollection(pluginConfig: BillingPluginConfig): CollectionConfig {
|
||||||
return {
|
const overrides = typeof pluginConfig.collections?.payments === 'object' ? pluginConfig.collections?.payments : {}
|
||||||
slug,
|
let fields: Field[] = [
|
||||||
access: {
|
|
||||||
create: ({ req: { user } }: AccessArgs) => !!user,
|
|
||||||
delete: ({ req: { user } }: AccessArgs) => !!user,
|
|
||||||
read: ({ req: { user } }: AccessArgs) => !!user,
|
|
||||||
update: ({ req: { user } }: AccessArgs) => !!user,
|
|
||||||
},
|
|
||||||
admin: {
|
|
||||||
defaultColumns: ['id', 'provider', 'status', 'amount', 'currency', 'createdAt'],
|
|
||||||
group: 'Billing',
|
|
||||||
useAsTitle: 'id',
|
|
||||||
},
|
|
||||||
fields: [
|
|
||||||
{
|
{
|
||||||
name: 'provider',
|
name: 'provider',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
@@ -81,14 +72,6 @@ export function createPaymentsCollection(slug: string = 'payments'): CollectionC
|
|||||||
description: 'Payment description',
|
description: 'Payment description',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: 'customer',
|
|
||||||
type: 'relationship',
|
|
||||||
admin: {
|
|
||||||
position: 'sidebar',
|
|
||||||
},
|
|
||||||
relationTo: 'customers',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'invoice',
|
name: 'invoice',
|
||||||
type: 'relationship',
|
type: 'relationship',
|
||||||
@@ -122,7 +105,25 @@ export function createPaymentsCollection(slug: string = 'payments'): CollectionC
|
|||||||
hasMany: true,
|
hasMany: true,
|
||||||
relationTo: 'refunds',
|
relationTo: 'refunds',
|
||||||
},
|
},
|
||||||
],
|
]
|
||||||
|
if (overrides?.fields) {
|
||||||
|
fields = overrides?.fields({defaultFields: fields})
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
slug: extractSlug(pluginConfig.collections?.payments || defaults.paymentsCollection),
|
||||||
|
access: overrides?.access || {
|
||||||
|
create: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
|
delete: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
|
read: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
|
update: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
|
},
|
||||||
|
admin: {
|
||||||
|
defaultColumns: ['id', 'provider', 'status', 'amount', 'currency', 'createdAt'],
|
||||||
|
group: 'Billing',
|
||||||
|
useAsTitle: 'id',
|
||||||
|
...overrides?.admin
|
||||||
|
},
|
||||||
|
fields,
|
||||||
hooks: {
|
hooks: {
|
||||||
beforeChange: [
|
beforeChange: [
|
||||||
({ data, operation }) => {
|
({ data, operation }) => {
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
import type { CollectionConfig } from 'payload'
|
import type { AccessArgs, CollectionConfig } from 'payload'
|
||||||
|
import { BillingPluginConfig, defaults } from '@/plugin/config'
|
||||||
|
import { extractSlug } from '@/plugin/utils'
|
||||||
|
|
||||||
import type {
|
export function createRefundsCollection(pluginConfig: BillingPluginConfig): CollectionConfig {
|
||||||
AccessArgs,
|
const overrides = typeof pluginConfig.collections?.invoices === 'object' ? pluginConfig.collections?.invoices : {}
|
||||||
CollectionAfterChangeHook,
|
// TODO: finish collection overrides
|
||||||
CollectionBeforeChangeHook,
|
|
||||||
RefundData,
|
|
||||||
RefundDocument
|
|
||||||
} from '../types/payload'
|
|
||||||
|
|
||||||
export function createRefundsCollection(slug: string = 'refunds'): CollectionConfig {
|
|
||||||
return {
|
return {
|
||||||
slug,
|
slug: extractSlug(pluginConfig.collections?.refunds || defaults.refundsCollection),
|
||||||
access: {
|
access: {
|
||||||
create: ({ req: { user } }: AccessArgs) => !!user,
|
create: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
delete: ({ req: { user } }: AccessArgs) => !!user,
|
delete: ({ req: { user } }: AccessArgs) => !!user,
|
||||||
@@ -113,7 +109,7 @@ export function createRefundsCollection(slug: string = 'refunds'): CollectionCon
|
|||||||
],
|
],
|
||||||
hooks: {
|
hooks: {
|
||||||
afterChange: [
|
afterChange: [
|
||||||
async ({ doc, operation, req }: CollectionAfterChangeHook<RefundDocument>) => {
|
async ({ doc, operation, req }) => {
|
||||||
if (operation === 'create') {
|
if (operation === 'create') {
|
||||||
req.payload.logger.info(`Refund created: ${doc.id} for payment: ${doc.payment}`)
|
req.payload.logger.info(`Refund created: ${doc.id} for payment: ${doc.payment}`)
|
||||||
|
|
||||||
@@ -129,7 +125,7 @@ export function createRefundsCollection(slug: string = 'refunds'): CollectionCon
|
|||||||
id: typeof doc.payment === 'string' ? doc.payment : doc.payment.id,
|
id: typeof doc.payment === 'string' ? doc.payment : doc.payment.id,
|
||||||
collection: 'payments',
|
collection: 'payments',
|
||||||
data: {
|
data: {
|
||||||
refunds: [...refundIds, doc.id as any],
|
refunds: [...refundIds, doc.id],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -139,7 +135,7 @@ export function createRefundsCollection(slug: string = 'refunds'): CollectionCon
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
beforeChange: [
|
beforeChange: [
|
||||||
({ data, operation }: CollectionBeforeChangeHook<RefundData>) => {
|
({ data, operation }) => {
|
||||||
if (operation === 'create') {
|
if (operation === 'create') {
|
||||||
// Validate amount format
|
// Validate amount format
|
||||||
if (data.amount && !Number.isInteger(data.amount)) {
|
if (data.amount && !Number.isInteger(data.amount)) {
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
export * from './types'
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
|
import { CollectionConfig } from 'payload'
|
||||||
|
import { FieldsOverride } from '@/plugin/utils'
|
||||||
|
|
||||||
export const defaults = {
|
export const defaults = {
|
||||||
paymentsCollection: 'payments'
|
paymentsCollection: 'payments',
|
||||||
|
invoicesCollection: 'invoices',
|
||||||
|
refundsCollection: 'refunds',
|
||||||
|
customerRelationSlug: 'customer'
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provider configurations
|
// Provider configurations
|
||||||
@@ -51,13 +56,12 @@ export interface BillingPluginConfig {
|
|||||||
dashboard?: boolean
|
dashboard?: boolean
|
||||||
}
|
}
|
||||||
collections?: {
|
collections?: {
|
||||||
customerRelation?: boolean | string // false to disable, string for custom collection slug
|
invoices?: string | (Partial<CollectionConfig> & {fields?: FieldsOverride})
|
||||||
customers?: string
|
payments?: string | (Partial<CollectionConfig> & {fields?: FieldsOverride})
|
||||||
invoices?: string
|
refunds?: string | (Partial<CollectionConfig> & {fields?: FieldsOverride})
|
||||||
payments?: string
|
|
||||||
refunds?: string
|
|
||||||
}
|
}
|
||||||
customerInfoExtractor?: CustomerInfoExtractor // Callback to extract customer info from relationship
|
customerInfoExtractor?: CustomerInfoExtractor // Callback to extract customer info from relationship
|
||||||
|
customerRelationSlug?: string // Customer collection slug for relationship
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
providers?: {
|
providers?: {
|
||||||
mollie?: MollieConfig
|
mollie?: MollieConfig
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Config } from 'payload'
|
import type { Config } from 'payload'
|
||||||
import { createInvoicesCollection, createPaymentsCollection, createRefundsCollection } from '@/collections'
|
import { createInvoicesCollection, createPaymentsCollection, createRefundsCollection } from '@/collections'
|
||||||
import { BillingPluginConfig } from '@/plugin/config'
|
import type { BillingPluginConfig } from '@/plugin/config'
|
||||||
|
|
||||||
export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config: Config): Config => {
|
export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config: Config): Config => {
|
||||||
if (pluginConfig.disabled) {
|
if (pluginConfig.disabled) {
|
||||||
@@ -12,16 +12,10 @@ export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config
|
|||||||
config.collections = []
|
config.collections = []
|
||||||
}
|
}
|
||||||
|
|
||||||
const customerSlug = pluginConfig.collections?.customers || 'customers'
|
|
||||||
|
|
||||||
config.collections.push(
|
config.collections.push(
|
||||||
createPaymentsCollection(pluginConfig.collections?.payments || 'payments'),
|
createPaymentsCollection(pluginConfig),
|
||||||
createInvoicesCollection(
|
createInvoicesCollection(pluginConfig),
|
||||||
pluginConfig.collections?.invoices || 'invoices',
|
createRefundsCollection(pluginConfig),
|
||||||
pluginConfig.collections?.customerRelation !== false ? customerSlug : undefined,
|
|
||||||
pluginConfig.customerInfoExtractor
|
|
||||||
),
|
|
||||||
createRefundsCollection(pluginConfig.collections?.refunds || 'refunds'),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Initialize endpoints
|
// Initialize endpoints
|
||||||
|
|||||||
@@ -1,7 +1,58 @@
|
|||||||
import { Customer, Refund } from '../../dev/payload-types'
|
|
||||||
|
|
||||||
export type Id = string | number
|
export type Id = string | number
|
||||||
|
|
||||||
|
export interface Refund {
|
||||||
|
id: number;
|
||||||
|
/**
|
||||||
|
* The refund ID from the payment provider
|
||||||
|
*/
|
||||||
|
providerId: string;
|
||||||
|
payment: number | Payment;
|
||||||
|
status: 'pending' | 'processing' | 'succeeded' | 'failed' | 'canceled';
|
||||||
|
/**
|
||||||
|
* Refund amount in cents
|
||||||
|
*/
|
||||||
|
amount: number;
|
||||||
|
/**
|
||||||
|
* ISO 4217 currency code (e.g., USD, EUR)
|
||||||
|
*/
|
||||||
|
currency: string;
|
||||||
|
/**
|
||||||
|
* Reason for the refund
|
||||||
|
*/
|
||||||
|
reason?: ('duplicate' | 'fraudulent' | 'requested_by_customer' | 'other') | null;
|
||||||
|
/**
|
||||||
|
* Additional details about the refund
|
||||||
|
*/
|
||||||
|
description?: string | null;
|
||||||
|
/**
|
||||||
|
* Additional refund metadata
|
||||||
|
*/
|
||||||
|
metadata?:
|
||||||
|
| {
|
||||||
|
[k: string]: unknown;
|
||||||
|
}
|
||||||
|
| unknown[]
|
||||||
|
| string
|
||||||
|
| number
|
||||||
|
| boolean
|
||||||
|
| null;
|
||||||
|
/**
|
||||||
|
* Raw data from the payment provider
|
||||||
|
*/
|
||||||
|
providerData?:
|
||||||
|
| {
|
||||||
|
[k: string]: unknown;
|
||||||
|
}
|
||||||
|
| unknown[]
|
||||||
|
| string
|
||||||
|
| number
|
||||||
|
| boolean
|
||||||
|
| null;
|
||||||
|
updatedAt: string;
|
||||||
|
createdAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Payment {
|
export interface Payment {
|
||||||
id: Id;
|
id: Id;
|
||||||
provider: 'stripe' | 'mollie' | 'test';
|
provider: 'stripe' | 'mollie' | 'test';
|
||||||
@@ -22,7 +73,6 @@ export interface Payment {
|
|||||||
* Payment description
|
* Payment description
|
||||||
*/
|
*/
|
||||||
description?: string | null;
|
description?: string | null;
|
||||||
customer?: (Id | null) | Customer;
|
|
||||||
invoice?: (Id | null) | Invoice;
|
invoice?: (Id | null) | Invoice;
|
||||||
/**
|
/**
|
||||||
* Additional metadata for the payment
|
* Additional metadata for the payment
|
||||||
@@ -53,7 +103,7 @@ export interface Payment {
|
|||||||
createdAt: string;
|
createdAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Invoice {
|
export interface Invoice<TCustomer = unknown> {
|
||||||
id: Id;
|
id: Id;
|
||||||
/**
|
/**
|
||||||
* Invoice number (e.g., INV-001)
|
* Invoice number (e.g., INV-001)
|
||||||
@@ -62,7 +112,7 @@ export interface Invoice {
|
|||||||
/**
|
/**
|
||||||
* Link to customer record (optional)
|
* Link to customer record (optional)
|
||||||
*/
|
*/
|
||||||
customer?: (Id | null) | Customer;
|
customer?: (Id | null) | TCustomer;
|
||||||
/**
|
/**
|
||||||
* Customer billing information (auto-populated from customer relationship)
|
* Customer billing information (auto-populated from customer relationship)
|
||||||
*/
|
*/
|
||||||
@@ -166,3 +216,4 @@ export interface Invoice {
|
|||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
src/plugin/utils.ts
Normal file
5
src/plugin/utils.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import type { CollectionConfig, Field } from 'payload'
|
||||||
|
|
||||||
|
export type FieldsOverride = (args: { defaultFields: Field[] }) => Field[]
|
||||||
|
|
||||||
|
export const extractSlug = (arg: string | Partial<CollectionConfig>) => typeof arg === 'string' ? arg : arg.slug!
|
||||||
Reference in New Issue
Block a user