Skip to content

Commit 9998d99

Browse files
authored
Fix wrong price formatting in Apple/Google Pay (#4480)
* make 'three_decimal_currencies' method public * use currency decimal supported in Stripe * add changelog * remove unnecessary else * convert currency to lower case * add test for test_get_stripe_currency_decimals
1 parent fc01211 commit 9998d99

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Fix - No such customer error when creating a payment method with a new Stripe account
1919
* Fix - Validate create customer payload against required billing fields before sending to Stripe
2020
* Update - Enhanced logging system with support for all log levels and improved context handling
21+
* Fix - Fixes wrong price formatting in express checkout
2122
* Fix - Require email address only for Stripe customer validation when request is from the Add Payment Method page
2223
* Fix - Set default values for custom field options
2324
* Fix - Enforce rate limiter for failed add payment method attempts

includes/class-wc-stripe-helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public static function no_decimal_currencies() {
306306
*
307307
* @return array $currencies
308308
*/
309-
private static function three_decimal_currencies() {
309+
public static function three_decimal_currencies() {
310310
return [
311311
'bhd', // Bahraini Dinar
312312
'jod', // Jordanian Dinar

includes/payment-methods/class-wc-stripe-express-checkout-helper.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,22 @@ public function get_allowed_shipping_countries() {
314314
return array_keys( $allowed_shipping_countries );
315315
}
316316

317+
/**
318+
* Get the number of decimals supported by Stripe for the currency.
319+
*
320+
* @return int
321+
*/
322+
public static function get_stripe_currency_decimals() {
323+
$currency = strtolower( get_woocommerce_currency() );
324+
if ( in_array( $currency, WC_Stripe_Helper::no_decimal_currencies(), true ) ) {
325+
return 0;
326+
} elseif ( in_array( $currency, WC_Stripe_Helper::three_decimal_currencies(), true ) ) {
327+
return 3;
328+
}
329+
330+
return 2;
331+
}
332+
317333
/**
318334
* JS params data used by cart and checkout pages.
319335
*
@@ -323,7 +339,7 @@ public function get_checkout_data() {
323339
$data = [
324340
'url' => wc_get_checkout_url(),
325341
'currency_code' => strtolower( get_woocommerce_currency() ),
326-
'currency_decimals' => wc_get_price_decimals(),
342+
'currency_decimals' => $this->get_stripe_currency_decimals(),
327343
'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
328344
'needs_shipping' => 'no',
329345
'needs_payer_phone' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ),

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
128128
* Fix - No such customer error when creating a payment method with a new Stripe account
129129
* Fix - Validate create customer payload against required billing fields before sending to Stripe
130130
* Update - Enhanced logging system with support for all log levels and improved context handling
131+
* Fix - Fixes wrong price formatting in express checkout
131132
* Fix - Require email address only for Stripe customer validation when request is from the Add Payment Method page
132133
* Fix - Set default values for custom field options
133134
* Fix - Enforce rate limiter for failed add payment method attempts

tests/phpunit/PaymentMethods/WC_Stripe_Express_Checkout_Helper_Test.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,48 @@ public function provide_test_is_request_to_store_api() {
637637
],
638638
];
639639
}
640+
641+
/**
642+
* Test for `get_stripe_currency_decimals`.
643+
*
644+
* @param string $currency Currency code.
645+
* @param int $expected Expected number of decimals.
646+
*
647+
* @dataProvider provide_test_get_stripe_currency_decimals
648+
*/
649+
public function test_get_stripe_currency_decimals( $currency, $expected ) {
650+
update_option( 'woocommerce_currency', $currency );
651+
652+
$actual = WC_Stripe_Express_Checkout_Helper::get_stripe_currency_decimals();
653+
$this->assertEquals( $expected, $actual );
654+
}
655+
656+
/**
657+
* Provider for `test_get_stripe_currency_decimals`.
658+
*
659+
* @return array
660+
*/
661+
public function provide_test_get_stripe_currency_decimals() {
662+
return [
663+
// No decimal currencies - should return 0
664+
'Japanese Yen (no decimals)' => [
665+
'currency' => 'JPY',
666+
'expected' => 0,
667+
],
668+
// Three decimal currencies - should return 3
669+
'Bahraini Dinar (three decimals)' => [
670+
'currency' => 'BHD',
671+
'expected' => 3,
672+
],
673+
// Default currencies - should return 2
674+
'US Dollar (default)' => [
675+
'currency' => 'USD',
676+
'expected' => 2,
677+
],
678+
'Euro (default)' => [
679+
'currency' => 'EUR',
680+
'expected' => 2,
681+
],
682+
];
683+
}
640684
}

0 commit comments

Comments
 (0)