64 lines
2.4 KiB
PHP
64 lines
2.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
return [
|
||
|
|
'debug' => true,
|
||
|
|
|
||
|
|
'languages' => true,
|
||
|
|
|
||
|
|
'routes' => [
|
||
|
|
[
|
||
|
|
'pattern' => 'snipcart-webhook',
|
||
|
|
'method' => 'POST',
|
||
|
|
'action' => function () {
|
||
|
|
// Webhook handler pour Snipcart
|
||
|
|
// Vérifie la signature et décrémente le stock
|
||
|
|
|
||
|
|
$requestBody = file_get_contents('php://input');
|
||
|
|
$event = json_decode($requestBody, true);
|
||
|
|
|
||
|
|
// Vérifier la signature Snipcart (à implémenter avec la clé secrète)
|
||
|
|
// $signature = $_SERVER['HTTP_X_SNIPCART_REQUESTTOKEN'] ?? '';
|
||
|
|
|
||
|
|
if (!$event || !isset($event['eventName'])) {
|
||
|
|
return Response::json(['error' => 'Invalid request'], 400);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Gérer l'événement order.completed
|
||
|
|
if ($event['eventName'] === 'order.completed') {
|
||
|
|
$order = $event['content'] ?? null;
|
||
|
|
|
||
|
|
if ($order && isset($order['items'])) {
|
||
|
|
foreach ($order['items'] as $item) {
|
||
|
|
$productId = $item['id'] ?? null;
|
||
|
|
$quantity = $item['quantity'] ?? 0;
|
||
|
|
|
||
|
|
if ($productId && $quantity > 0) {
|
||
|
|
// Trouver le produit par son snipcartId
|
||
|
|
$products = site()->index()->filterBy('intendedTemplate', 'product');
|
||
|
|
|
||
|
|
foreach ($products as $product) {
|
||
|
|
if ($product->slug() === $productId) {
|
||
|
|
// Décrémenter le stock
|
||
|
|
$currentStock = (int) $product->stock()->value();
|
||
|
|
$newStock = max(0, $currentStock - $quantity);
|
||
|
|
|
||
|
|
// Mettre à jour le stock dans toutes les langues
|
||
|
|
$product->update([
|
||
|
|
'stock' => $newStock
|
||
|
|
]);
|
||
|
|
|
||
|
|
kirby()->impersonate('kirby');
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return Response::json(['status' => 'success'], 200);
|
||
|
|
}
|
||
|
|
]
|
||
|
|
]
|
||
|
|
];
|