auth = $auth instanceof Akkedli_V2_Callback_Auth ? $auth : new Akkedli_V2_Callback_Auth(); $this->host_auth = $host_auth instanceof Akkedli_V2_Host_Binding_Auth ? $host_auth : new Akkedli_V2_Host_Binding_Auth(); $this->session_auth = class_exists('Akkedli_V2_Session_Proof_Auth') ? ($session_auth instanceof Akkedli_V2_Session_Proof_Auth ? $session_auth : new Akkedli_V2_Session_Proof_Auth()) : null; $this->outbox_ready = is_callable($outbox_ready) ? $outbox_ready : array(new Akkedli_V2_Outbox_Repository(), 'is_usable'); $this->contexts = new SplObjectStorage(); add_action('rest_api_init', array($this, 'register_route')); add_filter('rest_post_dispatch', array($this, 'add_no_store'), 10, 3); } public function register_route() { register_rest_route('akkedli/v2', '/verification-result', array( 'methods' => 'POST', 'callback' => array($this, 'handle'), 'permission_callback' => array($this, 'authorize'), )); } public function authorize(WP_REST_Request $request) { if ($this->contexts->contains($request)) { $this->contexts->detach($request); } try { $context = is_object($this->session_auth) ? $this->session_auth->authenticate($request) : false; if (false === $context) { $context = $this->host_auth->authenticate($request); } if (false === $context) { $context = $this->auth->authenticate($request); } } catch (Throwable $error) { unset($error); return new WP_Error( 'akkedli_v2_internal_error', 'Connector request failed.', array('status' => 500) ); } if (is_wp_error($context)) { return $context; } $this->contexts->attach($request, $context); return true; } public function handle(WP_REST_Request $request) { try { return $this->process($request); } catch (Throwable $error) { unset($error); return $this->failure('akkedli_v2_internal_error', 500, 'Connector request failed.'); } } private function process(WP_REST_Request $request) { if (!$this->contexts->contains($request)) { return $this->failure('akkedli_v2_authentication_failed', 401, 'Connector authentication failed.'); } $context = $this->contexts[$request]; $this->contexts->detach($request); if (isset($context['kind']) && 'connection_session' === $context['kind']) { $data = Akkedli_V2_Session_Proof::response($context); unset($context); return $this->response($data, 200); } if (isset($context['kind']) && 'host_binding' === $context['kind']) { if (!$this->host_binding_ready($context) || !$this->commit_host_binding($context)) { unset($context); return $this->failure( 'akkedli_v2_binding_commit_failed', 503, 'Connector binding is not ready.' ); } $data = Akkedli_V2_Host_Binding::response($context); unset($context); return $this->response($data, 200); } $now = time(); $claim = Akkedli_V2_Callback_Claims::claim($context, $now); if ('unavailable' === $claim['outcome']) { unset($context); return $this->failure('akkedli_v2_storage_unavailable', 503, 'Connector storage unavailable.'); } if ('conflict' === $claim['outcome']) { unset($context); return $this->failure('akkedli_v2_callback_conflict', 409, 'Connector callback conflict.'); } $lock = Akkedli_V2_Callback_Claims::acquire_order($context['order_id'], $now); if (!is_array($lock)) { Akkedli_V2_Callback_Claims::release($claim); unset($context, $claim); return $this->failure('akkedli_v2_callback_conflict', 409, 'Connector callback conflict.'); } try { $order = wc_get_order($context['order_id_int']); if (!is_object($order) || !is_a($order, 'WC_Order') || (string) $order->get_id() !== $context['order_id']) { Akkedli_V2_Callback_Claims::release($claim); return $this->failure('akkedli_v2_order_not_found', 404, 'Connector order not found.'); } $current = $order->get_status(); if (in_array($current, array('trash', 'auto-draft', 'checkout-draft'), true)) { Akkedli_V2_Callback_Claims::release($claim); return $this->failure('akkedli_v2_order_not_found', 404, 'Connector order not found.'); } if ('completed' === $claim['outcome']) { if (!Akkedli_V2_Callback_Claims::complete($claim, time())) { Akkedli_V2_Callback_Claims::release($claim); return $this->failure( 'akkedli_v2_storage_unavailable', 503, 'Connector storage unavailable.' ); } return $this->response(array( 'success' => true, 'data' => array('status' => 'updated'), ), 200); } if ('verified' === $context['verification_result']) { if (!Akkedli_Order_Confirmation::mark_confirmed($order)) { Akkedli_V2_Callback_Claims::release($claim); return $this->failure( 'akkedli_v2_transition_failed', 503, 'Connector transition failed.' ); } } else { $target = 'cancelled'; $ignore_stale_callback = $current !== $target && in_array($current, array('completed', 'refunded', 'shipped'), true); if ($current !== $target && !$ignore_stale_callback) { $suppression = Akkedli_V2_Outbox::suppress_order($context['order_id']); if (!is_string($suppression)) { Akkedli_V2_Callback_Claims::release($claim); return $this->failure('akkedli_v2_transition_failed', 503, 'Connector transition failed.'); } try { $updated = $order->update_status($target); } finally { Akkedli_V2_Outbox::release_suppression($suppression); unset($suppression); } if (false === $updated || is_wp_error($updated)) { Akkedli_V2_Callback_Claims::release($claim); return $this->failure('akkedli_v2_transition_failed', 503, 'Connector transition failed.'); } $reloaded = $this->reload_order($order, $context['order_id_int']); if (!is_object($reloaded) || !is_a($reloaded, 'WC_Order') || $reloaded === $order || (string) $reloaded->get_id() !== $context['order_id'] || $reloaded->get_status() !== $target) { Akkedli_V2_Callback_Claims::release($claim); return $this->failure('akkedli_v2_transition_failed', 503, 'Connector transition failed.'); } $order = $reloaded; } } if (!Akkedli_V2_Callback_Claims::complete($claim, time())) { Akkedli_V2_Callback_Claims::release($claim); return $this->failure('akkedli_v2_storage_unavailable', 503, 'Connector storage unavailable.'); } return $this->response(array( 'success' => true, 'data' => array('status' => 'updated'), ), 200); } catch (Throwable $error) { unset($error); Akkedli_V2_Callback_Claims::release($claim); return $this->failure('akkedli_v2_transition_failed', 503, 'Connector transition failed.'); } finally { Akkedli_V2_Callback_Claims::release_order($lock); unset($context, $claim, $lock, $order, $reloaded); } } private function commit_host_binding(array $context) { if (!isset($context['state']) || !is_array($context['state'])) { return false; } $expected = $context['state']; if ('v2_pending' === $expected['mode'] && Akkedli_Connector_State::replace( Akkedli_V2_Binding_State::verified($expected), $expected )) { return Akkedli_V2_Outbox_Recovery::authority_recovered(); } $current = Akkedli_Connector_State::load(); return is_array($current) && 'v2' === $current['mode'] && 1 === $current['protocol_version'] && hash_equals($context['credential_id'], $current['credential_id']) && hash_equals($expected['secret'], $current['secret']) && Akkedli_V2_Outbox_Recovery::authority_recovered(); } private function host_binding_ready(array $context) { $current = Akkedli_Connector_State::load(); return isset($context['state'], $context['credential_id']) && is_array($context['state']) && is_array($current) && in_array($current['mode'], array('v2_pending', 'v2'), true) && hash_equals($context['credential_id'], $current['credential_id']) && hash_equals($context['state']['secret'], $current['secret']) && true === call_user_func($this->outbox_ready); } public function add_no_store($response, $server, WP_REST_Request $request) { unset($server); if (Akkedli_V2_Callback_Auth::ROUTE !== $request->get_route()) { return $response; } if ($this->contexts->contains($request)) { $this->contexts->detach($request); } if (is_object($response) && method_exists($response, 'header')) { $response->header('Cache-Control', 'private, no-store'); } return $response; } private function reload_order($order, $order_id) { $cache_class = 'Automattic\\WooCommerce\\Caches\\OrderCache'; if (class_exists($cache_class) && function_exists('wc_get_container')) { $cache = wc_get_container()->get($cache_class); if (!is_object($cache) || !method_exists($cache, 'remove')) { return false; } $cache->remove($order_id); } if (function_exists('clean_post_cache')) { clean_post_cache($order_id); } $reloaded = wc_get_order($order_id); return $reloaded === $order ? false : $reloaded; } private function failure($code, $status, $message) { return $this->response(array( 'success' => false, 'error' => $code, 'code' => $code, 'message' => $message, ), $status); } private function response(array $data, $status) { $response = new WP_REST_Response($data, $status); $response->header('Cache-Control', 'private, no-store'); return $response; } }