Skip to content

Commit 20f7ca1

Browse files
apreaChris
and
Chris
authored
Fix rounding error when displaying fee percentages (Automattic#2172)
* Fix rounding error when displaying fee percentages * Add changelog entry. * Update readme.txt Co-authored-by: Chris <[email protected]@[email protected]>
1 parent 7ead985 commit 20f7ca1

File tree

10 files changed

+60
-31
lines changed

10 files changed

+60
-31
lines changed

changelog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Fix - Fix fatal error if store currency is changed after enabled (multi) currencies set.
77
* Fix - Use of deprecated call-style to registerPaymentMethods. WooCommerce Payments now requires WooCommerce Blocks of at least version 3.9.0.
88
* Fix - Deposit date on Transactions list page.
9+
* Fix - Rounding error when displaying fee percentages on the Overview and Transactions pages.
910
* Add - Error message when total size of dispute evidence files uploaded goes over limit.
1011
* Update - Pass currency to wc_price when adding intent notes to orders.
1112
* Update - Instant deposit inbox note wording.

client/components/account-status/account-fees/test/__snapshots__/index.js.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports[`AccountFees renders discounted base fee 1`] = `
77
<s>
88
2.9% + $0.30 per transaction
99
</s>
10-
2.0% + $0.20 per transaction
10+
2% + $0.20 per transaction
1111
<div
1212
class="progressbar"
1313
>
@@ -54,7 +54,7 @@ exports[`AccountFees renders discounted fee with end date 1`] = `
5454
<s>
5555
2.9% + $0.30 per transaction
5656
</s>
57-
2.0% + $0.21 per transaction (30% discount)
57+
2.03% + $0.21 per transaction (30% discount)
5858
<p
5959
class="description"
6060
>
@@ -79,7 +79,7 @@ exports[`AccountFees renders discounted fee with volume limit 1`] = `
7979
<s>
8080
2.9% + $0.30 per transaction
8181
</s>
82-
2.0% + $0.21 per transaction (30% discount)
82+
2.03% + $0.21 per transaction (30% discount)
8383
<div
8484
class="progressbar"
8585
>
@@ -126,7 +126,7 @@ exports[`AccountFees renders discounted fee with volume limit and end date 1`] =
126126
<s>
127127
2.9% + $0.30 per transaction
128128
</s>
129-
2.0% + $0.21 per transaction (30% discount)
129+
2.03% + $0.21 per transaction (30% discount)
130130
<div
131131
class="progressbar"
132132
>
@@ -173,7 +173,7 @@ exports[`AccountFees renders discounted fee without volume limit 1`] = `
173173
<s>
174174
2.9% + $0.30 per transaction
175175
</s>
176-
2.0% + $0.21 per transaction (30% discount)
176+
2.03% + $0.21 per transaction (30% discount)
177177
<p>
178178
<a
179179
href="https://woocommerce.com/terms-conditions/woocommerce-payments-promotion/"
@@ -240,7 +240,7 @@ exports[`AccountFees renders first discounted fee ignoring the rest 1`] = `
240240
<s>
241241
2.9% + $0.30 per transaction
242242
</s>
243-
2.3% + $0.24 per transaction (20% discount)
243+
2.32% + $0.24 per transaction (20% discount)
244244
<p>
245245
<a
246246
href="https://woocommerce.com/terms-conditions/woocommerce-payments-promotion/"

client/payment-details/timeline/map-events.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { __experimentalCreateInterpolateElement as createInterpolateElement } fr
1616
*/
1717
import { reasons as disputeReasons } from 'disputes/strings';
1818
import { formatCurrency, formatFX } from 'utils/currency';
19+
import { formatFee } from 'utils/fees';
1920

2021
/**
2122
* Creates a Gridicon
@@ -194,9 +195,9 @@ const composeFeeString = ( event ) => {
194195

195196
return sprintf(
196197
/* translators: %1$s is the total fee amount, %2$f%% is the fee percentage, and %3$s is the fixed fee amount. */
197-
__( 'Fee (%2$.1f%% + %3$s): %1$s', 'woocommerce-payments' ),
198+
__( 'Fee (%2$f%% + %3$s): %1$s', 'woocommerce-payments' ),
198199
formatCurrency( -feeAmount, feeCurrency ),
199-
percentage * 100,
200+
formatFee( percentage ),
200201
formatCurrency( fixed, fixedCurrency )
201202
);
202203
};

client/payment-details/timeline/test/__snapshots__/map-events.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ Array [
490490
Object {
491491
"body": Array [
492492
undefined,
493-
"Fee (1.9% + $0.15): $-3.50",
493+
"Fee (1.95% + $0.15): $-3.50",
494494
"Net deposit: $59.50",
495495
],
496496
"date": 2020-04-01T14:37:54.000Z,

client/utils/account-fees.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { __experimentalCreateInterpolateElement as createInterpolateElement } fr
1010
* Internal dependencies
1111
*/
1212
import { formatCurrency } from 'utils/currency';
13+
import { formatFee } from 'utils/fees';
1314

1415
export const getCurrentFee = ( accountFees ) => {
1516
return accountFees.discount.length
@@ -23,8 +24,8 @@ export const formatAccountFeesDescription = ( accountFees ) => {
2324

2425
let feeDescription = sprintf(
2526
/* translators: %1: Percentage part of the fee. %2: Fixed part of the fee */
26-
__( '%1$.1f%% + %2$s per transaction', 'woocommerce-payments' ),
27-
baseFee.percentage_rate * 100,
27+
__( '%1$f%% + %2$s per transaction', 'woocommerce-payments' ),
28+
formatFee( baseFee.percentage_rate ),
2829
formatCurrency( baseFee.fixed_rate, baseFee.currency )
2930
);
3031

@@ -45,21 +46,21 @@ export const formatAccountFeesDescription = ( accountFees ) => {
4546
let descriptionString = sprintf(
4647
/* translators: %1 Base fee (that don't apply to this account at this moment), %2 and %3: Current fee (e.g: 2.9% + $.30) */
4748
__(
48-
'<s>%1$s</s> %2$.1f%% + %3$s per transaction',
49+
'<s>%1$s</s> %2$f%% + %3$s per transaction',
4950
'woocommerce-payments'
5051
),
5152
feeDescription,
52-
percentage * 100,
53+
formatFee( percentage ),
5354
formatCurrency( fixed, baseFee.currency )
5455
);
5556

5657
if ( currentFee.discount ) {
5758
descriptionString +=
5859
' ' +
5960
sprintf(
60-
/* translators: %d percentage discount to apply */
61-
__( '(%1$d%% discount)', 'woocommerce-payments' ),
62-
currentFee.discount * 100
61+
/* translators: %f percentage discount to apply */
62+
__( '(%f%% discount)', 'woocommerce-payments' ),
63+
formatFee( currentFee.discount )
6364
);
6465
}
6566

client/utils/fees/index.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { formatFee } from 'utils/fees';
5+
6+
describe( 'Fees utilities', () => {
7+
it.each( [
8+
[ 0.03, 3 ],
9+
[ 0.0175, 1.75 ],
10+
[ 0.005, 0.5 ],
11+
[ 0.0005, 0.05 ],
12+
[ 0.00011, 0.011 ],
13+
[ 0.00019, 0.019 ],
14+
[ 0.000011, 0.001 ],
15+
[ 0.000019, 0.002 ],
16+
] )(
17+
'returns an input fee (%f) as a formatted fee (%f)',
18+
( inputFee, expectedFee ) => {
19+
expect( formatFee( inputFee ) ).toBe( expectedFee );
20+
}
21+
);
22+
} );

client/utils/fees/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Returns the given decimal fee as a percentage with
3+
* a maximum of 3 decimal places.
4+
*
5+
* @param {number} fee The fee represented as a decimal.
6+
* @return {number} The fee represented as a percentage.
7+
*/
8+
export const formatFee = ( fee: number ): number => {
9+
return Number( ( fee * 100 ).toFixed( 3 ) );
10+
};

lint-staged.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
'*.{js,jsx,ts,tsx}': [ 'npm run format:provided', 'eslint' ],
3+
'*.{ts,tsx}': [ () => 'tsc --noEmit' ],
4+
'*.{scss,css}': [ 'npm run format:provided', 'npm run lint:css' ],
5+
'*.php':
6+
'./vendor/bin/phpcs --standard=phpcs.xml.dist --basepath=. --colors',
7+
'composer.json': 'composer validate --strict --no-check-all',
8+
};

package.json

-15
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,6 @@
5656
"tube:start": "./docker/bin/jt/tunnel.sh",
5757
"tube:stop": "./docker/bin/jt/tunnel.sh break"
5858
},
59-
"lint-staged": {
60-
"*.{js,jsx,ts,tsx}": [
61-
"npm run format:provided",
62-
"eslint"
63-
],
64-
"*.{ts,tsx}": [
65-
"tsc --noEmit --jsx react"
66-
],
67-
"*.{scss,css}": [
68-
"npm run format:provided",
69-
"npm run lint:css"
70-
],
71-
"*.php": "./vendor/bin/phpcs --standard=phpcs.xml.dist --basepath=. --colors",
72-
"composer.json": "composer validate --strict --no-check-all"
73-
},
7459
"dependencies": {
7560
"@stripe/react-stripe-js": "1.1.2",
7661
"@stripe/stripe-js": "1.12.1",

readme.txt

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Please note that our support for the checkout block is still experimental and th
106106
* Fix - Fix fatal error if store currency is changed after enabled (multi) currencies set.
107107
* Fix - Use of deprecated call-style to registerPaymentMethods. WooCommerce Payments now requires WooCommerce Blocks of at least version 3.9.0.
108108
* Fix - Deposit date on Transactions list page.
109+
* Fix - Rounding error when displaying fee percentages on the Overview and Transactions pages.
109110
* Add - Error message when total size of dispute evidence files uploaded goes over limit.
110111
* Update - Pass currency to wc_price when adding intent notes to orders.
111112
* Update - Instant deposit inbox note wording.

0 commit comments

Comments
 (0)