Skip to content

Commit bbf6794

Browse files
committed
♻️ Replace number_format with CartHelper method
1 parent 6e13bb6 commit bbf6794

5 files changed

Lines changed: 17 additions & 26 deletions

File tree

modules/ppcp-store-sync/src/CartValidation/CouponValidator/CouponContextBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ private function build_minimum_spend( string $code, PayPalCart $cart, ?WC_Coupon
193193
$currency = CartHelper::currency( $cart, get_woocommerce_currency() );
194194

195195
return array(
196-
'minimum_required' => number_format( $minimum, 2, '.', '' ),
197-
'current_subtotal' => number_format( $subtotal, 2, '.', '' ),
198-
'shortage_amount' => number_format( $shortage, 2, '.', '' ),
196+
'minimum_required' => CartHelper::format_decimal( $minimum ),
197+
'current_subtotal' => CartHelper::format_decimal( $subtotal ),
198+
'shortage_amount' => CartHelper::format_decimal( $shortage ),
199199
'currency_code' => $currency,
200200
);
201201
}
@@ -219,8 +219,8 @@ private function build_maximum_spend( string $code, PayPalCart $cart, ?WC_Coupon
219219
$currency = CartHelper::currency( $cart, get_woocommerce_currency() );
220220

221221
return array(
222-
'maximum_allowed' => number_format( $maximum, 2, '.', '' ),
223-
'current_subtotal' => number_format( $subtotal, 2, '.', '' ),
222+
'maximum_allowed' => CartHelper::format_decimal( $maximum ),
223+
'current_subtotal' => CartHelper::format_decimal( $subtotal ),
224224
'currency_code' => $currency,
225225
);
226226
}

modules/ppcp-store-sync/src/CartValidation/CouponValidator/DiscountCalculator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use WC_Discounts;
1616
use WooCommerce\PayPalCommerce\StoreSync\Helper\ProductManager;
1717
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
18+
use WooCommerce\PayPalCommerce\StoreSync\Helper\CartHelper;
1819

1920
/**
2021
* Calculates discount amounts for coupons using WooCommerce's native discount calculation.
@@ -70,8 +71,7 @@ public function calculate_discount_amount( WC_Coupon $wc_coupon, PayPalCart $car
7071
? array_sum( $totals[ $code ] )
7172
: $totals[ $code ];
7273

73-
// The value from get_discounts_by_coupon() is already in regular decimal format.
74-
return number_format( $discount_value, 2, '.', '' );
74+
return CartHelper::format_decimal( $discount_value );
7575
}
7676

7777
return '0.00';

modules/ppcp-store-sync/src/Helper/CartHelper.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ static function ( float $cart_total, CartItem $item ): float {
7979
}
8080

8181
/**
82-
* Formats a price value to two decimal places.
82+
* Formats a price value for an API response.
8383
*
84-
* @param float $value The price value to format.
84+
* PayPal expects monetary values to be strings with two decimal places.
85+
*
86+
* @param int|float|string $value The price value to format.
8587
* @return string The formatted price (e.g., "123.45").
8688
*/
87-
public static function format_decimal( float $value ): string {
88-
return number_format( $value, 2, '.', '' );
89+
public static function format_decimal( $value ): string {
90+
return number_format( (float) $value, 2, '.', '' );
8991
}
9092

9193
public static function full_customer_name( PayPalCart $cart, string $default = '' ): string {
@@ -195,7 +197,7 @@ public static function calculate_totals( WC_Cart $wc_cart, string $currency_code
195197
public static function money( string $currency_code, float $value ): array {
196198
return array(
197199
'currency_code' => $currency_code,
198-
'value' => number_format( $value, 2 ),
200+
'value' => self::format_decimal( $value ),
199201
);
200202
}
201203
}

modules/ppcp-store-sync/src/Helper/PayPalOrderManager.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private function build_items_for_patch( PayPalCart $cart ): array {
257257
'quantity' => (string) $item->quantity(),
258258
'unit_amount' => array(
259259
'currency_code' => $currency,
260-
'value' => $this->format_money( (float) $price->value() ),
260+
'value' => CartHelper::format_decimal( $price->value() ),
261261
),
262262
);
263263
}
@@ -406,16 +406,4 @@ public function capture_order( string $order_id ): ?array {
406406
return null;
407407
}
408408
}
409-
410-
/**
411-
* Format a money value for PayPal API.
412-
*
413-
* PayPal requires money values as strings with 2 decimal places.
414-
*
415-
* @param float $value The money value.
416-
* @return string Formatted money value.
417-
*/
418-
private function format_money( float $value ): string {
419-
return number_format( $value, 2, '.', '' );
420-
}
421409
}

modules/ppcp-store-sync/src/Ingestion/ProductsPayload.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use WC_Product;
66
use WC_Product_Variation;
7+
use WooCommerce\PayPalCommerce\StoreSync\Helper\CartHelper;
78

89
class ProductsPayload {
910
private string $merchant_store_url;
@@ -293,6 +294,6 @@ private function format_price( $price ): string {
293294
return '';
294295
}
295296

296-
return number_format( (float) $price, 2, '.', '' ) . ' ' . get_woocommerce_currency();
297+
return CartHelper::format_decimal( $price ) . ' ' . get_woocommerce_currency();
297298
}
298299
}

0 commit comments

Comments
 (0)