diff --git a/composer.json b/composer.json index 8554282..b185e4d 100644 --- a/composer.json +++ b/composer.json @@ -124,6 +124,7 @@ "phpunit/phpcov": "^8.2.1", "wpackagist-plugin/jetpack": "^14.0", "wpackagist-plugin/woocommerce": ">=9", + "wpackagist-plugin/woocommerce-payments": "^8.7", "wpackagist-theme/twentytwentyfive": "*" }, "extra": { diff --git a/composer.lock b/composer.lock index 488b1af..bcb4e02 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "88883a57a82ab53060440eee6ebf8b5a", + "content-hash": "75f09d45d123c97ce8bf91bf5fcca454", "packages": [ { "name": "doctrine/inflector", @@ -6770,6 +6770,24 @@ "type": "wordpress-plugin", "homepage": "https://wordpress.org/plugins/woocommerce/" }, + { + "name": "wpackagist-plugin/woocommerce-payments", + "version": "8.7.0", + "source": { + "type": "svn", + "url": "https://plugins.svn.wordpress.org/woocommerce-payments/", + "reference": "tags/8.7.0" + }, + "dist": { + "type": "zip", + "url": "https://downloads.wordpress.org/plugin/woocommerce-payments.8.7.0.zip" + }, + "require": { + "composer/installers": "^1.0 || ^2.0" + }, + "type": "wordpress-plugin", + "homepage": "https://wordpress.org/plugins/woocommerce-payments/" + }, { "name": "wpackagist-theme/twentytwentyfive", "version": "1.0", @@ -6791,13 +6809,13 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { "ext-json": "*" }, - "platform-dev": [], + "platform-dev": {}, "platform-overrides": { "php": "7.3" }, diff --git a/includes/Listeners/Commerce.php b/includes/Listeners/Commerce.php index 2c02fdc..75f53a7 100644 --- a/includes/Listeners/Commerce.php +++ b/includes/Listeners/Commerce.php @@ -27,6 +27,8 @@ public function register_hooks() { add_filter( 'update_option_ewc4wp_sso_account_status', array( $this, 'ecomdash_connected' ), 10, 2 ); add_filter( 'woocommerce_update_product', array( $this, 'product_created_or_updated' ), 100, 2 ); add_action( 'update_option_woocommerce_custom_orders_table_enabled', array( $this, 'woocommerce_hpos_enabled' ), 10, 3 ); + // Hook into the update of the 'wcpay_account_data' option to trigger an event when WooPay is connected. + add_filter( 'update_option_wcpay_account_data', array( $this, 'woopay_connection' ), 10, 2 ); } /** @@ -329,4 +331,38 @@ public function woocommerce_hpos_enabled( $old_value, $new_value, string $option ); } } + + /** + * This method triggers a `payment_connected` event when WooPay is connected (when `wcpay_account_data` goes from not existing to existing) + * + * * Connection Data (from `wcpay_account_data`): + * - account_id: Unique WooPay account ID. + * - status: Connection status (e.g., 'connected', 'disconnected'). + * - last_updated: timestamp, (e.g. '2025-01-08T12:34:56Z') + * - is_live + * + * @hooked update_option_wcpay_account_data + * @see \WC_Payments_Account::get_cached_account_data() + * @see \WCPay\Database_Cache::ACCOUNT_KEY + * @see \WCPay\Database_Cache::get_or_add() + * @see update_option() + * + * @param array{data:array{account_id:string,status:string,last_updated:string}} $new_option New value of the woopay connection option + * @param array|false|string $old_option Old value of the woopay connection option + */ + public function woopay_connection( $new_option, $old_option ): void { + $url = is_ssl() ? 'https://' : 'http://'; + $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + $data = array( + 'label_key' => 'provider', + 'provider' => 'woopay', + 'page' => $url, + ); + if ( empty( $old_option ) && ! empty( $new_option ) ) { + $this->push( + 'payment_connected', + $data + ); + } + } } diff --git a/tests/phpunit/includes/Listeners/CommerceTest.php b/tests/phpunit/includes/Listeners/CommerceTest.php index af90502..ed3f3b1 100644 --- a/tests/phpunit/includes/Listeners/CommerceTest.php +++ b/tests/phpunit/includes/Listeners/CommerceTest.php @@ -61,4 +61,37 @@ function ( Event $event ) { $this->assertConditionsMet(); } + + /** + * @covers ::woopay_connection + */ + public function test_woopay_connection(): void { + + WP_Mock::userFunction( 'is_ssl' )->once()->andReturnTrue(); + $_SERVER['HTTP_HOST'] = 'example.com/'; + $_SERVER['REQUEST_URI'] = 'subdir'; + + $expected_data_array = array( + 'label_key' => 'provider', + 'provider' => 'woopay', + 'page' => 'https://example.com/subdir', + ); + + $sut = Mockery::mock( Commerce::class )->makePartial(); + $sut->shouldAllowMockingProtectedMethods(); + $sut->expects( 'push' )->once() + ->with( 'payment_connected', $expected_data_array ); + + $wcpay_account_data = array( + 'data' => array( + 'account_id' => 'acc_123456789', + 'status' => 'connected', + 'last_updated' => '2025-01-08T12:34:56Z', + ), + ); + + $sut->woopay_connection( $wcpay_account_data, '' ); + + $this->assertConditionsMet(); + } }