From d1996ca2fe3cddc9c676cb118e10dc483af463c9 Mon Sep 17 00:00:00 2001 From: Zvonimir Maglica Date: Wed, 30 Aug 2023 16:56:50 +0200 Subject: [PATCH] Code review fixes - Added support for non wcpay subscriptions --- includes/class-wc-payment-gateway-wcpay.php | 21 +++++++++++-------- .../test-class-upe-payment-gateway.php | 6 +++--- .../test-class-upe-split-payment-gateway.php | 8 +++---- ...ay-wcpay-subscriptions-process-payment.php | 12 +++++------ .../test-class-wc-payment-gateway-wcpay.php | 2 +- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/includes/class-wc-payment-gateway-wcpay.php b/includes/class-wc-payment-gateway-wcpay.php index ff9f6982b6d..59b8c0f25ce 100644 --- a/includes/class-wc-payment-gateway-wcpay.php +++ b/includes/class-wc-payment-gateway-wcpay.php @@ -1509,15 +1509,18 @@ protected function get_metadata_from_order( $order, $payment_type ) { 'subscription_payment' => 'no', ]; - // If the order belongs to a WCPay Subscription, set the payment context to 'wcpay_subscription' (this helps with associating which fees belong to orders). - if ( 'recurring' === (string) $payment_type && ! $this->is_subscriptions_plugin_active() ) { - $subscriptions = wcs_get_subscriptions_for_order( $order, [ 'order_type' => 'any' ] ); - - foreach ( $subscriptions as $subscription ) { - if ( WC_Payments_Subscription_Service::is_wcpay_subscription( $subscription ) ) { - $metadata['payment_context'] = 'wcpay_subscription'; - $metadata['subscription_payment'] = count( $subscriptions ) > 1 ? 'renewal' : 'initial'; - break; + if ( 'recurring' === (string) $payment_type ) { + $subscriptions = wcs_get_subscriptions_for_order( $order, [ 'order_type' => 'any' ] ); + $metadata['subscription_payment'] = count( $subscriptions ) > 1 ? 'renewal' : 'initial'; + $metadata['payment_context'] = 'regular_subscription'; + + // If the order belongs to a WCPay Subscription, set the payment context to 'wcpay_subscription' (this helps with associating which fees belong to orders). + if ( $this->is_subscriptions_plugin_active() ) { + foreach ( $subscriptions as $subscription ) { + if ( WC_Payments_Subscription_Service::is_wcpay_subscription( $subscription ) ) { + $metadata['payment_context'] = 'wcpay_subscription'; + break; + } } } } diff --git a/tests/unit/payment-methods/test-class-upe-payment-gateway.php b/tests/unit/payment-methods/test-class-upe-payment-gateway.php index 66571902a2a..7f78813486a 100644 --- a/tests/unit/payment-methods/test-class-upe-payment-gateway.php +++ b/tests/unit/payment-methods/test-class-upe-payment-gateway.php @@ -382,7 +382,7 @@ public function test_update_payment_intent_adds_customer_save_payment_and_level3 'order_key' => $order->get_order_key(), 'payment_type' => Payment_Type::SINGLE(), 'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used. - 'checkout_type' => 'unknown', + 'checkout_type' => '', 'client_version' => WCPAY_VERSION_NUMBER, 'subscription_payment' => 'no', ] @@ -484,7 +484,7 @@ public function test_update_payment_intent_with_selected_upe_payment_method() { 'order_key' => $order->get_order_key(), 'payment_type' => Payment_Type::SINGLE(), 'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used. - 'checkout_type' => 'unknown', + 'checkout_type' => '', 'client_version' => WCPAY_VERSION_NUMBER, 'subscription_payment' => 'no', ] @@ -582,7 +582,7 @@ public function test_update_payment_intent_with_payment_country() { 'order_key' => $order->get_order_key(), 'payment_type' => Payment_Type::SINGLE(), 'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used. - 'checkout_type' => 'unknown', + 'checkout_type' => '', 'client_version' => WCPAY_VERSION_NUMBER, 'subscription_payment' => 'no', ] diff --git a/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php b/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php index fd5223c2b59..0d2d8af1e05 100644 --- a/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php +++ b/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php @@ -416,7 +416,7 @@ public function test_update_payment_intent_adds_customer_save_payment_and_level3 'order_key' => $order->get_order_key(), 'payment_type' => Payment_Type::SINGLE(), 'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used. - 'checkout_type' => 'unknown', + 'checkout_type' => '', 'client_version' => WCPAY_VERSION_NUMBER, 'subscription_payment' => 'no', ]; @@ -491,8 +491,8 @@ public function test_update_payment_intent_with_selected_upe_payment_method() { 'order_key' => $order->get_order_key(), 'payment_type' => Payment_Type::SINGLE(), 'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used. - 'checkout_type' => 'unknown', - 'client_version' => '6.3.2', + 'checkout_type' => '', + 'client_version' => WCPAY_VERSION_NUMBER, 'subscription_payment' => 'no', ]; @@ -570,7 +570,7 @@ public function test_update_payment_intent_with_payment_country() { 'order_key' => $order->get_order_key(), 'payment_type' => Payment_Type::SINGLE(), 'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used. - 'checkout_type' => 'unknown', + 'checkout_type' => '', 'client_version' => WCPAY_VERSION_NUMBER, 'subscription_payment' => 'no', ]; diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-process-payment.php b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-process-payment.php index bef80ba1878..74a4ad79b29 100644 --- a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-process-payment.php +++ b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-process-payment.php @@ -445,15 +445,18 @@ function( $metadata ) { } public function test_saved_card_zero_dollar_subscription() { - $order = WC_Helper_Order::create_order( self::USER_ID, 0 ); + $order = WC_Helper_Order::create_order( self::USER_ID, 0 ); + $subscriptions = [ new WC_Subscription() ]; + $subscriptions[0]->set_parent( $order ); + + $this->mock_wcs_order_contains_subscription( true ); + $this->mock_wcs_get_subscriptions_for_order( $subscriptions ); $_POST = [ 'payment_method' => WC_Payment_Gateway_WCPay::GATEWAY_ID, self::TOKEN_REQUEST_KEY => $this->token->get_id(), ]; - $this->mock_wcs_order_contains_subscription( true ); - // The card is already saved and there's no payment needed, so no Setup Intent needs to be created. $request = $this->mock_wcpay_request( Create_And_Confirm_Setup_Intention::class, 0 ); @@ -463,9 +466,6 @@ public function test_saved_card_zero_dollar_subscription() { ->expects( $this->never() ) ->method( 'add_payment_method_to_user' ); - $subscriptions = [ WC_Helper_Order::create_order( self::USER_ID ) ]; - $this->mock_wcs_get_subscriptions_for_order( $subscriptions ); - $result = $this->mock_wcpay_gateway->process_payment( $order->get_id() ); $result_order = wc_get_order( $order->get_id() ); diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay.php b/tests/unit/test-class-wc-payment-gateway-wcpay.php index 150e3d47798..f39f4dea3ed 100644 --- a/tests/unit/test-class-wc-payment-gateway-wcpay.php +++ b/tests/unit/test-class-wc-payment-gateway-wcpay.php @@ -1322,7 +1322,7 @@ public function test_capture_charge_metadata() { 'order_key' => $order->get_order_key(), 'payment_type' => Payment_Type::SINGLE(), 'gateway_type' => 'classic', - 'checkout_type' => 'unknown', + 'checkout_type' => '', 'client_version' => WCPAY_VERSION_NUMBER, 'subscription_payment' => 'no', ];