Purchase Code Verification Example
= $environment ?>
= htmlspecialchars($message, ENT_QUOTES, 'UTF-8') ?>
['min_range' => 1]]); $sandbox = str_starts_with($apiKey, 'mf_test_'); $host = parse_url($endpoint, PHP_URL_HOST); $isLocalSandbox = $sandbox && in_array($host, ['localhost', '127.0.0.1', '[::1]'], true) && parse_url($endpoint, PHP_URL_SCHEME) === 'http'; $configured = extension_loaded('curl') && $productId && $host && preg_match('/\Amf_(test|live)_[A-Za-z0-9]{48}\z/', $apiKey) && (parse_url($endpoint, PHP_URL_SCHEME) === 'https' || $isLocalSandbox); $environment = $configured ? ($sandbox ? 'Sandbox' : 'Live') : 'Configuration Required'; header('Content-Type: text/html; charset=UTF-8'); header('Cache-Control: no-store'); header('Referrer-Policy: no-referrer'); $message = null; $verified = false; if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') { $code = $_POST['purchase_code'] ?? null; if (! $configured) { $message = 'Ask the installer administrator to configure purchase verification.'; } elseif (! is_string($code) || ! preg_match('/\A[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}\z/i', trim($code))) { $message = 'Enter the full purchase code from your licence certificate.'; } else { $client = curl_init($endpoint); curl_setopt_array($client, [ CURLOPT_POST => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer '.$apiKey, 'Content-Type: application/json', 'Accept: application/json', ], CURLOPT_POSTFIELDS => json_encode(['product_id' => $productId, 'purchase_code' => trim($code)], JSON_THROW_ON_ERROR), CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 4, CURLOPT_TIMEOUT => 10, ]); $response = curl_exec($client); $status = curl_getinfo($client, CURLINFO_RESPONSE_CODE); $result = is_string($response) ? json_decode($response, true) : null; $verified = $status === 200 && ($result['valid'] ?? null) === true && ($result['product_id'] ?? null) === $productId && ($result['mode'] ?? null) === ($sandbox ? 'test' : 'live'); $message = match (true) { $verified => 'Purchase code verified. The installer can continue.', $status === 404 || $status === 422 => 'This purchase code is invalid for this product or environment.', $status === 401 || $status === 403 => 'Ask the installer administrator to check the API key and listing settings.', default => 'Purchase verification is temporarily unavailable. Please try again.', }; } } ?>
= $environment ?>
= htmlspecialchars($message, ENT_QUOTES, 'UTF-8') ?>