add_action('init', function () { if (!isset($_GET['chargily_webhook'])) { return; } $payload = file_get_contents("php://input"); $event = json_decode($payload, true); if (!$event) { http_response_code(400); exit('Invalid JSON'); } // đ„ VĂ©rifier type d'Ă©vĂ©nement if (($event['type'] ?? '') !== 'checkout.paid') { http_response_code(200); exit('Event ignored'); } $checkout = $event['data'] ?? null; if (!$checkout) { http_response_code(400); exit('No checkout data'); } // đ§ STATUS CHECK if (($checkout['status'] ?? '') !== 'paid') { http_response_code(200); exit('Not paid'); } // đš IMPORTANT: rĂ©cupĂ©ration commande $order_id = null; // CASE 1: metadata future (recommandĂ©) if (!empty($checkout['metadata']['order_id'])) { $order_id = $checkout['metadata']['order_id']; } // CASE 2: fallback (si tu n'as pas metadata) if (!$order_id && !empty($checkout['id'])) { // â ïž ici on ne peut pas deviner la commande â log erreur error_log("Chargily webhook: order_id missing for checkout " . $checkout['id']); http_response_code(200); exit('No order mapping'); } $order = wc_get_order($order_id); if (!$order) { http_response_code(404); exit('Order not found'); } // đł MARK AS PAID $order->payment_complete(); // đ NOTE $order->add_order_note('Paiement confirmĂ© via Chargily webhook (checkout.paid)'); // đŠ STOCK wc_reduce_stock_levels($order_id); http_response_code(200); exit('OK'); });






