API apps > Register an app with return address * http://localhost:8000/callback * 2. Put your client id and secret below (or set PD24_CLIENT_ID / PD24_CLIENT_SECRET). * 3. php -S localhost:8000 prodesk24-sample-app.php * 4. Open http://localhost:8000 and tap "Connect your business". * * A real app keeps each business's key in its database (encrypted), not in the browser session. */ $config = [ 'prodesk24' => getenv('PD24_BASE') ?: 'https://prodesk24.com', // no /api at the end 'client_id' => getenv('PD24_CLIENT_ID') ?: 'pd24app_your_client_id', 'client_secret' => getenv('PD24_CLIENT_SECRET') ?: 'pd24sec_your_client_secret', 'redirect_uri' => getenv('PD24_REDIRECT') ?: 'http://localhost:8000/callback', 'scope' => 'read', // read, read_write or catalogue ]; session_start(); $path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH); /** Call the ProDesk24 API. */ function pd(string $method, string $path, array $config, ?array $body = null, ?string $key = null): array { $ch = curl_init($config['prodesk24'] . '/api/v1' . $path); $headers = ['Accept: application/json']; if ($key) { $headers[] = 'Authorization: Bearer ' . $key; } if ($body !== null) { $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); } curl_setopt_array($ch, [CURLOPT_CUSTOMREQUEST => $method, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_TIMEOUT => 20]); $raw = curl_exec($ch); $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $data = json_decode((string) $raw, true) ?: []; if ($status >= 400 || $status === 0) { throw new RuntimeException($data['error']['message'] ?? 'Could not reach ProDesk24.'); } return $data; } function h($v): string { return htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8'); } function page(string $title, string $body): void { echo '' . '' . h($title) . '
' . $body . '
'; } try { // 1. Send the owner to ProDesk24 to approve. if ($path === '/connect') { $_SESSION['pd_state'] = bin2hex(random_bytes(16)); header('Location: ' . $config['prodesk24'] . '/connect?' . http_build_query([ 'client_id' => $config['client_id'], 'redirect_uri' => $config['redirect_uri'], 'scope' => $config['scope'], 'state' => $_SESSION['pd_state'], ])); exit; } // 2. ProDesk24 sends the owner back here with a one-time code. if ($path === '/callback') { if (!hash_equals((string) ($_SESSION['pd_state'] ?? ''), (string) ($_GET['state'] ?? ''))) { throw new RuntimeException('This return did not come from our own connect link. Try again.'); } unset($_SESSION['pd_state']); if (!empty($_GET['error'])) { throw new RuntimeException($_GET['error'] === 'access_denied' ? 'You cancelled the connection.' : 'ProDesk24 said: ' . $_GET['error']); } // 3. Swap the code for this business's key (server to server, with the secret). $tok = pd('POST', '/oauth/token', $config, [ 'client_id' => $config['client_id'], 'client_secret' => $config['client_secret'], 'code' => (string) ($_GET['code'] ?? ''), 'redirect_uri' => $config['redirect_uri'], ]); $_SESSION['pd_key'] = $tok['api_key']; $_SESSION['pd_business'] = $tok['business']['name']; header('Location: /'); exit; } if ($path === '/disconnect') { session_destroy(); header('Location: /'); exit; } $key = $_SESSION['pd_key'] ?? null; if (!$key) { page('Connect', '

Stock and sales on your phone

' . '

See today\'s sales and what is running low, from your ProDesk24 business.

' . '

Connect your business

'); exit; } // 4. Use the key. $today = date('Y-m-d'); $sum = pd('GET', '/reports/sales-summary?group=day&from=' . $today . '&to=' . $today, $config, null, $key); $low = pd('GET', '/stock?low_only=1&per_page=10', $config, null, $key); $recent = pd('GET', '/sales?per_page=5', $config, null, $key); $cur = $sum['meta']['currency']; $body = '

' . h($_SESSION['pd_business']) . '

' . '

Today

' . h($cur) . ' ' . h(number_format((float) $sum['meta']['total_amount'], 2)) . '
' . '
' . (int) $sum['meta']['total_sales'] . ' sales
' . '

Running low

'; if (!$low['data']) { $body .= '
Nothing is below its reorder point.
'; } foreach ($low['data'] as $s) { $body .= '
' . h($s['name']) . '' . h($s['quantity']) . ' left
'; } $body .= '

Latest sales

'; foreach ($recent['data'] as $s) { $body .= '
' . h($s['receipt_number']) . ' ' . h(substr((string) $s['created_at'], 11, 5)) . '' . h($cur . ' ' . $s['total']) . '
'; } $body .= '

Disconnect

'; page('Today', $body); } catch (Throwable $e) { page('Problem', '
' . h($e->getMessage()) . '

Back

'); }