Skip to content

Commit

Permalink
Merge branch 'develop' into test/simplify-csv-merged
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagesh Pai committed Dec 25, 2024
2 parents 9c8187a + 889ae36 commit c182a62
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
4 changes: 4 additions & 0 deletions changelog/feature-consistent-note-formatting
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Ensure consistent formatting of refund notes with MC.
12 changes: 7 additions & 5 deletions includes/class-wc-payments-order-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
27 changes: 27 additions & 0 deletions includes/multi-currency/MultiCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use WCPay\MultiCurrency\Logger;
use WCPay\MultiCurrency\Notes\NoteMultiCurrencyAvailable;
use WCPay\MultiCurrency\Utils;
use WC_Payments_Features;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test-class-wc-payments-webhook-processing-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
Expand Down Expand Up @@ -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' )
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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' )
Expand Down Expand Up @@ -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' )
Expand Down

0 comments on commit c182a62

Please sign in to comment.