Skip to content

Commit

Permalink
Merge pull request #114 from newfold-labs/feature/PRESS12-44-GA-event…
Browse files Browse the repository at this point in the history
…-for-Woopay-connection

PRESS12-44 Added a GA event for WooPay connection.
  • Loading branch information
wpscholar authored Jan 9, 2025
2 parents 9c810cf + 77fdef5 commit b2ce9be
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
24 changes: 21 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions includes/Listeners/Commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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
);
}
}
}
33 changes: 33 additions & 0 deletions tests/phpunit/includes/Listeners/CommerceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit b2ce9be

Please sign in to comment.