Skip to content

Commit 71c2f63

Browse files
paulpopusmikecebul
andauthored
fix(plugin-form-builder): allow overrides to the payment fields group (#9522)
Co-authored-by: mikecebul <mikecebul@gmail.com>
1 parent 4e3be44 commit 71c2f63

File tree

4 files changed

+81
-62
lines changed

4 files changed

+81
-62
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { Field } from 'payload'
2+
3+
export const defaultPaymentFields: Field = {
4+
name: 'payment',
5+
type: 'group',
6+
admin: {
7+
readOnly: true,
8+
},
9+
fields: [
10+
{
11+
name: 'field',
12+
type: 'text',
13+
label: 'Field',
14+
},
15+
{
16+
name: 'status',
17+
type: 'text',
18+
label: 'Status',
19+
},
20+
{
21+
name: 'amount',
22+
type: 'number',
23+
admin: {
24+
description: 'Amount in cents',
25+
},
26+
},
27+
{
28+
name: 'paymentProcessor',
29+
type: 'text',
30+
},
31+
{
32+
name: 'creditCard',
33+
type: 'group',
34+
fields: [
35+
{
36+
name: 'token',
37+
type: 'text',
38+
label: 'token',
39+
},
40+
{
41+
name: 'brand',
42+
type: 'text',
43+
label: 'Brand',
44+
},
45+
{
46+
name: 'number',
47+
type: 'text',
48+
label: 'Number',
49+
},
50+
],
51+
label: 'Credit Card',
52+
},
53+
],
54+
}

packages/plugin-form-builder/src/collections/FormSubmissions/index.ts

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { CollectionConfig, Field } from 'payload'
22

33
import type { FormBuilderPluginConfig } from '../../types.js'
44

5+
import { defaultPaymentFields } from './fields/defaultPaymentFields.js'
56
import { createCharge } from './hooks/createCharge.js'
67
import { sendEmail } from './hooks/sendEmail.js'
78

@@ -11,6 +12,8 @@ export const generateSubmissionCollection = (
1112
): CollectionConfig => {
1213
const formSlug = formConfig?.formOverrides?.slug || 'forms'
1314

15+
const enablePaymentFields = Boolean(formConfig?.fields?.payment)
16+
1417
const defaultFields: Field[] = [
1518
{
1619
name: 'form',
@@ -79,6 +82,7 @@ export const generateSubmissionCollection = (
7982
},
8083
],
8184
},
85+
...(enablePaymentFields ? [defaultPaymentFields] : []),
8286
]
8387

8488
const newConfig: CollectionConfig = {
@@ -108,63 +112,5 @@ export const generateSubmissionCollection = (
108112
],
109113
},
110114
}
111-
112-
const paymentFieldConfig = formConfig?.fields?.payment
113-
114-
if (paymentFieldConfig) {
115-
newConfig.fields.push({
116-
name: 'payment',
117-
type: 'group',
118-
admin: {
119-
readOnly: true,
120-
},
121-
fields: [
122-
{
123-
name: 'field',
124-
type: 'text',
125-
label: 'Field',
126-
},
127-
{
128-
name: 'status',
129-
type: 'text',
130-
label: 'Status',
131-
},
132-
{
133-
name: 'amount',
134-
type: 'number',
135-
admin: {
136-
description: 'Amount in cents',
137-
},
138-
},
139-
{
140-
name: 'paymentProcessor',
141-
type: 'text',
142-
},
143-
{
144-
name: 'creditCard',
145-
type: 'group',
146-
fields: [
147-
{
148-
name: 'token',
149-
type: 'text',
150-
label: 'token',
151-
},
152-
{
153-
name: 'brand',
154-
type: 'text',
155-
label: 'Brand',
156-
},
157-
{
158-
name: 'number',
159-
type: 'text',
160-
label: 'Number',
161-
},
162-
],
163-
label: 'Credit Card',
164-
},
165-
],
166-
})
167-
}
168-
169115
return newConfig
170116
}

test/plugin-form-builder/config.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path'
33
const filename = fileURLToPath(import.meta.url)
44
const dirname = path.dirname(filename)
55
import type { BeforeEmail } from '@payloadcms/plugin-form-builder/types'
6-
import type { Block } from 'payload'
6+
import type { Block, Field } from 'payload'
77

88
//import { nodemailerAdapter } from '@payloadcms/email-nodemailer'
99
import { formBuilderPlugin, fields as formFields } from '@payloadcms/plugin-form-builder'
@@ -104,8 +104,25 @@ export default buildConfigWithDefaults({
104104
},
105105
formSubmissionOverrides: {
106106
fields: ({ defaultFields }) => {
107+
const modifiedFields: Field[] = defaultFields.map((field) => {
108+
if ('name' in field && field.type === 'group' && field.name === 'payment') {
109+
return {
110+
...field,
111+
fields: [
112+
...field.fields, // comment this out to override payments group entirely
113+
{
114+
name: 'stripeCheckoutSession',
115+
type: 'text',
116+
},
117+
],
118+
}
119+
}
120+
121+
return field
122+
})
123+
107124
return [
108-
...defaultFields,
125+
...modifiedFields,
109126
{
110127
name: 'custom',
111128
type: 'text',

test/plugin-form-builder/payload-types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ export interface FormSubmission {
301301
id?: string | null;
302302
}[]
303303
| null;
304-
custom?: string | null;
305304
payment?: {
306305
field?: string | null;
307306
status?: string | null;
@@ -312,7 +311,9 @@ export interface FormSubmission {
312311
brand?: string | null;
313312
number?: string | null;
314313
};
314+
stripeCheckoutSession?: string | null;
315315
};
316+
custom?: string | null;
316317
updatedAt: string;
317318
createdAt: string;
318319
}
@@ -584,7 +585,6 @@ export interface FormSubmissionsSelect<T extends boolean = true> {
584585
value?: T;
585586
id?: T;
586587
};
587-
custom?: T;
588588
payment?:
589589
| T
590590
| {
@@ -599,7 +599,9 @@ export interface FormSubmissionsSelect<T extends boolean = true> {
599599
brand?: T;
600600
number?: T;
601601
};
602+
stripeCheckoutSession?: T;
602603
};
604+
custom?: T;
603605
updatedAt?: T;
604606
createdAt?: T;
605607
}

0 commit comments

Comments
 (0)