Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/stupid-lemons-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@godaddy/react": patch
---

Refactor currency formatting to respect currency precision
38 changes: 22 additions & 16 deletions packages/react/src/components/checkout/form/checkout-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ShippingMethodForm } from '@/components/checkout/shipping/shipping-meth
import { Target } from '@/components/checkout/target/target';
import { TipsForm } from '@/components/checkout/tips/tips-form';
import { DraftOrderTotals } from '@/components/checkout/totals/totals';
import { formatCurrency } from '@/components/checkout/utils/format-currency';
import {
Accordion,
AccordionContent,
Expand Down Expand Up @@ -105,22 +106,22 @@ export function CheckoutForm({
const { data: totals, isLoading: totalsLoading } = draftOrderTotalsQuery;
const { data: order } = draftOrder;

// Order summary calculations
const subtotal = (totals?.subTotal?.value || 0) / 100;
const orderDiscount = (totals?.discountTotal?.value || 0) / 100;
// Order summary calculations - keep all values in minor units
const subtotal = totals?.subTotal?.value || 0;
const orderDiscount = totals?.discountTotal?.value || 0;
const shipping =
(order?.shippingLines?.reduce(
order?.shippingLines?.reduce(
(sum, line) => sum + (line?.amount?.value || 0),
0
) || 0) / 100;
const taxTotal = (totals?.taxTotal?.value || 0) / 100;
const orderTotal = (totals?.total?.value || 0) / 100;
const tipTotal = (tipAmount || 0) / 100;
) || 0;
const taxTotal = totals?.taxTotal?.value || 0;
const orderTotal = totals?.total?.value || 0;
const tipTotal = tipAmount || 0;
const currencyCode = totals?.total?.currencyCode || 'USD';
const itemCount = items.reduce((sum, item) => sum + (item?.quantity || 0), 0);

const isFree = orderTotal <= 0;
const showExpressButtons = (totals?.subTotal?.value || 0) > 0;
const showExpressButtons = subtotal > 0;

useEffect(() => {
if (!totalsLoading && isFree) {
Expand All @@ -136,8 +137,8 @@ export function CheckoutForm({
eventId: eventIds.checkoutStart,
type: TrackingEventType.IMPRESSION,
properties: {
subtotal: Math.round(subtotal * 100),
total: Math.round(orderTotal * 100),
subtotal: subtotal,
total: orderTotal,
itemCount,
currencyCode,
},
Expand Down Expand Up @@ -244,7 +245,7 @@ export function CheckoutForm({
deliveryMethod: data.deliveryMethod,
hasShippingAddress: !!data.shippingAddressLine1,
hasBillingAddress: !!data.billingAddressLine1,
total: Math.round(orderTotal * 100),
total: orderTotal,
},
});
};
Expand Down Expand Up @@ -435,10 +436,11 @@ export function CheckoutForm({
{t.totals.orderSummary}
</span>
<span className='font-bold text-lg pr-2 self-center'>
{new Intl.NumberFormat('en-us', {
style: 'currency',
currency: currencyCode,
}).format(orderTotal)}
{formatCurrency({
amount: totals?.total?.value || 0,
currencyCode,
isInCents: true,
})}
</span>
</div>
</AccordionTrigger>
Expand All @@ -447,9 +449,11 @@ export function CheckoutForm({
<DraftOrderLineItems
currencyCode={currencyCode}
items={items}
isInCents
/>

<DraftOrderTotals
isInCents
currencyCode={currencyCode}
tip={tipTotal}
taxes={taxTotal}
Expand All @@ -476,9 +480,11 @@ export function CheckoutForm({
<DraftOrderLineItems
currencyCode={currencyCode}
items={items}
isInCents
/>

<DraftOrderTotals
isInCents
currencyCode={currencyCode}
tip={tipTotal}
taxes={taxTotal}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// import { Badge } from "@/components/ui/badge";

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

Expand Down Expand Up @@ -55,11 +56,13 @@ export type Product = Partial<SKUProduct> & {
export interface DraftOrderLineItemsProps {
items: Product[];
currencyCode?: string;
isInCents?: boolean;
}

export function DraftOrderLineItems({
items,
currencyCode = 'USD',
isInCents = false,
}: DraftOrderLineItemsProps) {
const { t } = useGoDaddyContext();

Expand Down Expand Up @@ -129,10 +132,11 @@ export function DraftOrderLineItems({
<div className='text-right'>
<div>
<span className='text-sm'>
{new Intl.NumberFormat('en-us', {
style: 'currency',
currency: currencyCode,
}).format(item.originalPrice * item.quantity)}
{formatCurrency({
amount: item.originalPrice * item.quantity,
currencyCode,
isInCents,
})}
</span>
</div>
</div>
Expand Down
Loading