From 8ea5c5f1794ec3751fefb7218cbe9a6f18849c1b Mon Sep 17 00:00:00 2001 From: Zvonimir Maglica Date: Thu, 31 Aug 2023 19:58:39 +0200 Subject: [PATCH] Add/5669 add further payment metadata (#7091) Co-authored-by: Radoslav Georgiev --- .../add-5669-add-further-payment-metadata | 4 ++ includes/class-wc-payment-gateway-wcpay.php | 39 +++++++------ .../test-class-upe-payment-gateway.php | 54 +++++++++++------- .../test-class-upe-split-payment-gateway.php | 55 ++++++++++++------- ...ay-wcpay-subscriptions-process-payment.php | 12 ++-- .../test-class-wc-payment-gateway-wcpay.php | 18 +++--- 6 files changed, 109 insertions(+), 73 deletions(-) create mode 100644 changelog/add-5669-add-further-payment-metadata diff --git a/changelog/add-5669-add-further-payment-metadata b/changelog/add-5669-add-further-payment-metadata new file mode 100644 index 00000000000..347c49daf22 --- /dev/null +++ b/changelog/add-5669-add-further-payment-metadata @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Added additional meta data to payment requests diff --git a/includes/class-wc-payment-gateway-wcpay.php b/includes/class-wc-payment-gateway-wcpay.php index 826ed51d02e..5f3912b4b1d 100644 --- a/includes/class-wc-payment-gateway-wcpay.php +++ b/includes/class-wc-payment-gateway-wcpay.php @@ -1484,30 +1484,33 @@ public function set_payment_method_title_for_order( $order, $payment_method_type * @return array Array of keyed metadata values. */ protected function get_metadata_from_order( $order, $payment_type ) { + if ( $this instanceof UPE_Split_Payment_Gateway ) { + $gateway_type = 'split_upe'; + } elseif ( $this instanceof UPE_Payment_Gateway ) { + $gateway_type = 'upe'; + } else { + $gateway_type = 'classic'; + } $name = sanitize_text_field( $order->get_billing_first_name() ) . ' ' . sanitize_text_field( $order->get_billing_last_name() ); $email = sanitize_email( $order->get_billing_email() ); $metadata = [ - 'customer_name' => $name, - 'customer_email' => $email, - 'site_url' => esc_url( get_site_url() ), - 'order_id' => $order->get_id(), - 'order_number' => $order->get_order_number(), - 'order_key' => $order->get_order_key(), - 'payment_type' => $payment_type, + 'customer_name' => $name, + 'customer_email' => $email, + 'site_url' => esc_url( get_site_url() ), + 'order_id' => $order->get_id(), + 'order_number' => $order->get_order_number(), + 'order_key' => $order->get_order_key(), + 'payment_type' => $payment_type, + 'gateway_type' => $gateway_type, + 'checkout_type' => $order->get_created_via(), + 'client_version' => WCPAY_VERSION_NUMBER, + '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'; - break; - } - } + if ( 'recurring' === (string) $payment_type && function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order ) ) { + $metadata['subscription_payment'] = wcs_order_contains_renewal( $order ) ? 'renewal' : 'initial'; + $metadata['payment_context'] = $this->is_subscriptions_plugin_active() ? 'regular_subscription' : 'wcpay_subscription'; } - return apply_filters( 'wcpay_metadata_from_order', $metadata, $order, $payment_type ); } 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 b01a28df986..715a43ff967 100644 --- a/tests/unit/payment-methods/test-class-upe-payment-gateway.php +++ b/tests/unit/payment-methods/test-class-upe-payment-gateway.php @@ -374,13 +374,17 @@ public function test_update_payment_intent_adds_customer_save_payment_and_level3 ->method( 'set_metadata' ) ->with( [ - 'customer_name' => 'Jeroen Sormani', - 'customer_email' => 'admin@example.org', - 'site_url' => 'http://example.org', - 'order_id' => $order_id, - 'order_number' => $order_number, - 'order_key' => $order->get_order_key(), - 'payment_type' => Payment_Type::SINGLE(), + 'customer_name' => 'Jeroen Sormani', + 'customer_email' => 'admin@example.org', + 'site_url' => 'http://example.org', + 'order_id' => $order_id, + 'order_number' => $order_number, + 'order_key' => $order->get_order_key(), + 'payment_type' => Payment_Type::SINGLE(), + 'gateway_type' => 'upe', + 'checkout_type' => '', + 'client_version' => WCPAY_VERSION_NUMBER, + 'subscription_payment' => 'no', ] ); @@ -472,13 +476,17 @@ public function test_update_payment_intent_with_selected_upe_payment_method() { ->method( 'set_metadata' ) ->with( [ - 'customer_name' => 'Jeroen Sormani', - 'customer_email' => 'admin@example.org', - 'site_url' => 'http://example.org', - 'order_id' => $order_id, - 'order_number' => $order_number, - 'order_key' => $order->get_order_key(), - 'payment_type' => Payment_Type::SINGLE(), + 'customer_name' => 'Jeroen Sormani', + 'customer_email' => 'admin@example.org', + 'site_url' => 'http://example.org', + 'order_id' => $order_id, + 'order_number' => $order_number, + 'order_key' => $order->get_order_key(), + 'payment_type' => Payment_Type::SINGLE(), + 'gateway_type' => 'upe', + 'checkout_type' => '', + 'client_version' => WCPAY_VERSION_NUMBER, + 'subscription_payment' => 'no', ] ); @@ -566,13 +574,17 @@ public function test_update_payment_intent_with_payment_country() { ->method( 'set_metadata' ) ->with( [ - 'customer_name' => 'Jeroen Sormani', - 'customer_email' => 'admin@example.org', - 'site_url' => 'http://example.org', - 'order_id' => $order_id, - 'order_number' => $order_number, - 'order_key' => $order->get_order_key(), - 'payment_type' => Payment_Type::SINGLE(), + 'customer_name' => 'Jeroen Sormani', + 'customer_email' => 'admin@example.org', + 'site_url' => 'http://example.org', + 'order_id' => $order_id, + 'order_number' => $order_number, + 'order_key' => $order->get_order_key(), + 'payment_type' => Payment_Type::SINGLE(), + 'gateway_type' => 'upe', + '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 4b829032e9e..4a613570de1 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 @@ -408,13 +408,17 @@ public function test_update_payment_intent_adds_customer_save_payment_and_level3 ->method( 'create_customer_for_user' ); $metadata = [ - 'customer_name' => 'Jeroen Sormani', - 'customer_email' => 'admin@example.org', - 'site_url' => 'http://example.org', - 'order_id' => $order_id, - 'order_number' => $order_number, - 'order_key' => $order->get_order_key(), - 'payment_type' => Payment_Type::SINGLE(), + 'customer_name' => 'Jeroen Sormani', + 'customer_email' => 'admin@example.org', + 'site_url' => 'http://example.org', + 'order_id' => $order_id, + 'order_number' => $order_number, + 'order_key' => $order->get_order_key(), + 'payment_type' => Payment_Type::SINGLE(), + 'gateway_type' => 'split_upe', + 'checkout_type' => '', + 'client_version' => WCPAY_VERSION_NUMBER, + 'subscription_payment' => 'no', ]; $level3 = [ @@ -479,13 +483,18 @@ public function test_update_payment_intent_with_selected_upe_payment_method() { ->method( 'create_customer_for_user' ); $metadata = [ - 'customer_name' => 'Jeroen Sormani', - 'customer_email' => 'admin@example.org', - 'site_url' => 'http://example.org', - 'order_id' => $order_id, - 'order_number' => $order_number, - 'order_key' => $order->get_order_key(), - 'payment_type' => Payment_Type::SINGLE(), + 'customer_name' => 'Jeroen Sormani', + 'customer_email' => 'admin@example.org', + 'site_url' => 'http://example.org', + 'order_id' => $order_id, + 'order_number' => $order_number, + 'order_key' => $order->get_order_key(), + 'payment_type' => Payment_Type::SINGLE(), + 'gateway_type' => 'split_upe', + 'checkout_type' => '', + 'client_version' => WCPAY_VERSION_NUMBER, + 'subscription_payment' => 'no', + ]; $level3 = [ @@ -553,13 +562,17 @@ public function test_update_payment_intent_with_payment_country() { ->method( 'create_customer_for_user' ); $metadata = [ - 'customer_name' => 'Jeroen Sormani', - 'customer_email' => 'admin@example.org', - 'site_url' => 'http://example.org', - 'order_id' => $order_id, - 'order_number' => $order_number, - 'order_key' => $order->get_order_key(), - 'payment_type' => Payment_Type::SINGLE(), + 'customer_name' => 'Jeroen Sormani', + 'customer_email' => 'admin@example.org', + 'site_url' => 'http://example.org', + 'order_id' => $order_id, + 'order_number' => $order_number, + 'order_key' => $order->get_order_key(), + 'payment_type' => Payment_Type::SINGLE(), + 'gateway_type' => 'split_upe', + 'checkout_type' => '', + 'client_version' => WCPAY_VERSION_NUMBER, + 'subscription_payment' => 'no', ]; $level3 = [ 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 0f098b61ba9..f39f4dea3ed 100644 --- a/tests/unit/test-class-wc-payment-gateway-wcpay.php +++ b/tests/unit/test-class-wc-payment-gateway-wcpay.php @@ -1314,13 +1314,17 @@ public function test_capture_charge_metadata() { ); $merged_metadata = [ - 'customer_name' => 'Test', - 'customer_email' => $order->get_billing_email(), - 'site_url' => esc_url( get_site_url() ), - 'order_id' => $order->get_id(), - 'order_number' => $order->get_order_number(), - 'order_key' => $order->get_order_key(), - 'payment_type' => Payment_Type::SINGLE(), + 'customer_name' => 'Test', + 'customer_email' => $order->get_billing_email(), + 'site_url' => esc_url( get_site_url() ), + 'order_id' => $order->get_id(), + 'order_number' => $order->get_order_number(), + 'order_key' => $order->get_order_key(), + 'payment_type' => Payment_Type::SINGLE(), + 'gateway_type' => 'classic', + 'checkout_type' => '', + 'client_version' => WCPAY_VERSION_NUMBER, + 'subscription_payment' => 'no', ]; $request = $this->mock_wcpay_request( Get_Intention::class, 1, $intent_id );