diff --git a/.changeset/cold-dodos-taste.md b/.changeset/cold-dodos-taste.md new file mode 100644 index 0000000000..faf1cd10dd --- /dev/null +++ b/.changeset/cold-dodos-taste.md @@ -0,0 +1,5 @@ +--- +"@adyen/adyen-web": patch +--- + +Fix internal `Address` component to only emit the Address form data for schema specified fields. diff --git a/packages/lib/src/components/BaseElement.test.ts b/packages/lib/src/components/BaseElement.test.ts index 6280b698c1..c846f43389 100644 --- a/packages/lib/src/components/BaseElement.test.ts +++ b/packages/lib/src/components/BaseElement.test.ts @@ -34,22 +34,6 @@ describe('BaseElement', () => { expect(baseElement.data).toEqual({ clientStateDataIndicator: true }); expect(spy).toHaveBeenCalled(); }); - - test('return correct billingAddress data', () => { - const Element = class extends BaseElement<{}> { - constructor(props) { - super(props); - } - protected formatData(): any { - return { billingAddress: { firstName: 'bla' } }; - } - }; - let element; - element = new Element({ type: 'riverty' }); - expect(element.data).toEqual({ clientStateDataIndicator: true, billingAddress: { firstName: 'bla' } }); - element = new Element({ type: 'card' }); - expect(element.data).toEqual({ clientStateDataIndicator: true, billingAddress: {} }); - }); }); describe('render', () => { diff --git a/packages/lib/src/components/BaseElement.ts b/packages/lib/src/components/BaseElement.ts index 7d888f6dea..b820f2d42d 100644 --- a/packages/lib/src/components/BaseElement.ts +++ b/packages/lib/src/components/BaseElement.ts @@ -65,14 +65,6 @@ class BaseElement
{ componentData.paymentMethod.checkoutAttemptId = checkoutAttemptId; } - // Workaround, to be fixed properly - // Remove the firstName & lastName in the billingAddress for non Riverty components - // @ts-ignore type exists - if (this.props.type !== 'riverty' && componentData.billingAddress) { - const { firstName, lastName, ...rest } = componentData.billingAddress; - componentData.billingAddress = { ...rest }; - } - return { ...(clientData && { riskData: { clientData } }), ...(order && { order: { orderData: order.orderData, pspReference: order.pspReference } }), diff --git a/packages/lib/src/components/internal/Address/Address.test.tsx b/packages/lib/src/components/internal/Address/Address.test.tsx index 2a17db416a..01f61b1cbe 100644 --- a/packages/lib/src/components/internal/Address/Address.test.tsx +++ b/packages/lib/src/components/internal/Address/Address.test.tsx @@ -40,7 +40,7 @@ describe('Address', () => { postalCode: '95014', city: 'Cupertino', houseNumberOrName: '1', - country: 'US', + country: 'NL', stateOrProvince: 'CA' }; @@ -171,4 +171,26 @@ describe('Address', () => { wrapper.update(null); expect(receivedData.stateOrProvince).toBe(undefined); }); + + test('does not include fields if they are not part of valid schema fields', () => { + const data = { + street: '1 Infinite Loop', + postalCode: '95014', + country: 'NL', + stateOrProvince: 'CA', + firstName: 'dummy', + invalidField: 'dummy' + }; + + const onChangeMock = jest.fn(); + getWrapper({ data, onChange: onChangeMock }); + const lastOnChangeCall = onChangeMock.mock.calls.pop(); + const receivedData = lastOnChangeCall[0].data; + expect(receivedData.street).toBe(data.street); + expect(receivedData.postalCode).toBe(data.postalCode); + expect(receivedData.country).toBe(data.country); + expect(receivedData.stateOrProvince).toBe(data.stateOrProvince); + expect(receivedData.firstName).toBe(undefined); + expect(receivedData.invalidField).toBe(undefined); + }); }); diff --git a/packages/lib/src/components/internal/Address/Address.tsx b/packages/lib/src/components/internal/Address/Address.tsx index ea23d1861d..e98acd31ba 100644 --- a/packages/lib/src/components/internal/Address/Address.tsx +++ b/packages/lib/src/components/internal/Address/Address.tsx @@ -120,7 +120,11 @@ export default function Address(props: AddressProps) { useEffect((): void => { const optionalFields = specifications.getOptionalFieldsForCountry(data.country); + // Country specific / default schema fields, include both required and optional fields. + const validSchemaFields = specifications.getAddressSchemaForCountryFlat(data.country); const processedData = ADDRESS_SCHEMA.reduce((acc, cur) => { + if (!validSchemaFields?.includes(cur)) return acc; + const isOptional = optionalFields.includes(cur); const isRequired = requiredFields.includes(cur); const newValue = data[cur]; diff --git a/packages/lib/src/components/internal/Address/Specifications.ts b/packages/lib/src/components/internal/Address/Specifications.ts index e17555a43e..c8f90fd97a 100644 --- a/packages/lib/src/components/internal/Address/Specifications.ts +++ b/packages/lib/src/components/internal/Address/Specifications.ts @@ -79,7 +79,6 @@ class Specifications { * Returns an array with the address schema of the selected country or the default address schema * Flat version of getAddressSchemaForCountry * @param country - The selected country - * @param mode - Address schema mode, can be 'full', 'partial' or 'none' * @returns Array */ getAddressSchemaForCountryFlat(country: string): AddressField[] {