Skip to content
Closed
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/eager-numbers-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@godaddy/react": patch
---

Use formatCurrency util to respect all currency precisions
20 changes: 9 additions & 11 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 @@ -106,16 +107,16 @@ export function CheckoutForm({
const { data: order } = draftOrder;

// Order summary calculations
const subtotal = (totals?.subTotal?.value || 0) / 100;
const orderDiscount = (totals?.discountTotal?.value || 0) / 100;
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);

Expand Down Expand Up @@ -427,10 +428,7 @@ 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: orderTotal, currencyCode })}
</span>
</div>
</AccordionTrigger>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Image } from 'lucide-react';
import { useGoDaddyContext } from '@/godaddy-provider';
import type { SKUProduct } from '@/types';
import { formatCurrency } from '../utils/format-currency';

export interface Note {
content: string | null;
Expand Down Expand Up @@ -129,10 +130,10 @@ 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,
})}
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { filterAndSortShippingMethods } from '@/components/checkout/shipping/uti
import { useGetShippingMethodByAddress } from '@/components/checkout/shipping/utils/use-get-shipping-methods';
import { useGetTaxes } from '@/components/checkout/taxes/utils/use-get-taxes';
import { mapOrderToFormValues } from '@/components/checkout/utils/checkout-transformers';
import { formatCurrency } from '@/components/checkout/utils/format-currency';
import { Skeleton } from '@/components/ui/skeleton';
import { useGoDaddyContext } from '@/godaddy-provider';
import { GraphQLErrorWithCodes } from '@/lib/graphql-with-errors';
Expand Down Expand Up @@ -100,7 +101,13 @@ export function ExpressCheckoutButton() {
type: 'SHIPPING' as const,
subtotalPrice: {
currencyCode: currency,
value: Number(amount) * 100 || 0,
value: Number(
formatCurrency({
amount: Number(amount) || 0,
currencyCode: currency,
isInCents: false,
})
),
},
},
],
Expand Down Expand Up @@ -133,18 +140,22 @@ export function ExpressCheckoutButton() {
});

const methods = sortedMethods?.map(method => {
const shippingMethodPrice = new Intl.NumberFormat('en-us', {
style: 'currency',
currency: method.cost?.currencyCode || 'USD',
}).format((method.cost?.value || 0) / 100);
const shippingMethodPrice = formatCurrency({
amount: method.cost?.value || 0,
currencyCode: method.cost?.currencyCode || 'USD',
});

return {
id: method?.displayName?.replace(/\s+/g, '-')?.toLowerCase(),
label: method.displayName || '',
detail: method.description
? `(${method.description}) ${shippingMethodPrice}`
: `${shippingMethodPrice}`,
amount: ((method.cost?.value || 0) / 100).toString(),
amount: formatCurrency({
amount: method.cost?.value || 0,
currencyCode,
returnRaw: true,
}),
};
});

