Skip to content

Commit 41f7d3b

Browse files
use full tax and discount data for confirm checkout (#1268)
* use full tax and discount data for confirm checkout * fix tax and discount input types * add changeset
1 parent 0ce173c commit 41f7d3b

File tree

7 files changed

+496
-176
lines changed

7 files changed

+496
-176
lines changed

.changeset/two-ducks-enter.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+
Send new tax, price adjustment, and shipping data to express checkout confirmation

packages/react/src/components/checkout/discount/utils/use-get-price-adjustments.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { useMutation } from '@tanstack/react-query';
33
import { useCheckoutContext } from '@/components/checkout/checkout';
44
import { useGoDaddyContext } from '@/godaddy-provider';
55
import { getDraftOrderPriceAdjustments } from '@/lib/godaddy/godaddy';
6-
import type { DraftOrderPriceAdjustmentsQueryInput } from '@/types';
6+
import type {
7+
CalculatedAdjustments,
8+
DraftOrderPriceAdjustmentsQueryInput,
9+
} from '@/types';
710

811
type Vars = {
912
discountCodes: DraftOrderPriceAdjustmentsQueryInput['discountCodes'];
@@ -14,7 +17,7 @@ export function useGetPriceAdjustments() {
1417
const { session, jwt } = useCheckoutContext();
1518
const { apiHost } = useGoDaddyContext();
1619

17-
return useMutation<number | null | undefined, Error, Vars>({
20+
return useMutation<CalculatedAdjustments | null | undefined, Error, Vars>({
1821
mutationKey: [
1922
'get-price-adjustments-by-discount-code',
2023
session?.id ?? 'no-session',
@@ -34,8 +37,7 @@ export function useGetPriceAdjustments() {
3437
apiHost
3538
);
3639

37-
return data.checkoutSession?.draftOrder?.calculatedAdjustments
38-
?.totalDiscountAmount?.value;
40+
return data.checkoutSession?.draftOrder?.calculatedAdjustments;
3941
},
4042
});
4143
}

packages/react/src/components/checkout/payment/checkout-buttons/express/godaddy.tsx

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
TrackingEventType,
4848
track,
4949
} from '@/tracking/track';
50+
import type { CalculatedAdjustments, CalculatedTaxes } from '@/types';
5051

5152
export function ExpressCheckoutButton() {
5253
const formatCurrency = useFormatCurrency();
@@ -60,7 +61,10 @@ export function ExpressCheckoutButton() {
6061
undefined
6162
);
6263
const [error, setError] = useState('');
63-
const [priceAdjustment, setPriceAdjustment] = useState<number | null>(null);
64+
const [calculatedAdjustments, setCalculatedAdjustments] =
65+
useState<CalculatedAdjustments | null>(null);
66+
const [calculatedTaxes, setCalculatedTaxes] =
67+
useState<CalculatedTaxes | null>(null);
6468
const [appliedCouponCode, setAppliedCouponCode] = useState<string | null>(
6569
null
6670
);
@@ -177,7 +181,7 @@ export function ExpressCheckoutButton() {
177181
let expressRequest = { ...poyntExpressRequest };
178182

179183
// If there's an applied coupon code and price adjustment, add it to the request
180-
if (appliedCouponCode && priceAdjustment !== null) {
184+
if (appliedCouponCode && calculatedAdjustments?.totalDiscountAmount) {
181185
// console.log("[poynt collect] Adding discount to express request", {
182186
// appliedCouponCode,
183187
// priceAdjustment,
@@ -189,7 +193,7 @@ export function ExpressCheckoutButton() {
189193
updatedLineItems.push({
190194
label: t.totals.discount,
191195
amount: formatCurrency({
192-
amount: -priceAdjustment,
196+
amount: -(calculatedAdjustments?.totalDiscountAmount?.value || 0),
193197
currencyCode,
194198
inputInMinorUnits: true,
195199
returnRaw: true,
@@ -199,7 +203,8 @@ export function ExpressCheckoutButton() {
199203

200204
// Calculate the correct total in minor units
201205
const totalInMinorUnits =
202-
(totals?.subTotal?.value || 0) - priceAdjustment;
206+
(totals?.subTotal?.value || 0) -
207+
(calculatedAdjustments?.totalDiscountAmount?.value || 0);
203208

204209
const totalAmount = formatCurrency({
205210
amount: totalInMinorUnits,
@@ -220,7 +225,7 @@ export function ExpressCheckoutButton() {
220225
code: appliedCouponCode,
221226
label: t.totals.discount,
222227
amount: formatCurrency({
223-
amount: -priceAdjustment,
228+
amount: -(calculatedAdjustments?.totalDiscountAmount?.value || 0),
224229
currencyCode,
225230
inputInMinorUnits: true,
226231
returnRaw: true,
@@ -270,7 +275,7 @@ export function ExpressCheckoutButton() {
270275
[
271276
poyntExpressRequest,
272277
appliedCouponCode,
273-
priceAdjustment,
278+
calculatedAdjustments,
274279
t,
275280
setCheckoutErrors,
276281
]
@@ -322,7 +327,7 @@ export function ExpressCheckoutButton() {
322327

323328
if (result) {
324329
setAppliedCouponCode(discountCodes?.[0]);
325-
setPriceAdjustment(result);
330+
setCalculatedAdjustments(result);
326331
}
327332
}
328333
// Mark the fetch as complete regardless of whether there were discounts
@@ -355,12 +360,12 @@ export function ExpressCheckoutButton() {
355360
let couponConfig:
356361
| { code: string; label: string; amount: string }
357362
| undefined;
358-
if (priceAdjustment && appliedCouponCode) {
363+
if (calculatedAdjustments?.totalDiscountAmount && appliedCouponCode) {
359364
couponConfig = {
360365
code: appliedCouponCode,
361366
label: t.totals.discount,
362367
amount: formatCurrency({
363-
amount: priceAdjustment,
368+
amount: calculatedAdjustments?.totalDiscountAmount?.value || 0,
364369
currencyCode,
365370
inputInMinorUnits: true,
366371
returnRaw: true,
@@ -392,7 +397,7 @@ export function ExpressCheckoutButton() {
392397
session,
393398
isPoyntLoaded,
394399
isCollectLoading,
395-
priceAdjustment,
400+
calculatedAdjustments,
396401
appliedCouponCode,
397402
draftOrder,
398403
couponFetchStatus,
@@ -550,7 +555,7 @@ export function ExpressCheckoutButton() {
550555
if (!couponCode) {
551556
// User removed the coupon code
552557
setAppliedCouponCode(null);
553-
setPriceAdjustment(null);
558+
setCalculatedAdjustments(null);
554559

555560
// Add shipping and taxes if they exist
556561
const finalLineItems = [...baseLineItems];
@@ -621,14 +626,16 @@ export function ExpressCheckoutButton() {
621626
}
622627

623628
// Call the price adjustments mutation with the new coupon code
624-
const adjustment = await getPriceAdjustments.mutateAsync({
629+
const adjustments = await getPriceAdjustments.mutateAsync({
625630
discountCodes: [couponCode],
626631
shippingLines,
627632
});
628633

629-
if (adjustment) {
634+
if (adjustments?.totalDiscountAmount?.value) {
630635
setAppliedCouponCode(couponCode);
631-
setPriceAdjustment(adjustment);
636+
setCalculatedAdjustments(adjustments);
637+
638+
const adjustmentValue = adjustments.totalDiscountAmount.value;
632639

633640
// Build line items with shipping, taxes, and the new discount
634641
const finalLineItems = [...baseLineItems];
@@ -661,7 +668,7 @@ export function ExpressCheckoutButton() {
661668
finalLineItems.push({
662669
label: t.totals.discount,
663670
amount: formatCurrency({
664-
amount: -adjustment,
671+
amount: -adjustmentValue,
665672
currencyCode,
666673
inputInMinorUnits: true,
667674
returnRaw: true,
@@ -673,7 +680,7 @@ export function ExpressCheckoutButton() {
673680
(totals?.subTotal?.value || 0) +
674681
godaddyTotals.shipping.value +
675682
godaddyTotals.taxes.value -
676-
adjustment;
683+
adjustmentValue;
677684

678685
const totalAmount = formatCurrency({
679686
amount: totalInMinorUnits,
@@ -692,7 +699,7 @@ export function ExpressCheckoutButton() {
692699
code: couponCode,
693700
label: t.totals.discount,
694701
amount: formatCurrency({
695-
amount: -adjustment,
702+
amount: -adjustmentValue,
696703
currencyCode,
697704
inputInMinorUnits: true,
698705
returnRaw: true,
@@ -766,14 +773,8 @@ export function ExpressCheckoutButton() {
766773
shippingTotal: godaddyTotals.shipping,
767774
}
768775
: {}),
769-
...(godaddyTotals.taxes
770-
? {
771-
taxTotal: {
772-
value: godaddyTotals.taxes.value,
773-
currencyCode: godaddyTotals.taxes.currencyCode,
774-
},
775-
}
776-
: {}),
776+
...(calculatedTaxes ? { calculatedTaxes } : {}),
777+
...(calculatedAdjustments ? { calculatedAdjustments } : {}),
777778
...(event?.billingAddress
778779
? {
779780
billing: {
@@ -958,10 +959,10 @@ export function ExpressCheckoutButton() {
958959
shippingLines,
959960
});
960961

961-
if (newAdjustments) {
962-
setPriceAdjustment(newAdjustments);
962+
if (newAdjustments?.totalDiscountAmount) {
963+
setCalculatedAdjustments(newAdjustments);
963964
} else {
964-
setPriceAdjustment(null);
965+
setCalculatedAdjustments(null);
965966
setAppliedCouponCode('');
966967
}
967968
} catch (err) {
@@ -988,11 +989,14 @@ export function ExpressCheckoutButton() {
988989
shippingAmount || '0'
989990
);
990991

991-
if (taxesResult?.value) {
992+
if (taxesResult?.totalTaxAmount?.value) {
993+
// Store the full tax calculation response
994+
setCalculatedTaxes(taxesResult);
995+
992996
poyntLineItems.push({
993997
label: t.totals.estimatedTaxes,
994998
amount: formatCurrency({
995-
amount: taxesResult.value,
999+
amount: taxesResult?.totalTaxAmount?.value,
9961000
currencyCode,
9971001
inputInMinorUnits: true,
9981002
returnRaw: true,
@@ -1003,7 +1007,7 @@ export function ExpressCheckoutButton() {
10031007
...value,
10041008
taxes: {
10051009
currencyCode: currencyCode,
1006-
value: taxesResult.value || 0,
1010+
value: taxesResult?.totalTaxAmount?.value || 0,
10071011
},
10081012
}));
10091013
}
@@ -1024,11 +1028,11 @@ export function ExpressCheckoutButton() {
10241028
}
10251029

10261030
// Add discount line if a coupon is applied
1027-
if (priceAdjustment && appliedCouponCode) {
1031+
if (calculatedAdjustments?.totalDiscountAmount && appliedCouponCode) {
10281032
poyntLineItems.push({
10291033
label: t.totals.discount,
10301034
amount: formatCurrency({
1031-
amount: -priceAdjustment,
1035+
amount: -(calculatedAdjustments?.totalDiscountAmount?.value || 0),
10321036
currencyCode,
10331037
inputInMinorUnits: true,
10341038
returnRaw: true,
@@ -1057,12 +1061,12 @@ export function ExpressCheckoutButton() {
10571061
};
10581062

10591063
// Add coupon code to the request if one is applied
1060-
if (appliedCouponCode && priceAdjustment !== null) {
1064+
if (appliedCouponCode && calculatedAdjustments?.totalDiscountAmount) {
10611065
updatedOrder.couponCode = {
10621066
code: appliedCouponCode,
10631067
label: t.totals.discount,
10641068
amount: formatCurrency({
1065-
amount: -priceAdjustment,
1069+
amount: -(calculatedAdjustments?.totalDiscountAmount?.value || 0),
10661070
currencyCode,
10671071
inputInMinorUnits: true,
10681072
returnRaw: true,
@@ -1126,10 +1130,10 @@ export function ExpressCheckoutButton() {
11261130
shippingLines,
11271131
});
11281132

1129-
if (newAdjustments) {
1130-
setPriceAdjustment(newAdjustments);
1133+
if (newAdjustments?.totalDiscountAmount) {
1134+
setCalculatedAdjustments(newAdjustments);
11311135
} else {
1132-
setPriceAdjustment(null);
1136+
setCalculatedAdjustments(null);
11331137
setAppliedCouponCode('');
11341138
}
11351139
} catch (err) {
@@ -1174,11 +1178,14 @@ export function ExpressCheckoutButton() {
11741178

11751179
// console.log("[poynt collect] Taxes result", { taxesResult });
11761180

1177-
if (taxesResult?.value) {
1181+
if (taxesResult?.totalTaxAmount?.value) {
1182+
// Store the full tax calculation response
1183+
setCalculatedTaxes(taxesResult);
1184+
11781185
poyntLineItems.push({
11791186
label: t.totals.estimatedTaxes,
11801187
amount: formatCurrency({
1181-
amount: taxesResult.value,
1188+
amount: taxesResult.totalTaxAmount.value,
11821189
currencyCode,
11831190
inputInMinorUnits: true,
11841191
returnRaw: true,
@@ -1189,7 +1196,7 @@ export function ExpressCheckoutButton() {
11891196
...value,
11901197
taxes: {
11911198
currencyCode: currencyCode,
1192-
value: taxesResult.value || 0,
1199+
value: taxesResult?.totalTaxAmount?.value || 0,
11931200
},
11941201
}));
11951202
}
@@ -1213,11 +1220,11 @@ export function ExpressCheckoutButton() {
12131220
}
12141221

12151222
// Add discount line if a coupon is applied
1216-
if (priceAdjustment && appliedCouponCode) {
1223+
if (calculatedAdjustments?.totalDiscountAmount && appliedCouponCode) {
12171224
poyntLineItems.push({
12181225
label: t.totals.discount,
12191226
amount: formatCurrency({
1220-
amount: -priceAdjustment,
1227+
amount: -(calculatedAdjustments?.totalDiscountAmount?.value || 0),
12211228
currencyCode,
12221229
inputInMinorUnits: true,
12231230
returnRaw: true,
@@ -1247,12 +1254,12 @@ export function ExpressCheckoutButton() {
12471254
};
12481255

12491256
// Add coupon code to the request if one is applied
1250-
if (appliedCouponCode && priceAdjustment !== null) {
1257+
if (appliedCouponCode && calculatedAdjustments?.totalDiscountAmount) {
12511258
updatedOrder.couponCode = {
12521259
code: appliedCouponCode,
12531260
label: appliedCouponCode || 'Discount',
12541261
amount: formatCurrency({
1255-
amount: -priceAdjustment,
1262+
amount: -(calculatedAdjustments?.totalDiscountAmount?.value || 0),
12561263
currencyCode,
12571264
inputInMinorUnits: true,
12581265
returnRaw: true,
@@ -1296,7 +1303,7 @@ export function ExpressCheckoutButton() {
12961303
getSortedShippingMethods,
12971304
convertAddressToShippingLines,
12981305
getPriceAdjustments.mutateAsync,
1299-
priceAdjustment,
1306+
calculatedAdjustments,
13001307
appliedCouponCode,
13011308
t,
13021309
form,

packages/react/src/components/checkout/taxes/utils/use-get-taxes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function useGetTaxes() {
2929
)
3030
: await getDraftOrderTaxes(session, { destination, lines }, apiHost);
3131

32-
return data.checkoutSession?.draftOrder?.calculatedTaxes?.totalTaxAmount;
32+
return data.checkoutSession?.draftOrder?.calculatedTaxes;
3333
},
3434
});
3535
}

0 commit comments

Comments
 (0)