Skip to content

Commit 815fa6c

Browse files
fix(react): use pickup location address for tax calculation on billing updates (#1355)
* fix(react): use pickup location address for tax calculation on billing updates * add changeset * modify changeset to be patch
1 parent 52bc89b commit 815fa6c

3 files changed

Lines changed: 172 additions & 1 deletion

File tree

.changeset/seven-signs-ask.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@godaddy/react": patch
3+
---
4+
5+
Fix pickup tax recalculation to always use the pickup location address when billing address fields are updated.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
/**
4+
* Helper function to get the tax destination address based on delivery method.
5+
* This is the core logic used in useUpdateOrder to determine which address
6+
* to send for tax calculation.
7+
*
8+
* For pickup orders: Always use the pickup location address
9+
* For shipping orders: Let backend use the saved shipping address (undefined)
10+
*/
11+
function getTaxDestinationAddress(
12+
deliveryMethod: string | undefined,
13+
pickupLocationId: string | undefined,
14+
locations: Array<{ id: string; address: Record<string, unknown> }> | undefined
15+
): Record<string, unknown> | undefined {
16+
const isPickup = deliveryMethod === 'PICKUP';
17+
18+
if (isPickup) {
19+
const pickupLocationAddress = locations?.find(
20+
loc => loc.id === pickupLocationId
21+
)?.address;
22+
return pickupLocationAddress;
23+
}
24+
25+
// For shipping, return undefined to let backend use saved shipping address
26+
return undefined;
27+
}
28+
29+
describe('useUpdateOrder - Tax calculation address logic', () => {
30+
const mockLocations = [
31+
{
32+
id: 'location-1',
33+
address: {
34+
addressLine1: '599 Stegall Dr',
35+
adminArea1: 'GA',
36+
adminArea2: 'Jasper',
37+
countryCode: 'US',
38+
postalCode: '30143',
39+
},
40+
},
41+
{
42+
id: 'location-2',
43+
address: {
44+
addressLine1: '123 Main St',
45+
adminArea1: 'NY',
46+
adminArea2: 'New York',
47+
countryCode: 'US',
48+
postalCode: '10001',
49+
},
50+
},
51+
];
52+
53+
describe('Pickup orders', () => {
54+
it('should return pickup location address for PICKUP delivery method', () => {
55+
const result = getTaxDestinationAddress(
56+
'PICKUP',
57+
'location-1',
58+
mockLocations
59+
);
60+
61+
expect(result).toEqual({
62+
addressLine1: '599 Stegall Dr',
63+
adminArea1: 'GA',
64+
adminArea2: 'Jasper',
65+
countryCode: 'US',
66+
postalCode: '30143',
67+
});
68+
});
69+
70+
it('should return correct location when multiple locations exist', () => {
71+
const result = getTaxDestinationAddress(
72+
'PICKUP',
73+
'location-2',
74+
mockLocations
75+
);
76+
77+
expect(result).toEqual({
78+
addressLine1: '123 Main St',
79+
adminArea1: 'NY',
80+
adminArea2: 'New York',
81+
countryCode: 'US',
82+
postalCode: '10001',
83+
});
84+
});
85+
86+
it('should return undefined if pickup location not found', () => {
87+
const result = getTaxDestinationAddress(
88+
'PICKUP',
89+
'non-existent-location',
90+
mockLocations
91+
);
92+
93+
expect(result).toBeUndefined();
94+
});
95+
96+
it('should return undefined if locations array is empty', () => {
97+
const result = getTaxDestinationAddress('PICKUP', 'location-1', []);
98+
99+
expect(result).toBeUndefined();
100+
});
101+
102+
it('should return undefined if locations is undefined', () => {
103+
const result = getTaxDestinationAddress(
104+
'PICKUP',
105+
'location-1',
106+
undefined
107+
);
108+
109+
expect(result).toBeUndefined();
110+
});
111+
});
112+
113+
describe('Shipping orders', () => {
114+
it('should return undefined for SHIP delivery method', () => {
115+
const result = getTaxDestinationAddress(
116+
'SHIP',
117+
'location-1',
118+
mockLocations
119+
);
120+
121+
expect(result).toBeUndefined();
122+
});
123+
124+
it('should return undefined when delivery method is undefined', () => {
125+
const result = getTaxDestinationAddress(
126+
undefined,
127+
'location-1',
128+
mockLocations
129+
);
130+
131+
expect(result).toBeUndefined();
132+
});
133+
});
134+
135+
describe('Edge cases', () => {
136+
it('should handle pickupLocationId being undefined for pickup', () => {
137+
const result = getTaxDestinationAddress('PICKUP', undefined, mockLocations);
138+
139+
expect(result).toBeUndefined();
140+
});
141+
142+
it('should handle empty string pickupLocationId for pickup', () => {
143+
const result = getTaxDestinationAddress('PICKUP', '', mockLocations);
144+
145+
expect(result).toBeUndefined();
146+
});
147+
});
148+
});

packages/react/src/components/checkout/order/use-update-order.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useMutation, useQueryClient } from '@tanstack/react-query';
2+
import { useFormContext } from 'react-hook-form';
23
import { useCheckoutContext } from '@/components/checkout/checkout';
4+
import { DeliveryMethods } from '@/components/checkout/delivery/delivery-method';
35
import { useUpdateTaxes } from '@/components/checkout/order/use-update-taxes';
46
import { useGoDaddyContext } from '@/godaddy-provider';
57
import { updateDraftOrder } from '@/lib/godaddy/godaddy';
@@ -10,6 +12,7 @@ export function useUpdateOrder() {
1012
const { apiHost } = useGoDaddyContext();
1113
const updateTaxes = useUpdateTaxes();
1214
const queryClient = useQueryClient();
15+
const form = useFormContext();
1316

1417
return useMutation({
1518
mutationKey: ['update-draft-order'],
@@ -32,7 +35,22 @@ export function useUpdateOrder() {
3235
(!input.shipping?.address && input.billing?.address)
3336
) {
3437
if (session?.enableTaxCollection) {
35-
updateTaxes.mutate(undefined);
38+
// For pickup orders, always use the pickup location address for tax calculation
39+
const deliveryMethod = form?.getValues?.('deliveryMethod');
40+
const isPickup = deliveryMethod === DeliveryMethods.PICKUP;
41+
42+
if (isPickup) {
43+
const pickupLocationId = form?.getValues?.('pickupLocationId');
44+
const pickupLocationAddress = session?.locations?.find(
45+
loc => loc.id === pickupLocationId
46+
)?.address;
47+
48+
// Always send pickup location address for pickup orders
49+
updateTaxes.mutate(pickupLocationAddress);
50+
} else {
51+
// For shipping, let backend use the saved shipping address
52+
updateTaxes.mutate(undefined);
53+
}
3654
} else {
3755
queryClient.invalidateQueries({
3856
queryKey: ['draft-order', session.id],

0 commit comments

Comments
 (0)