` string the merchant * copies from the Afileasy dashboard. It bundles everything the plugin needs: * * k → integration token (uuid) : webhook URL path token * s → webhook auth token (secret) : sent in the X-Afileasy-Token header * o → organization public key : the tracking script's data-afileasy id * u → Afileasy application URL : the base older keys build their URLs from * t → tracking script URL : the afileasy-script.js embedded on the storefront * w → order events endpoint (optional): where order events are POSTed * l → lead endpoint (optional) : where referred sign-ups are registered * a → API base URL (optional) : handed to the script as data-api-url */ class Connection { /** * Decode a raw connection key into its parts, or null when malformed. * * @return array|null */ public static function decode(string $key): ?array { $key = trim($key); if (strpos($key, 'afl_') !== 0) { return null; } $json = base64_decode(strtr(substr($key, 4), '-_', '+/'), true); if ($json === false) { return null; } $data = json_decode($json, true); if (! is_array($data)) { return null; } foreach (['k', 's', 'o', 'u', 't'] as $field) { if (empty($data[$field]) || ! is_string($data[$field])) { return null; } } $connection = [ 'k' => $data['k'], 's' => $data['s'], 'o' => $data['o'], 'u' => rtrim($data['u'], '/'), 't' => $data['t'], ]; foreach (['w', 'l', 'a'] as $field) { if (! empty($data[$field]) && is_string($data[$field])) { $connection[$field] = rtrim($data[$field], '/'); } } return $connection; } /** * Return the stored connection, or null when not connected. * * @return array|null */ public static function get(): ?array { $stored = get_option(AFILEASY_WC_OPTION); return is_array($stored) && ! empty($stored['k']) ? $stored : null; } /** * Persist a decoded connection (or clear it with null). * * @param array|null $connection */ public static function store(?array $connection): void { if ($connection === null) { delete_option(AFILEASY_WC_OPTION); return; } update_option(AFILEASY_WC_OPTION, $connection, false); } public static function webhookUrl(array $connection): string { if (! empty($connection['w'])) { return $connection['w']; } return $connection['u'].'/webhook/woocommerce/'.rawurlencode($connection['k']); } public static function leadUrl(array $connection): string { if (! empty($connection['l'])) { return $connection['l']; } return $connection['u'].'/api/track/lead'; } }