Skip to content

Commit

Permalink
Fix incorrect amounts caused by zero-decimal currencies on JS CSV exp…
Browse files Browse the repository at this point in the history
…ort (#7863)
  • Loading branch information
mordeth authored Dec 8, 2023
1 parent 377429f commit fc0f89d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-7834-zero-decimals-csv-export
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix incorrect amounts caused by zero-decimal currencies on Transactions, Deposits and Deposits CSV export
4 changes: 2 additions & 2 deletions client/deposits/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { useDispatch } from '@wordpress/data';
*/
import { useDeposits, useDepositsSummary } from 'wcpay/data';
import { displayType, displayStatus } from '../strings';
import { formatExplicitCurrency } from 'utils/currency';
import { formatExplicitCurrency, formatExportAmount } from 'utils/currency';
import DetailsLink, { getDetailsURL } from 'components/details-link';
import ClickableCell from 'components/clickable-cell';
import Page from '../../components/page';
Expand Down Expand Up @@ -143,7 +143,7 @@ export const DepositsList = (): JSX.Element => {
display: clickable( displayType[ deposit.type ] ),
},
amount: {
value: deposit.amount / 100,
value: formatExportAmount( deposit.amount, deposit.currency ),
display: clickable(
formatExplicitCurrency( deposit.amount, deposit.currency )
),
Expand Down
4 changes: 2 additions & 2 deletions client/disputes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Page from 'components/page';
import { TestModeNotice } from 'components/test-mode-notice';
import { reasons } from './strings';
import { formatStringValue } from 'utils';
import { formatExplicitCurrency } from 'utils/currency';
import { formatExplicitCurrency, formatExportAmount } from 'utils/currency';
import DisputesFilters from './filters';
import DownloadButton from 'components/download-button';
import disputeStatusMapping from 'components/dispute-status-chip/mappings';
Expand Down Expand Up @@ -232,7 +232,7 @@ export const DisputesList = (): JSX.Element => {
};
} = {
amount: {
value: dispute.amount / 100,
value: formatExportAmount( dispute.amount, dispute.currency ),
display: clickable(
formatExplicitCurrency( dispute.amount, dispute.currency )
),
Expand Down
9 changes: 7 additions & 2 deletions client/payment-details/readers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useCardReaderStats } from 'wcpay/data';
import { TestModeNotice } from 'components/test-mode-notice';
import Page from 'components/page';
import DownloadButton from 'components/download-button';
import { formatExplicitCurrency } from 'utils/currency';
import { formatExplicitCurrency, formatExportAmount } from 'utils/currency';

const PaymentCardReaderChargeDetails = ( props ) => {
const { readers, chargeError, isLoading } = useCardReaderStats(
Expand Down Expand Up @@ -100,7 +100,12 @@ const RenderPaymentCardReaderChargeDetails = ( props ) => {
display: reader.count,
},
{
value: reader.fee ? reader.fee.amount / 100 : 0,
value: reader.fee
? formatExportAmount(
reader.fee.amount,
reader.fee.currency
)
: 0,
display: reader.fee
? formatExplicitCurrency(
reader.fee.amount,
Expand Down
16 changes: 11 additions & 5 deletions client/transactions/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ import { getDetailsURL } from 'components/details-link';
import { displayType } from 'transactions/strings';
import { displayStatus as displayDepositStatus } from 'deposits/strings';
import { formatStringValue } from 'utils';
import { formatCurrency, formatExplicitCurrency } from 'utils/currency';
import {
formatCurrency,
formatExplicitCurrency,
formatExportAmount,
} from 'utils/currency';
import { getChargeChannel } from 'utils/charge';
import Deposit from './deposit';
import ConvertedAmount from './converted-amount';
Expand Down Expand Up @@ -343,7 +347,7 @@ export const TransactionsList = (
const fromAmount = txn.customer_amount ? txn.customer_amount : 0;

return {
value: amount / 100,
value: formatExportAmount( amount, currency ),
display: clickable(
<ConvertedAmount
amount={ amount }
Expand All @@ -357,8 +361,10 @@ export const TransactionsList = (
const formatFees = () => {
const isCardReader =
txn.metadata && txn.metadata.charge_type === 'card_reader_fee';
const feeAmount =
( isCardReader ? txn.amount : txn.fees * -1 ) / 100;
const feeAmount = formatExportAmount(
isCardReader ? txn.amount : txn.fees * -1,
currency
);
return {
value: feeAmount,
display: clickable(
Expand Down Expand Up @@ -461,7 +467,7 @@ export const TransactionsList = (
// fees should display as negative. The format $-9.99 is determined by WC-Admin
fees: formatFees(),
net: {
value: txn.net / 100,
value: formatExportAmount( txn.net, currency ),
display: clickable(
formatExplicitCurrency( txn.net, currency )
),
Expand Down
18 changes: 18 additions & 0 deletions client/utils/currency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ export const isZeroDecimalCurrency = ( currencyCode ) => {
);
};

/**
* Formats the amount for CSV export, considering zero-decimal currencies
*
* @param {number} amount Amount
* @param {string} currencyCode Currency code
*
* @return {number} Export amount
*/
export const formatExportAmount = ( amount, currencyCode ) => {
const isZeroDecimal = isZeroDecimalCurrency( currencyCode );

if ( ! isZeroDecimal ) {
amount /= 100;
}

return amount;
};

/**
* Formats amount according to the given currency.
*
Expand Down
7 changes: 7 additions & 0 deletions client/utils/currency/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,11 @@ describe( 'Currency utilities', () => {

expect( utils.formatCurrency( 100000, 'EUR' ) ).toEqual( '€1,000.00' );
} );

test( 'format export amounts', () => {
expect( utils.formatExportAmount( 1000, 'USD' ) ).toEqual( 10 );
expect( utils.formatExportAmount( 1250, 'USD' ) ).toEqual( 12.5 );
expect( utils.formatExportAmount( 1000, 'JPY' ) ).toEqual( 1000 );
expect( utils.formatExportAmount( 3450, 'JPY' ) ).toEqual( 3450 );
} );
} );

0 comments on commit fc0f89d

Please sign in to comment.