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: checkout processing when fields are hidden #7130

Merged
merged 2 commits into from
Sep 5, 2023
Merged
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
4 changes: 4 additions & 0 deletions changelog/fix-7121-null-coalescing
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

checkout processing when fields are hidden via customizer
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,25 @@ function createStripePaymentMethod(
name: document.querySelector( '#billing_first_name' )
? (
document.querySelector( '#billing_first_name' )
.value +
?.value +
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed on Slack - adding null-coalescing on all usages, just to be safe.

' ' +
document.querySelector( '#billing_last_name' ).value
document.querySelector( '#billing_last_name' )
?.value
).trim()
: undefined,
email: document.querySelector( '#billing_email' ).value,
phone: document.querySelector( '#billing_phone' ).value,
email: document.querySelector( '#billing_email' )?.value,
phone: document.querySelector( '#billing_phone' )?.value,
address: {
city: document.querySelector( '#billing_city' ).value,
country: document.querySelector( '#billing_country' ).value,
line1: document.querySelector( '#billing_address_1' ).value,
line2: document.querySelector( '#billing_address_2' ).value,
city: document.querySelector( '#billing_city' )?.value,
country: document.querySelector( '#billing_country' )
?.value,
line1: document.querySelector( '#billing_address_1' )
?.value,
line2: document.querySelector( '#billing_address_2' )
?.value,
postal_code: document.querySelector( '#billing_postcode' )
.value,
state: document.querySelector( '#billing_state' ).value,
?.value,
state: document.querySelector( '#billing_state' )?.value,
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,12 @@ describe( 'Payment processing', () => {

const mockJqueryForm = {
submit: jest.fn(),
addClass: jest.fn( () => {
return {
block: jest.fn(),
};
} ),
removeClass: jest.fn(),
unblock: jest.fn(),
addClass: jest.fn( () => ( {
block: jest.fn(),
} ) ),
removeClass: jest.fn( () => ( {
unblock: jest.fn(),
} ) ),
attr: jest.fn().mockReturnValue( 'checkout' ),
};

Expand Down Expand Up @@ -377,13 +376,46 @@ describe( 'Payment processing', () => {

const checkoutForm = {
submit: jest.fn(),
addClass: jest.fn( () => {
return {
block: jest.fn(),
};
} ),
removeClass: jest.fn(),
unblock: jest.fn(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that unblock is called after removeClass (similar to addClass().block()). So I fixed it on all the tests.

addClass: jest.fn( () => ( {
block: jest.fn(),
} ) ),
removeClass: jest.fn( () => ( {
unblock: jest.fn(),
} ) ),
attr: jest.fn().mockReturnValue( 'checkout' ),
};

await processPayment( apiMock, checkoutForm, 'card' );

expect( mockCreatePaymentMethod ).toHaveBeenCalledWith( {
elements: expect.any( Object ),
params: {
billing_details: expect.any( Object ),
},
} );
} );

test( 'Payment processing does not create error when some fields are hidden via customizer', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for our use case

setupBillingDetailsFields();
// pretending that the customizer removed the billing phone field
document.body.removeChild( document.getElementById( 'billing_phone' ) );
getFingerprint.mockImplementation( () => {
return 'fingerprint';
} );

const mockDomElement = document.createElement( 'div' );
mockDomElement.dataset.paymentMethodType = 'card';

await mountStripePaymentElement( apiMock, mockDomElement );

const checkoutForm = {
submit: jest.fn(),
addClass: jest.fn( () => ( {
block: jest.fn(),
} ) ),
removeClass: jest.fn( () => ( {
unblock: jest.fn(),
} ) ),
attr: jest.fn().mockReturnValue( 'checkout' ),
};

Expand All @@ -410,13 +442,12 @@ describe( 'Payment processing', () => {

const addPaymentMethodForm = {
submit: jest.fn(),
addClass: jest.fn( () => {
return {
block: jest.fn(),
};
} ),
removeClass: jest.fn(),
unblock: jest.fn(),
addClass: jest.fn( () => ( {
block: jest.fn(),
} ) ),
removeClass: jest.fn( () => ( {
unblock: jest.fn(),
} ) ),
attr: jest.fn().mockReturnValue( 'add_payment_method' ),
};

Expand Down