From 889ae36d6dd44e9db31ef4bfe2cde2de4b0835c5 Mon Sep 17 00:00:00 2001 From: deepakpathania <68396823+deepakpathania@users.noreply.github.com> Date: Tue, 24 Dec 2024 17:41:13 +0530 Subject: [PATCH] Consistently format the order notes for refunds and charges (#10036) --- changelog/feature-consistent-note-formatting | 4 +++ includes/class-wc-payments-order-service.php | 12 +++++---- includes/multi-currency/MultiCurrency.php | 27 +++++++++++++++++++ ...c-payment-gateway-wcpay-process-refund.php | 12 ++++----- ...wc-payments-webhook-processing-service.php | 24 +++++++++++++++++ 5 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 changelog/feature-consistent-note-formatting diff --git a/changelog/feature-consistent-note-formatting b/changelog/feature-consistent-note-formatting new file mode 100644 index 00000000000..bb18d73c7ee --- /dev/null +++ b/changelog/feature-consistent-note-formatting @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Ensure consistent formatting of refund notes with MC. diff --git a/includes/class-wc-payments-order-service.php b/includes/class-wc-payments-order-service.php index f685b50debf..eb74cde866f 100644 --- a/includes/class-wc-payments-order-service.php +++ b/includes/class-wc-payments-order-service.php @@ -1752,10 +1752,8 @@ private function generate_dispute_closed_note( $charge_id, $status, $is_inquiry * @return string HTML note. */ private function generate_payment_refunded_note( float $refunded_amount, string $refunded_currency, string $wcpay_refund_id, string $refund_reason, WC_Order $order ): string { - $formatted_price = WC_Payments_Explicit_Price_Formatter::get_explicit_price( - wc_price( $refunded_amount, [ 'currency' => strtoupper( $refunded_currency ) ] ), - $order - ); + $multi_currency_instance = WC_Payments_Multi_Currency(); + $formatted_price = WC_Payments_Explicit_Price_Formatter::get_explicit_price( $multi_currency_instance->get_backend_formatted_wc_price( $refunded_amount, [ 'currency' => strtoupper( $refunded_currency ) ] ), $order ); if ( empty( $refund_reason ) ) { $note = sprintf( @@ -1930,7 +1928,11 @@ private function complete_order_processing( $order, $intent_status = null ) { * @return string The formatted order total. */ private function get_order_amount( $order ) { - return WC_Payments_Explicit_Price_Formatter::get_explicit_price( wc_price( $order->get_total(), [ 'currency' => $order->get_currency() ] ), $order ); + $multi_currency_instance = WC_Payments_Multi_Currency(); + $order_price = $order->get_total(); + + $formatted_price = $multi_currency_instance->get_backend_formatted_wc_price( $order_price, [ 'currency' => $order->get_currency() ] ); + return WC_Payments_Explicit_Price_Formatter::get_explicit_price( $formatted_price, $order ); } /** diff --git a/includes/multi-currency/MultiCurrency.php b/includes/multi-currency/MultiCurrency.php index 9ab1ac0f19a..c177d253978 100644 --- a/includes/multi-currency/MultiCurrency.php +++ b/includes/multi-currency/MultiCurrency.php @@ -17,6 +17,7 @@ use WCPay\MultiCurrency\Logger; use WCPay\MultiCurrency\Notes\NoteMultiCurrencyAvailable; use WCPay\MultiCurrency\Utils; +use WC_Payments_Features; defined( 'ABSPATH' ) || exit; @@ -1326,6 +1327,32 @@ public function is_initialized(): bool { return static::$is_initialized; } + /** + * Returns the amount with the backend format. + * + * @param float $amount The amount to format. + * @param array $args The arguments to pass to wc_price. + * + * @return string The formatted amount. + */ + public function get_backend_formatted_wc_price( float $amount, array $args = [] ): string { + // Return early if MC isn't enabled or merchant has a single currency. + if ( ! self::has_additional_currencies_enabled() || ! WC_Payments_Features::is_customer_multi_currency_enabled() ) { + return wc_price( $amount, $args ); + } + + $has_filter = has_filter( 'wc_price_args', [ $this->backend_currencies, 'build_wc_price_args' ] ); + if ( false !== $has_filter ) { + return wc_price( $amount, $args ); + } + + add_filter( 'wc_price_args', [ $this->backend_currencies, 'build_wc_price_args' ], 50 ); + $price = wc_price( $amount, $args ); + remove_filter( 'wc_price_args', [ $this->backend_currencies, 'build_wc_price_args' ], 50 ); + + return $price; + } + /** * Gets the price after adjusting it with the rounding and charm settings. * diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-process-refund.php b/tests/unit/test-class-wc-payment-gateway-wcpay-process-refund.php index b75d1721c16..f3a0aefa442 100644 --- a/tests/unit/test-class-wc-payment-gateway-wcpay-process-refund.php +++ b/tests/unit/test-class-wc-payment-gateway-wcpay-process-refund.php @@ -278,7 +278,7 @@ public function test_process_refund_save_wcpay_refund_id_to_refund_meta_and_orde $this->assertTrue( $result ); $this->assertStringContainsString( 'successfully processed', $latest_wcpay_note->content ); - $this->assertStringContainsString( wc_price( 19.99, [ 'currency' => 'USD' ] ), $latest_wcpay_note->content ); + $this->assertStringContainsString( WC_Payments_Multi_Currency()->get_backend_formatted_wc_price( 19.99, [ 'currency' => 'USD' ] ), $latest_wcpay_note->content ); $this->assertStringContainsString( 're_123456789', $latest_wcpay_note->content ); } @@ -349,7 +349,7 @@ public function test_process_refund_non_usd() { $this->assertTrue( $result ); $this->assertStringContainsString( 'successfully processed', $latest_wcpay_note->content ); - $this->assertStringContainsString( wc_price( 19.99, [ 'currency' => strtoupper( $currency ) ] ), $latest_wcpay_note->content ); + $this->assertStringContainsString( WC_Payments_Multi_Currency()->get_backend_formatted_wc_price( 19.99, [ 'currency' => strtoupper( $currency ) ] ), $latest_wcpay_note->content ); } public function test_process_refund_with_reason_non_usd() { @@ -419,7 +419,7 @@ public function test_process_refund_with_reason_non_usd() { $this->assertStringContainsString( 'successfully processed', $latest_wcpay_note->content ); $this->assertStringContainsString( 'some reason', $latest_wcpay_note->content ); - $this->assertStringContainsString( wc_price( 19.99, [ 'currency' => strtoupper( $currency ) ] ), $latest_wcpay_note->content ); + $this->assertStringContainsString( WC_Payments_Multi_Currency()->get_backend_formatted_wc_price( 19.99, [ 'currency' => strtoupper( $currency ) ] ), $latest_wcpay_note->content ); $this->assertTrue( $result ); } @@ -511,7 +511,7 @@ public function test_process_refund_interac_present() { $this->assertTrue( $result ); $this->assertStringContainsString( 'successfully processed', $latest_wcpay_note->content ); - $this->assertStringContainsString( wc_price( $amount, [ 'currency' => $currency ] ), $latest_wcpay_note->content ); + $this->assertStringContainsString( WC_Payments_Multi_Currency()->get_backend_formatted_wc_price( $amount, [ 'currency' => $currency ] ), $latest_wcpay_note->content ); } public function test_process_refund_interac_present_without_payment_method_id_meta() { @@ -591,7 +591,7 @@ public function test_process_refund_interac_present_without_payment_method_id_me $this->assertTrue( $result ); $this->assertStringContainsString( 'successfully processed', $latest_wcpay_note->content ); - $this->assertStringContainsString( wc_price( $amount, [ 'currency' => 'USD' ] ), $latest_wcpay_note->content ); + $this->assertStringContainsString( WC_Payments_Multi_Currency()->get_backend_formatted_wc_price( $amount, [ 'currency' => 'USD' ] ), $latest_wcpay_note->content ); } public function test_process_refund_interac_present_without_app_refund() { @@ -809,7 +809,7 @@ public function test_process_refund_card_present() { $this->assertTrue( $result ); $this->assertStringContainsString( 'successfully processed', $latest_wcpay_note->content ); - $this->assertStringContainsString( wc_price( $amount, [ 'currency' => strtoupper( $currency ) ] ), $latest_wcpay_note->content ); + $this->assertStringContainsString( WC_Payments_Multi_Currency()->get_backend_formatted_wc_price( $amount, [ 'currency' => strtoupper( $currency ) ] ), $latest_wcpay_note->content ); } public function test_process_refund_on_uncaptured_payment() { diff --git a/tests/unit/test-class-wc-payments-webhook-processing-service.php b/tests/unit/test-class-wc-payments-webhook-processing-service.php index 2acb5c318ad..e628c533848 100644 --- a/tests/unit/test-class-wc-payments-webhook-processing-service.php +++ b/tests/unit/test-class-wc-payments-webhook-processing-service.php @@ -652,6 +652,10 @@ public function test_payment_intent_successful_and_completes_order() { ->expects( $this->exactly( 2 ) ) ->method( 'save' ); + $this->mock_order + ->method( 'get_total' ) + ->willReturn( 15.00 ); + $this->mock_order ->expects( $this->exactly( 2 ) ) ->method( 'has_status' ) @@ -747,6 +751,10 @@ public function test_payment_intent_successful_and_completes_order_without_inten ->expects( $this->exactly( 2 ) ) ->method( 'save' ); + $this->mock_order + ->method( 'get_total' ) + ->willReturn( 15.00 ); + $this->mock_order ->expects( $this->exactly( 2 ) ) ->method( 'has_status' ) @@ -939,6 +947,10 @@ public function test_payment_intent_successful_and_send_card_reader_receipt() { ) ->willReturn( false ); + $this->mock_order + ->method( 'get_total' ) + ->willReturn( 15.00 ); + $this->mock_order ->expects( $this->once() ) ->method( 'payment_complete' ); @@ -1040,6 +1052,10 @@ public function test_payment_intent_successful_and_save_mandate() { [ '_intention_status', $intent_status ] ); + $this->mock_order + ->method( 'get_total' ) + ->willReturn( 15.00 ); + $this->mock_order ->expects( $this->exactly( 2 ) ) ->method( 'save' ); @@ -1123,6 +1139,10 @@ public function test_payment_intent_fails_and_fails_order() { ->with( '_payment_method_id' ) ->willReturn( 'pm_123123123123123' ); + $this->mock_order + ->method( 'get_total' ) + ->willReturn( 15.00 ); + $this->mock_order ->expects( $this->exactly( 3 ) ) ->method( 'has_status' ) @@ -1190,6 +1210,10 @@ public function test_payment_intent_without_charges_fails_and_fails_order() { ->with( '_payment_method_id' ) ->willReturn( 'pm_123123123123123' ); + $this->mock_order + ->method( 'get_total' ) + ->willReturn( 15.00 ); + $this->mock_order ->expects( $this->exactly( 3 ) ) ->method( 'has_status' )