diff --git a/inc/gateways/class-paypal-webhook-handler.php b/inc/gateways/class-paypal-webhook-handler.php index 0571eace9..2a55704cc 100644 --- a/inc/gateways/class-paypal-webhook-handler.php +++ b/inc/gateways/class-paypal-webhook-handler.php @@ -202,7 +202,10 @@ protected function verify_webhook_signature(string $raw_body): bool { return false; } - // Build verification request + // Build verification request. The signature covers the exact bytes PayPal + // sent, so the raw event must be embedded as-is. Decoding and re-encoding + // it (empty objects become [], number formatting and key escaping change) + // makes PayPal return FAILURE for genuine webhooks. $verify_data = [ 'auth_algo' => $auth_algo, 'cert_url' => $cert_url, @@ -210,9 +213,17 @@ protected function verify_webhook_signature(string $raw_body): bool { 'transmission_sig' => $transmission_sig, 'transmission_time' => $transmission_time, 'webhook_id' => $webhook_id, - 'webhook_event' => json_decode($raw_body, true), ]; + $verify_json = wp_json_encode($verify_data); + + if (! is_string($verify_json)) { + $this->log('Failed to encode webhook verification request', LogLevel::ERROR); + return false; + } + + $verify_json = substr($verify_json, 0, -1) . ',"webhook_event":' . trim($raw_body) . '}'; + // Get client credentials $client_id = wu_get_setting("paypal_rest_{$mode_prefix}_client_id", ''); $client_secret = wu_get_setting("paypal_rest_{$mode_prefix}_client_secret", ''); @@ -256,7 +267,7 @@ protected function verify_webhook_signature(string $raw_body): bool { 'Authorization' => 'Bearer ' . $access_token, 'Content-Type' => 'application/json', ], - 'body' => wp_json_encode($verify_data), + 'body' => $verify_json, 'timeout' => 30, ] );