Skip to content

Commit 4058d97

Browse files
keep currency in minor units and convert using new currency util (#1213)
* keep curreny in minor units and concert using new currenyFormat util for all currencies * lint fix * add isInCents props for totals and line items * add changeset * lint fixes * fix skus query and public clientid * amp fixes and precision 2 fallback * fix line item currency formatting for gdp requests --------- Co-authored-by: Wes Cole <[email protected]>
1 parent 9a497a9 commit 4058d97

File tree

12 files changed

+544
-188
lines changed

12 files changed

+544
-188
lines changed

.changeset/stupid-lemons-smash.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+
Refactor currency formatting to respect currency precision

packages/react/src/components/checkout/form/checkout-form.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { ShippingMethodForm } from '@/components/checkout/shipping/shipping-meth
3838
import { Target } from '@/components/checkout/target/target';
3939
import { TipsForm } from '@/components/checkout/tips/tips-form';
4040
import { DraftOrderTotals } from '@/components/checkout/totals/totals';
41+
import { formatCurrency } from '@/components/checkout/utils/format-currency';
4142
import {
4243
Accordion,
4344
AccordionContent,
@@ -105,22 +106,22 @@ export function CheckoutForm({
105106
const { data: totals, isLoading: totalsLoading } = draftOrderTotalsQuery;
106107
const { data: order } = draftOrder;
107108

108-
// Order summary calculations
109-
const subtotal = (totals?.subTotal?.value || 0) / 100;
110-
const orderDiscount = (totals?.discountTotal?.value || 0) / 100;
109+
// Order summary calculations - keep all values in minor units
110+
const subtotal = totals?.subTotal?.value || 0;
111+
const orderDiscount = totals?.discountTotal?.value || 0;
111112
const shipping =
112-
(order?.shippingLines?.reduce(
113+
order?.shippingLines?.reduce(
113114
(sum, line) => sum + (line?.amount?.value || 0),
114115
0
115-
) || 0) / 100;
116-
const taxTotal = (totals?.taxTotal?.value || 0) / 100;
117-
const orderTotal = (totals?.total?.value || 0) / 100;
118-
const tipTotal = (tipAmount || 0) / 100;
116+
) || 0;
117+
const taxTotal = totals?.taxTotal?.value || 0;
118+
const orderTotal = totals?.total?.value || 0;
119+
const tipTotal = tipAmount || 0;
119120
const currencyCode = totals?.total?.currencyCode || 'USD';
120121
const itemCount = items.reduce((sum, item) => sum + (item?.quantity || 0), 0);
121122

122123
const isFree = orderTotal <= 0;
123-
const showExpressButtons = (totals?.subTotal?.value || 0) > 0;
124+
const showExpressButtons = subtotal > 0;
124125

125126
useEffect(() => {
126127
if (!totalsLoading && isFree) {
@@ -136,8 +137,8 @@ export function CheckoutForm({
136137
eventId: eventIds.checkoutStart,
137138
type: TrackingEventType.IMPRESSION,
138139
properties: {
139-
subtotal: Math.round(subtotal * 100),
140-
total: Math.round(orderTotal * 100),
140+
subtotal: subtotal,
141+
total: orderTotal,
141142
itemCount,
142143
currencyCode,
143144
},
@@ -244,7 +245,7 @@ export function CheckoutForm({
244245
deliveryMethod: data.deliveryMethod,
245246
hasShippingAddress: !!data.shippingAddressLine1,
246247
hasBillingAddress: !!data.billingAddressLine1,
247-
total: Math.round(orderTotal * 100),
248+
total: orderTotal,
248249
},
249250
});
250251
};
@@ -435,10 +436,11 @@ export function CheckoutForm({
435436
{t.totals.orderSummary}
436437
</span>
437438
<span className='font-bold text-lg pr-2 self-center'>
438-
{new Intl.NumberFormat('en-us', {
439-
style: 'currency',
440-
currency: currencyCode,
441-
}).format(orderTotal)}
439+
{formatCurrency({
440+
amount: totals?.total?.value || 0,
441+
currencyCode,
442+
isInCents: true,
443+
})}
442444
</span>
443445
</div>
444446
</AccordionTrigger>
@@ -447,9 +449,11 @@ export function CheckoutForm({
447449
<DraftOrderLineItems
448450
currencyCode={currencyCode}
449451
items={items}
452+
isInCents
450453
/>
451454

452455
<DraftOrderTotals
456+
isInCents
453457
currencyCode={currencyCode}
454458
tip={tipTotal}
455459
taxes={taxTotal}
@@ -476,9 +480,11 @@ export function CheckoutForm({
476480
<DraftOrderLineItems
477481
currencyCode={currencyCode}
478482
items={items}
483+
isInCents
479484
/>
480485

481486
<DraftOrderTotals
487+
isInCents
482488
currencyCode={currencyCode}
483489
tip={tipTotal}
484490
taxes={taxTotal}

packages/react/src/components/checkout/line-items/line-items.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// import { Badge } from "@/components/ui/badge";
22

33
import { Image } from 'lucide-react';
4+
import { formatCurrency } from '@/components/checkout/utils/format-currency';
45
import { useGoDaddyContext } from '@/godaddy-provider';
56
import type { SKUProduct } from '@/types';
67

@@ -55,11 +56,13 @@ export type Product = Partial<SKUProduct> & {
5556
export interface DraftOrderLineItemsProps {
5657
items: Product[];
5758
currencyCode?: string;
59+
isInCents?: boolean;
5860
}
5961

6062
export function DraftOrderLineItems({
6163
items,
6264
currencyCode = 'USD',
65+
isInCents = false,
6366
}: DraftOrderLineItemsProps) {
6467
const { t } = useGoDaddyContext();
6568

@@ -129,10 +132,11 @@ export function DraftOrderLineItems({
129132
<div className='text-right'>
130133
<div>
131134
<span className='text-sm'>
132-
{new Intl.NumberFormat('en-us', {
133-
style: 'currency',
134-
currency: currencyCode,
135-
}).format(item.originalPrice * item.quantity)}
135+
{formatCurrency({
136+
amount: item.originalPrice * item.quantity,
137+
currencyCode,
138+
isInCents,
139+
})}
136140
</span>
137141
</div>
138142
</div>

0 commit comments

Comments
 (0)