Expand Down Expand Up @@ -173,7 +184,11 @@ export function ExpressCheckoutButton() {
// Always add the discount line item, using state variables directly
updatedLineItems.push({
label: t.totals.discount,
amount: (-priceAdjustment / 100).toString(),
amount: formatCurrency({
amount: -priceAdjustment,
currencyCode,
returnRaw: true,
}),
isPending: false,
});

Expand All @@ -194,7 +209,11 @@ export function ExpressCheckoutButton() {
couponCode: {
code: appliedCouponCode,
label: t.totals.discount,
amount: (-priceAdjustment / 100).toString(),
amount: formatCurrency({
amount: -priceAdjustment,
currencyCode,
returnRaw: true,
}),
},
};
} else {
Expand Down Expand Up @@ -328,7 +347,11 @@ export function ExpressCheckoutButton() {
couponConfig = {
code: appliedCouponCode,
label: t.totals.discount,
amount: (priceAdjustment / 100).toString(),
amount: formatCurrency({
amount: -priceAdjustment,
currencyCode,
returnRaw: true,
}),
};
}

Expand Down Expand Up @@ -439,7 +462,13 @@ export function ExpressCheckoutButton() {
{
subTotal: {
currencyCode: currencyCode,
value: Number(selectedMethod.amount) * 100,
value: Number(
formatCurrency({
amount: Number(selectedMethod.amount),
currencyCode,
isInCents: false,
})
),
},
name: selectedMethod.name,
},
Expand Down Expand Up @@ -520,14 +549,22 @@ export function ExpressCheckoutButton() {
if (godaddyTotals.shipping.value > 0) {
finalLineItems.push({
label: 'Shipping',
amount: (godaddyTotals.shipping.value / 100).toString(),
amount: formatCurrency({
amount: godaddyTotals.shipping.value,
currencyCode,
returnRaw: true,
}),
});
}

if (godaddyTotals.taxes.value > 0) {
finalLineItems.push({
label: t.totals.estimatedTaxes,
amount: (godaddyTotals.taxes.value / 100).toString(),
amount: formatCurrency({
amount: godaddyTotals.taxes.value,
currencyCode,
returnRaw: true,
}),
});
}

Expand Down Expand Up @@ -555,7 +592,11 @@ export function ExpressCheckoutButton() {
let shippingLines: ReturnType<typeof convertAddressToShippingLines>;
if (shippingAddress && shippingMethod) {
const selectedMethodInfo = {
amount: (godaddyTotals.shipping.value / 100).toString(),
amount: formatCurrency({
amount: godaddyTotals.shipping.value,
currencyCode,
returnRaw: true,
}),
name: shippingMethod,
};
shippingLines = convertAddressToShippingLines(
Expand All @@ -580,21 +621,33 @@ export function ExpressCheckoutButton() {
if (godaddyTotals.shipping.value > 0) {
finalLineItems.push({
label: t.totals.shipping,
amount: (godaddyTotals.shipping.value / 100).toString(),
amount: formatCurrency({
amount: godaddyTotals.shipping.value,
currencyCode,
returnRaw: true,
}),
});
}

if (godaddyTotals.taxes.value > 0) {
finalLineItems.push({
label: t.totals.estimatedTaxes,
amount: (godaddyTotals.taxes.value / 100).toString(),
amount: formatCurrency({
amount: godaddyTotals.taxes.value,
currencyCode,
returnRaw: true,
}),
});
}

// Add the discount line item
finalLineItems.push({
label: t.totals.discount,
amount: (-adjustment / 100).toString(),
amount: formatCurrency({
amount: -adjustment,
currencyCode,
returnRaw: true,
}),
});

// Calculate the total amount
Expand All @@ -611,7 +664,11 @@ export function ExpressCheckoutButton() {
couponCode: {
code: couponCode,
label: t.totals.discount,
amount: (-adjustment / 100).toString(),
amount: formatCurrency({
amount: -adjustment,
currencyCode,
returnRaw: true,
}),
},
};
} else {
Expand Down Expand Up @@ -844,7 +901,13 @@ export function ExpressCheckoutButton() {
...value,
shipping: {
currencyCode: 'USD',
value: Number(shippingAmount) * 100 || 0,
value: Number(
formatCurrency({
amount: Number(shippingAmount) || 0,
currencyCode,
isInCents: false,
})
),
},
}));

Expand Down Expand Up @@ -897,7 +960,11 @@ export function ExpressCheckoutButton() {
if (taxesResult?.value) {
poyntLineItems.push({
label: t.totals.estimatedTaxes,
amount: (taxesResult.value / 100).toString(),
amount: formatCurrency({
amount: taxesResult.value,
currencyCode,
returnRaw: true,
}),
isPending: false,
});
setGoDaddyTotals(value => ({
Expand Down Expand Up @@ -925,7 +992,11 @@ export function ExpressCheckoutButton() {
if (priceAdjustment && appliedCouponCode) {
poyntLineItems.push({
label: t.totals.discount,
amount: (-priceAdjustment / 100).toString(),
amount: formatCurrency({
amount: -priceAdjustment,
currencyCode,
returnRaw: true,
}),
isPending: false,
});
}
Expand All @@ -949,7 +1020,11 @@ export function ExpressCheckoutButton() {
updatedOrder.couponCode = {
code: appliedCouponCode,
label: t.totals.discount,
amount: (-priceAdjustment / 100).toString(),
amount: formatCurrency({
amount: -priceAdjustment,
currencyCode,
returnRaw: true,
}),
};
}

Expand Down Expand Up @@ -979,7 +1054,13 @@ export function ExpressCheckoutButton() {
...value,
shipping: {
currencyCode: 'USD',
value: Number(methods?.[0]?.amount) * 100 || 0,
value: Number(
formatCurrency({
amount: Number(methods?.[0]?.amount) || 0,
currencyCode,
isInCents: false,
})
),
},
}));

Expand Down Expand Up @@ -1060,7 +1141,11 @@ export function ExpressCheckoutButton() {
if (taxesResult?.value) {
poyntLineItems.push({
label: t.totals.estimatedTaxes,
amount: (taxesResult.value / 100).toString(),
amount: formatCurrency({
amount: taxesResult.value,
currencyCode,
returnRaw: true,
}),
isPending: false,
});
setGoDaddyTotals(value => ({
Expand Down Expand Up @@ -1094,7 +1179,11 @@ export function ExpressCheckoutButton() {
if (priceAdjustment && appliedCouponCode) {
poyntLineItems.push({
label: t.totals.discount,
amount: (-priceAdjustment / 100).toString(),
amount: formatCurrency({
amount: -priceAdjustment,
currencyCode,
returnRaw: true,
}),
isPending: false,
});
}
Expand All @@ -1119,7 +1208,11 @@ export function ExpressCheckoutButton() {
updatedOrder.couponCode = {
code: appliedCouponCode,
label: appliedCouponCode || 'Discount',
amount: (-priceAdjustment / 100).toString(),
amount: formatCurrency({
amount: -priceAdjustment,
currencyCode,
returnRaw: true,
}),
};
} else {
updatedOrder.couponCode = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
DraftOrderTotals,
type DraftOrderTotalsProps,
} from '@/components/checkout/totals/totals';
import { formatCurrency } from '@/components/checkout/utils/format-currency';
import {
Accordion,
AccordionContent,
Expand Down Expand Up @@ -447,10 +448,10 @@ export function PaymentForm(
{t.totals.orderSummary}
</span>
<span className='font-bold text-lg pr-2 self-center'>
{new Intl.NumberFormat('en-us', {
style: 'currency',
currency: props.currencyCode,
}).format(props.total || 0)}
{formatCurrency({
amount: props.total || 0,
currencyCode: props.currencyCode || 'USD',
})}
</span>
</div>
</AccordionTrigger>
Expand Down
Loading