From cbcb80680c52c768c6c326a94e2f2f44147ab12a Mon Sep 17 00:00:00 2001 From: Alefe Souza Date: Thu, 9 Nov 2023 00:32:07 -0300 Subject: [PATCH] Add Affiliate for WooCommerce support on WooPay (#7291) --- ...x-woopay-affiliate-for-woocommerce-support | 4 ++ .../class-woopay-adapted-extensions.php | 48 ++++++++++++++++++- includes/woopay/class-woopay-session.php | 22 +++++---- .../test-class-woopay-adapted-extensions.php | 1 + .../unit/woopay/test-class-woopay-session.php | 12 ++--- 5 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 changelog/fix-woopay-affiliate-for-woocommerce-support diff --git a/changelog/fix-woopay-affiliate-for-woocommerce-support b/changelog/fix-woopay-affiliate-for-woocommerce-support new file mode 100644 index 00000000000..5a03c9c4dfe --- /dev/null +++ b/changelog/fix-woopay-affiliate-for-woocommerce-support @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Add Affiliate for WooCommerce support on WooPay. diff --git a/includes/woopay/class-woopay-adapted-extensions.php b/includes/woopay/class-woopay-adapted-extensions.php index 340af75bfab..e5a602dca9f 100644 --- a/includes/woopay/class-woopay-adapted-extensions.php +++ b/includes/woopay/class-woopay-adapted-extensions.php @@ -22,7 +22,7 @@ class WooPay_Adapted_Extensions extends IntegrationRegistry { /** * Initializa WC Blocks regitered integrations. */ - public function __construct() { + public function init() { do_action( 'woocommerce_blocks_checkout_block_registration', $this ); } @@ -171,9 +171,41 @@ public function get_extension_data() { ]; } + if ( $this->is_affiliate_for_woocommerce_enabled() ) { + /** + * @psalm-suppress UndefinedFunction + */ + $extension_data[ 'affiliate-for-woocommerce' ] = [ + 'affiliate-user' => afwc_get_referrer_id(), + ]; + } + return $extension_data; } + /** + * Update order extension data after finishing + * an order on WooPay, this usually is needed + * for extensions which uses cookies when an + * order is finished. + * + * @param int $order_id The successful WooPay order. + */ + public function update_order_extension_data( $order_id ) { + if ( ! empty( $_GET['affiliate'] ) && // phpcs:ignore WordPress.Security.NonceVerification + $this->is_affiliate_for_woocommerce_enabled() + ) { + $affiliate_id = (int) wc_clean( wp_unslash( $_GET['affiliate'] ) ); // phpcs:ignore WordPress.Security.NonceVerification + + // phpcs:ignore + /** + * @psalm-suppress UndefinedClass + */ + $affiliate_api = \AFWC_API::get_instance(); + $affiliate_api->track_conversion( $order_id, $affiliate_id, '', [ 'is_affiliate_eligible' => true ] ); + } + } + /** * Get WC Blocks registered integrations. * @@ -190,4 +222,18 @@ public function register( IntegrationInterface $integration ) { return true; } + + /** + * Check if Affiliate for WooCommerce is enabled and + * its functions used on WCPay are available. + * + * @return boolean + */ + public function is_affiliate_for_woocommerce_enabled() { + return defined( 'AFWC_PLUGIN_FILE' ) && + function_exists( 'afwc_get_referrer_id' ) && + class_exists( 'AFWC_API' ) && + method_exists( 'AFWC_API', 'get_instance' ) && + method_exists( 'AFWC_API', 'track_conversion' ); + } } diff --git a/includes/woopay/class-woopay-session.php b/includes/woopay/class-woopay-session.php index 6781f95d943..29952ce5f9f 100644 --- a/includes/woopay/class-woopay-session.php +++ b/includes/woopay/class-woopay-session.php @@ -57,7 +57,7 @@ class WooPay_Session { public static function init() { add_filter( 'determine_current_user', [ __CLASS__, 'determine_current_user_for_woopay' ], 20 ); add_filter( 'woocommerce_session_handler', [ __CLASS__, 'add_woopay_store_api_session_handler' ], 20 ); - add_action( 'woocommerce_order_payment_status_changed', [ __CLASS__, 'remove_order_customer_id_on_requests_with_verified_email' ] ); + add_action( 'woocommerce_order_payment_status_changed', [ __CLASS__, 'woopay_order_payment_status_changed' ] ); add_action( 'woopay_restore_order_customer_id', [ __CLASS__, 'restore_order_customer_id_from_requests_with_verified_email' ] ); register_deactivation_hook( WCPAY_PLUGIN_FILE, [ __CLASS__, 'run_and_remove_woopay_restore_order_customer_id_schedules' ] ); @@ -159,25 +159,29 @@ public static function get_user_id_from_cart_token() { } /** - * Prevent set order customer ID on requests with + * Update order data for extensions which uses cookies, + * also prevent set order customer ID on requests with * email verified to skip the login screen on the TYP. * After 10 minutes, the customer ID will be restored * and the user will need to login to access the TYP. * - * @param \WC_Order $order_id The order ID being updated. + * @param int $order_id The order ID being updated. */ - public static function remove_order_customer_id_on_requests_with_verified_email( $order_id ) { - $woopay_verified_email_address = self::get_woopay_verified_email_address(); - - if ( null === $woopay_verified_email_address ) { + public static function woopay_order_payment_status_changed( $order_id ) { + if ( ! self::is_woopay_enabled() ) { return; } - if ( ! self::is_woopay_enabled() ) { + if ( ! self::is_request_from_woopay() || ! self::is_store_api_request() ) { return; } - if ( ! self::is_request_from_woopay() || ! self::is_store_api_request() ) { + $woopay_adapted_extensions = new WooPay_Adapted_Extensions(); + $woopay_adapted_extensions->update_order_extension_data( $order_id ); + + $woopay_verified_email_address = self::get_woopay_verified_email_address(); + + if ( null === $woopay_verified_email_address ) { return; } diff --git a/tests/unit/woopay/test-class-woopay-adapted-extensions.php b/tests/unit/woopay/test-class-woopay-adapted-extensions.php index 9144ea7ed31..1e6a6792f5f 100644 --- a/tests/unit/woopay/test-class-woopay-adapted-extensions.php +++ b/tests/unit/woopay/test-class-woopay-adapted-extensions.php @@ -52,6 +52,7 @@ function( $integration_registry ) { $this->test_user = self::factory()->user->create_and_get(); $this->woopay_adapted_extensions = new WooPay_Adapted_Extensions(); + $this->woopay_adapted_extensions->init(); } public function test_get_adapted_extensions_data_without_enable_adapted_extensions() { diff --git a/tests/unit/woopay/test-class-woopay-session.php b/tests/unit/woopay/test-class-woopay-session.php index f15f32412ae..5368cbd9878 100644 --- a/tests/unit/woopay/test-class-woopay-session.php +++ b/tests/unit/woopay/test-class-woopay-session.php @@ -155,7 +155,7 @@ public function test_get_user_id_from_cart_token_with_verified_user_store_api_to $this->assertEquals( WooPay_Session::get_user_id_from_cart_token(), $verified_user->ID ); } - public function test_remove_order_customer_id_on_requests_with_verified_email_with_verified_user_store_api_token_without_adapted_extensions() { + public function test_woopay_order_payment_status_changed_with_verified_user_store_api_token_without_adapted_extensions() { $verified_user = self::factory()->user->create_and_get(); $woopay_store_api_token = WooPay_Store_Api_Token::init(); @@ -167,14 +167,14 @@ public function test_remove_order_customer_id_on_requests_with_verified_email_wi $order = \WC_Helper_Order::create_order( $verified_user->ID ); $order->set_billing_email( $verified_user->user_email ); $order->save(); - WooPay_Session::remove_order_customer_id_on_requests_with_verified_email( $order->get_Id() ); + WooPay_Session::woopay_order_payment_status_changed( $order->get_Id() ); $updated_order = wc_get_order( $order->get_id() ); $this->assertEmpty( $updated_order->get_meta( 'woopay_merchant_customer_id' ) ); $this->assertEquals( $updated_order->get_customer_id(), $verified_user->ID ); } - public function test_remove_order_customer_id_on_requests_with_verified_email_with_verified_user_store_api_token_with_non_matching_order_billing_email() { + public function test_woopay_order_payment_status_changed_with_verified_user_store_api_token_with_non_matching_order_billing_email() { $verified_user = self::factory()->user->create_and_get(); $woopay_store_api_token = WooPay_Store_Api_Token::init(); @@ -188,14 +188,14 @@ public function test_remove_order_customer_id_on_requests_with_verified_email_wi $order = \WC_Helper_Order::create_order( $verified_user->ID ); $order->set_billing_email( 'test@example.com' ); $order->save(); - WooPay_Session::remove_order_customer_id_on_requests_with_verified_email( $order->get_id() ); + WooPay_Session::woopay_order_payment_status_changed( $order->get_id() ); $updated_order = wc_get_order( $order->get_id() ); $this->assertEmpty( $updated_order->get_meta( 'woopay_merchant_customer_id' ) ); $this->assertEquals( $updated_order->get_customer_id(), $verified_user->ID ); } - public function test_remove_order_customer_id_on_requests_with_verified_email_with_verified_user_store_api_token() { + public function test_woopay_order_payment_status_changed_with_verified_user_store_api_token() { $verified_user = self::factory()->user->create_and_get(); $woopay_store_api_token = WooPay_Store_Api_Token::init(); @@ -209,7 +209,7 @@ public function test_remove_order_customer_id_on_requests_with_verified_email_wi $order = \WC_Helper_Order::create_order( $verified_user->ID ); $order->set_billing_email( $verified_user->user_email ); $order->save(); - WooPay_Session::remove_order_customer_id_on_requests_with_verified_email( $order->get_id() ); + WooPay_Session::woopay_order_payment_status_changed( $order->get_id() ); $updated_order = wc_get_order( $order->get_id() ); $this->assertEquals( $updated_order->get_meta( 'woopay_merchant_customer_id' ), $verified_user->ID );