From 3a061f9c4309fd73c52859525b1a7f42e7fb40b6 Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Wed, 15 Nov 2023 08:00:33 -0300 Subject: [PATCH 001/246] WIP Notification Request --- src/ConnectionTest.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index 4079495e70..761a719546 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -9,6 +9,7 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds; +use Automattic\Jetpack\Connection\Client; use Automattic\Jetpack\Connection\Manager; use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Ads; use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\AdsCampaign; @@ -30,6 +31,7 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer; use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncerException; use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Psr\Container\ContainerInterface; +use Jetpack_Client; use Jetpack_Options; use WP_REST_Request as Request; @@ -208,6 +210,15 @@ protected function render_admin_page() { + + Test: send partner notification request to WPCOM + +

+ Send partner notification request +

+ + + Test Authenticated WCS Request: @@ -696,6 +707,36 @@ protected function handle_actions() { $this->response .= wp_remote_retrieve_body( $response ); } + if ( 'partner-notification-test' === $_GET['action'] && check_admin_referer( 'partner-notification-test' ) ) { + $blog_id = Jetpack_Options::get_option( 'id' ); + $gmc_id = $this->container->get( OptionsInterface::class )->get_merchant_id(); + $partner = 'google'; + $topic = 'product/update'; + $url = "https://public-api.wordpress.com/wpcom/v2/sites/{$blog_id}/partners/{$partner}/notifications"; + $remote_args = [ + 'method' => 'POST', + 'blog_id' => $blog_id, + 'timeout' => 30, + 'headers' => [ + 'x-woocommerce-topic' => $topic, + 'x-woocommerce-gmc-id' => $gmc_id, + 'x-woocommerce-partner-id' => "92341", + ], + 'body' => [ + 'product_id' => "35", + ], + 'url' => $url, + ]; + + $response = Client::remote_request( $remote_args, wp_json_encode( $remote_args['body'] ) ); + $body = wp_remote_retrieve_body( $response ); + $decoded_body = json_decode( $body, true ); + $decoded_hash = base64_decode( $decoded_body["hash"] ); + $hash = hash_hmac('sha1', implode( "\n", $decoded_body ) . "\n", "w0H1kcLi4P0e3p7ADETr4kdn1BTVhoUNcLyQzpAQ4X9diKtusQ7u4OhvMvUjiC9e", true); + $is_hash_ok = hash_equals( $hash, $decoded_hash ); + $this->response .= 'Partner notification response: ' . $body . 'Signed: ' . $is_hash_ok ? 'OK' : 'NOK'; + } + if ( 'wcs-auth-test' === $_GET['action'] && check_admin_referer( 'wcs-auth-test' ) ) { $url = trailingslashit( $this->get_connect_server_url() ) . 'connection/test'; $args = [ From 7bbfdc9d8f97ab8199da425a07efa7b443c2e78b Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Wed, 15 Nov 2023 13:09:42 -0300 Subject: [PATCH 002/246] Add Notification Partner to Connection Test --- src/ConnectionTest.php | 78 +++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index 761a719546..3fa0bda876 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -210,15 +210,6 @@ protected function render_admin_page() { - - Test: send partner notification request to WPCOM - -

- Send partner notification request -

- - - Test Authenticated WCS Request: @@ -636,6 +627,47 @@ protected function render_admin_page() { +
+ + +

Partner API Pull Integration

+
+ + + + + + + + + +
+ + response .= wp_remote_retrieve_body( $response ); } - if ( 'partner-notification-test' === $_GET['action'] && check_admin_referer( 'partner-notification-test' ) ) { + if ( 'partner-notification' === $_GET['action'] && check_admin_referer( 'partner-notification' ) ) { + if ( ! isset( $_GET['client_secret'], $_GET['client_id'], $_GET['topic'], $_GET['item_id'] ) ) { + $this->response .= "\n Client Secret, Client Id, Topic and Item ID are required."; + return; + } + $blog_id = Jetpack_Options::get_option( 'id' ); $gmc_id = $this->container->get( OptionsInterface::class )->get_merchant_id(); $partner = 'google'; - $topic = 'product/update'; + $topic = $_GET[ 'topic' ]; $url = "https://public-api.wordpress.com/wpcom/v2/sites/{$blog_id}/partners/{$partner}/notifications"; $remote_args = [ 'method' => 'POST', @@ -720,10 +757,10 @@ protected function handle_actions() { 'headers' => [ 'x-woocommerce-topic' => $topic, 'x-woocommerce-gmc-id' => $gmc_id, - 'x-woocommerce-partner-id' => "92341", + 'x-woocommerce-partner-id' => $_GET['client_id'], ], 'body' => [ - 'product_id' => "35", + 'item_id' => $_GET['item_id'], ], 'url' => $url, ]; @@ -731,10 +768,17 @@ protected function handle_actions() { $response = Client::remote_request( $remote_args, wp_json_encode( $remote_args['body'] ) ); $body = wp_remote_retrieve_body( $response ); $decoded_body = json_decode( $body, true ); - $decoded_hash = base64_decode( $decoded_body["hash"] ); - $hash = hash_hmac('sha1', implode( "\n", $decoded_body ) . "\n", "w0H1kcLi4P0e3p7ADETr4kdn1BTVhoUNcLyQzpAQ4X9diKtusQ7u4OhvMvUjiC9e", true); - $is_hash_ok = hash_equals( $hash, $decoded_hash ); - $this->response .= 'Partner notification response: ' . $body . 'Signed: ' . $is_hash_ok ? 'OK' : 'NOK'; + $response_hash = base64_decode( wp_remote_retrieve_header( $response,'x-woocommerce-signature' ) ); + $hash = hash_hmac( 'sha1', serialize( $decoded_body ), $_GET['client_secret'], true ); + + $is_hash_ok = hash_equals( $hash, $response_hash ); + $this->response .= "\n Serialized Partner Notification response: " . $body; + if ( $is_hash_ok ) { + $this->response .= "\n OK - HMAC Signature is present and matches"; + } else { + $this->response .= "\n NOK - HMAC Signature is not present or doesn't match"; + } + return; } if ( 'wcs-auth-test' === $_GET['action'] && check_admin_referer( 'wcs-auth-test' ) ) { From b090bdf25741bcc92e66d34897ea2e9c4ded9968 Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Tue, 21 Nov 2023 16:21:11 -0300 Subject: [PATCH 003/246] Update serialization and sec algorithm --- src/ConnectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index 3fa0bda876..29496fa70a 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -769,7 +769,7 @@ protected function handle_actions() { $body = wp_remote_retrieve_body( $response ); $decoded_body = json_decode( $body, true ); $response_hash = base64_decode( wp_remote_retrieve_header( $response,'x-woocommerce-signature' ) ); - $hash = hash_hmac( 'sha1', serialize( $decoded_body ), $_GET['client_secret'], true ); + $hash = hash_hmac( 'sha256', wp_json_encode( $decoded_body ), $_GET['client_secret'], true ); $is_hash_ok = hash_equals( $hash, $response_hash ); $this->response .= "\n Serialized Partner Notification response: " . $body; From d0d5ace9aab4b6f23adab98a2bd8d6951e1f2d68 Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Fri, 1 Dec 2023 18:32:19 -0300 Subject: [PATCH 004/246] Update logic for testing Partner Notification --- src/ConnectionTest.php | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index 29496fa70a..5b23419169 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -652,9 +652,15 @@ protected function render_admin_page() { @@ -745,30 +751,31 @@ protected function handle_actions() { return; } - $blog_id = Jetpack_Options::get_option( 'id' ); - $gmc_id = $this->container->get( OptionsInterface::class )->get_merchant_id(); - $partner = 'google'; - $topic = $_GET[ 'topic' ]; - $url = "https://public-api.wordpress.com/wpcom/v2/sites/{$blog_id}/partners/{$partner}/notifications"; - $remote_args = [ + $blog_id = Jetpack_Options::get_option( 'id' ); + $merchant_domain = parse_url( $this->get_site_url() ); + + $partner = 'google'; + $topic = $_GET[ 'topic' ]; + $partner_endpoint = "https://shoppingdataintegration.googleapis.com/v1/webhooks/partners/woocommerce/merchants/" . $merchant_domain["host"] . "/types/" . $topic . ":webhookHandler"; + $url = "https://public-api.wordpress.com/wpcom/v2/sites/{$blog_id}/partners/{$partner}/notifications"; + $remote_args = [ 'method' => 'POST', - 'blog_id' => $blog_id, 'timeout' => 30, 'headers' => [ - 'x-woocommerce-topic' => $topic, - 'x-woocommerce-gmc-id' => $gmc_id, - 'x-woocommerce-partner-id' => $_GET['client_id'], + 'x-woocommerce-topic' => $topic, + 'x-woocommerce-partner-endpoint' => $partner_endpoint, + 'x-woocommerce-partner-id' => $_GET['client_id'], ], 'body' => [ 'item_id' => $_GET['item_id'], ], 'url' => $url, - ]; + ]; $response = Client::remote_request( $remote_args, wp_json_encode( $remote_args['body'] ) ); $body = wp_remote_retrieve_body( $response ); $decoded_body = json_decode( $body, true ); - $response_hash = base64_decode( wp_remote_retrieve_header( $response,'x-woocommerce-signature' ) ); + $response_hash = base64_decode( wp_remote_retrieve_header( $response,'X-WC-Webhook-Signature' ) ); $hash = hash_hmac( 'sha256', wp_json_encode( $decoded_body ), $_GET['client_secret'], true ); $is_hash_ok = hash_equals( $hash, $response_hash ); From 0e5af8dd957899ba8a09b9a4dbe90c507d7d9f29 Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:44:05 -0300 Subject: [PATCH 005/246] Update logic for Partner Notification Proxy --- src/ConnectionTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index 5b23419169..e09d382a14 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -752,18 +752,14 @@ protected function handle_actions() { } $blog_id = Jetpack_Options::get_option( 'id' ); - $merchant_domain = parse_url( $this->get_site_url() ); - $partner = 'google'; $topic = $_GET[ 'topic' ]; - $partner_endpoint = "https://shoppingdataintegration.googleapis.com/v1/webhooks/partners/woocommerce/merchants/" . $merchant_domain["host"] . "/types/" . $topic . ":webhookHandler"; $url = "https://public-api.wordpress.com/wpcom/v2/sites/{$blog_id}/partners/{$partner}/notifications"; $remote_args = [ 'method' => 'POST', 'timeout' => 30, 'headers' => [ 'x-woocommerce-topic' => $topic, - 'x-woocommerce-partner-endpoint' => $partner_endpoint, 'x-woocommerce-partner-id' => $_GET['client_id'], ], 'body' => [ From 6dd086db552d0b1931c1314714df236f2f22dd58 Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:40:47 -0300 Subject: [PATCH 006/246] Remove unnecessary client_id --- src/ConnectionTest.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index e09d382a14..8fd9c93ac3 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -641,10 +641,6 @@ protected function render_admin_page() { Product/Coupon/Shipping ID
- -
@@ -746,7 +742,7 @@ protected function handle_actions() { } if ( 'partner-notification' === $_GET['action'] && check_admin_referer( 'partner-notification' ) ) { - if ( ! isset( $_GET['client_secret'], $_GET['client_id'], $_GET['topic'], $_GET['item_id'] ) ) { + if ( ! isset( $_GET['client_secret'], $_GET['topic'], $_GET['item_id'] ) ) { $this->response .= "\n Client Secret, Client Id, Topic and Item ID are required."; return; } @@ -759,8 +755,7 @@ protected function handle_actions() { 'method' => 'POST', 'timeout' => 30, 'headers' => [ - 'x-woocommerce-topic' => $topic, - 'x-woocommerce-partner-id' => $_GET['client_id'], + 'x-woocommerce-topic' => $topic ], 'body' => [ 'item_id' => $_GET['item_id'], From 4e9ea21478e0acc0edfd9b496c9eca3f81c4237c Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Fri, 5 Jan 2024 13:42:20 -0500 Subject: [PATCH 007/246] Update Notification test call --- src/ConnectionTest.php | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index 8fd9c93ac3..7e5b407bf5 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -648,15 +648,12 @@ protected function render_admin_page() { @@ -755,7 +752,7 @@ protected function handle_actions() { 'method' => 'POST', 'timeout' => 30, 'headers' => [ - 'x-woocommerce-topic' => $topic + 'x-woocommerce-topic' => $topic, ], 'body' => [ 'item_id' => $_GET['item_id'], @@ -764,18 +761,7 @@ protected function handle_actions() { ]; $response = Client::remote_request( $remote_args, wp_json_encode( $remote_args['body'] ) ); - $body = wp_remote_retrieve_body( $response ); - $decoded_body = json_decode( $body, true ); - $response_hash = base64_decode( wp_remote_retrieve_header( $response,'X-WC-Webhook-Signature' ) ); - $hash = hash_hmac( 'sha256', wp_json_encode( $decoded_body ), $_GET['client_secret'], true ); - - $is_hash_ok = hash_equals( $hash, $response_hash ); - $this->response .= "\n Serialized Partner Notification response: " . $body; - if ( $is_hash_ok ) { - $this->response .= "\n OK - HMAC Signature is present and matches"; - } else { - $this->response .= "\n NOK - HMAC Signature is not present or doesn't match"; - } + $this->response .= "\n Serialized Partner Notification response: " . json_encode( $response ); return; } From 173b06c59ac2bd49bb342fad7334cb0e17a57f0b Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Fri, 5 Jan 2024 16:24:36 -0500 Subject: [PATCH 008/246] Update connection test page Notification Partner --- src/ConnectionTest.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index 7e5b407bf5..0cdf13dc4c 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -641,9 +641,6 @@ protected function render_admin_page() { Product/Coupon/Shipping ID
-