gateway->verify($raw, $signature); return $this->store->atomic(function () use ($event, $raw): array { $hash = hash('sha256', $raw); $existing = $this->store->one('SELECT * FROM inbox WHERE provider_event_id = ?', [$event['id']]); if ($existing) { if (!hash_equals($existing['body_hash'], $hash)) { throw new Problem('event_id_collision', 409); } return ['id' => $existing['id'], 'duplicate' => true]; } $this->store->run('INSERT INTO inbox (provider_event_id, body_hash, session_id, type, received_at) VALUES (?, ?, ?, ?, ?)', [$event['id'], $hash, $event['session_id'], $event['type'], time()]); return ['id' => $this->store->one('SELECT id FROM inbox WHERE provider_event_id = ?', [$event['id']])['id'], 'duplicate' => false]; }); } public function process(int $id, bool $crashAfterInsert = false): string { $event = $this->store->one('SELECT * FROM inbox WHERE id = ?', [$id]); if (!$event || $event['status'] !== 'pending') { return 'already_processed'; } $this->store->run('UPDATE inbox SET attempts = attempts + 1 WHERE id = ? AND status = ?', [$id, 'pending']); // Current provider state resolves delivery order before the business transaction. $session = $this->gateway->retrieveSession($event['session_id']); SandboxGuard::object($session); return $this->store->atomic(function () use ($id, $event, $session, $crashAfterInsert): string { $current = $this->store->one('SELECT * FROM inbox WHERE id = ?', [$id]); if ($current['status'] !== 'pending') { return 'already_processed'; } $order = $this->store->one('SELECT * FROM orders WHERE session_id = ?', [$event['session_id']]); if (!$order && is_string($session['order_id'] ?? null)) { $candidate = $this->store->one('SELECT orders.*, checkout_operations.id AS operation_id FROM orders JOIN checkout_operations ON checkout_operations.order_id = orders.id WHERE orders.id = ?', [$session['order_id']]); if ($candidate && $candidate['session_id'] === null && ($session['operation_key'] ?? null) === 'lab.checkout.'.$candidate['operation_id'] && ($session['amount_minor'] ?? null) === $candidate['amount_minor'] && ($session['currency'] ?? null) === $candidate['currency'] && ($session['id'] ?? null) === $event['session_id']) { // Trusted operation metadata closes the webhook-before-response race. $this->store->run('UPDATE orders SET session_id = ? WHERE id = ? AND session_id IS NULL', [$session['id'], $candidate['id']]); $order = $this->store->one('SELECT * FROM orders WHERE id = ?', [$candidate['id']]); } } if (!$order) { $this->store->run("UPDATE inbox SET status = 'ignored', processed_at = ? WHERE id = ?", [time(), $id]); return 'unrelated_session'; } if (($session['id'] ?? null) !== $order['session_id'] || ($session['order_id'] ?? null) !== $order['id'] || ($session['amount_minor'] ?? null) !== $order['amount_minor'] || ($session['currency'] ?? null) !== $order['currency']) { throw new Problem('session_mismatch', 409); } $state = $session['payment_state'] ?? null; if (!in_array($state, ['unpaid', 'processing', 'requires_action', 'paid', 'failed', 'expired'], true)) { throw new Problem('invalid_payment_state', 502); } if ($order['payment_state'] !== 'paid') { $this->store->run('UPDATE orders SET payment_state = ? WHERE id = ?', [$state, $order['id']]); } if ($state === 'paid') { $this->store->run('INSERT OR IGNORE INTO fulfillments VALUES (?, ?, ?)', [$order['id'], $id, time()]); if ($crashAfterInsert) { throw new Problem('injected_worker_crash', 503); } $this->store->run("UPDATE orders SET fulfillment_state = 'fulfilled' WHERE id = ?", [$order['id']]); } $this->store->run("UPDATE inbox SET status = 'processed', processed_at = ? WHERE id = ?", [time(), $id]); return 'processed'; }); } }