Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Address): only emit the Address form data for valid schema fields #2596

Open
wants to merge 2 commits into
base: v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-dodos-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@adyen/adyen-web": patch
---

Fix internal `Address` component to only emit the Address form data for schema specified fields.
16 changes: 0 additions & 16 deletions packages/lib/src/components/BaseElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
8 changes: 0 additions & 8 deletions packages/lib/src/components/BaseElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ class BaseElement<P extends BaseElementProps> {
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 } }),
Expand Down
24 changes: 23 additions & 1 deletion packages/lib/src/components/internal/Address/Address.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Address', () => {
postalCode: '95014',
city: 'Cupertino',
houseNumberOrName: '1',
country: 'US',
country: 'NL',
stateOrProvince: 'CA'
};

Expand Down Expand Up @@ -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);
});
});
4 changes: 4 additions & 0 deletions packages/lib/src/components/internal/Address/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
m1aw marked this conversation as resolved.
Show resolved Hide resolved

const isOptional = optionalFields.includes(cur);
const isRequired = requiredFields.includes(cur);
const newValue = data[cur];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down
Loading