index-main/site/config/routes/newsletter.php

117 lines
3.7 KiB
PHP
Raw Normal View History

<?php
return [
'pattern' => 'api/newsletter',
'method' => 'POST|OPTIONS',
'action' => function () {
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
die();
}
$config = kirby()->option('brevo');
$apiKey = $config['api_key'] ?? '';
$listId = (int)($config['list_id'] ?? 2);
$apiUrl = $config['api_url'] ?? 'https://api.brevo.com/v3/contacts';
if (empty($apiKey)) {
http_response_code(500);
die(json_encode(['error' => 'Server configuration error', 'message' => 'Brevo API key not configured']));
}
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if (!isset($data['email']) || empty($data['email'])) {
http_response_code(400);
die(json_encode(['error' => 'Email required']));
}
$email = filter_var($data['email'], FILTER_VALIDATE_EMAIL);
if ($email === false) {
http_response_code(400);
die(json_encode(['error' => 'Invalid email']));
}
$brevoData = [
'email' => $email,
'listIds' => [$listId],
'updateEnabled' => true,
];
if (isset($data['attributes']) && is_array($data['attributes']) && !empty($data['attributes'])) {
$brevoData['attributes'] = $data['attributes'];
}
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $apiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($brevoData),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'api-key: ' . $apiKey,
'User-Agent: Index-NGO-Newsletter',
],
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => true,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($response === false) {
http_response_code(500);
die(json_encode(['error' => 'Connection error', 'details' => $curlError]));
}
$responseData = json_decode($response, true);
switch ($httpCode) {
case 201:
case 204:
http_response_code(200);
die(json_encode(['success' => true, 'message' => 'Successfully subscribed', 'email' => $email]));
case 400:
$isDuplicate = isset($responseData['code']) && $responseData['code'] === 'duplicate_parameter';
http_response_code(400);
die(json_encode([
'error' => $isDuplicate ? 'email_already_exists' : 'invalid_data',
'message' => $isDuplicate ? 'You are already subscribed!' : 'Invalid email address.',
'user_message' => $isDuplicate ? 'Vous êtes déjà inscrit·e !' : 'Veuillez vérifier votre adresse email.',
]));
case 401:
http_response_code(500);
die(json_encode([
'error' => 'invalid_api_key',
'message' => 'Invalid API key',
'user_message' => 'Une erreur technique est survenue. Veuillez réessayer plus tard.',
]));
case 404:
http_response_code(500);
die(json_encode([
'error' => 'list_not_found',
'message' => 'Contact list not found',
'user_message' => 'Une erreur technique est survenue. Veuillez réessayer plus tard.',
]));
default:
http_response_code($httpCode);
die(json_encode([
'error' => 'api_error',
'message' => 'Error communicating with subscription service',
'user_message' => 'Une erreur est survenue. Veuillez réessayer.',
'http_code' => $httpCode,
]));
}
},
];