// Hook WooCommerce para enviar pedido a Billin tras pago add_action('woocommerce_thankyou', 'mt_sincronizar_pedido_billin', 10, 1); function mt_sincronizar_pedido_billin($order_id) { if (!$order_id) return; error_log('[Billin Sync] Pedido recibido: ' . $order_id); // Datos de conexión WooCommerce REST API $woocommerce_url = 'https://homewater.es/wp-json/wc/v3/orders/' . $order_id; $consumer_key = 'ck_cb38321eb2bbf6921aa367ba11c23eeb644b9e49'; $consumer_secret = 'cs_3b6c2008336c1ef6275c33654ead290129159119'; // Obtener token de Billin $access_token = mt_obtener_token_billin(); if (!$access_token) { error_log('[Billin Sync] No se pudo obtener el token de acceso.'); return; } // Llamada a WooCommerce API para obtener el pedido con autenticación básica correcta $response = wp_remote_get($woocommerce_url, [ 'headers' => [ 'Authorization' => 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret) ] ]); if (is_wp_error($response)) { error_log('[Billin Sync] Error al obtener pedido WooCommerce: ' . $response->get_error_message()); return; } $http_code = wp_remote_retrieve_response_code($response); $body_raw = wp_remote_retrieve_body($response); if ($http_code !== 200) { error_log("[Billin Sync] WooCommerce devolvió código HTTP $http_code"); error_log("[Billin Sync] Cuerpo recibido: " . $body_raw); return; } $order = json_decode($body_raw, true); if (!$order) { error_log('[Billin Sync] Error: la respuesta del pedido no es JSON válido.'); error_log('[Billin Sync] Contenido recibido: ' . $body_raw); return; } if (!isset($order['billing'])) { error_log('[Billin Sync] Error: no se encontró la sección billing en el pedido.'); return; } $first_name = trim($order['billing']['first_name'] ?? ''); $last_name = trim($order['billing']['last_name'] ?? ''); $email = trim($order['billing']['email'] ?? ''); if (empty($first_name) || empty($last_name) || empty($email)) { error_log('[Billin Sync] Pedido con campos de cliente incompletos: ' . 'first_name=' . $first_name . ' | last_name=' . $last_name . ' | email=' . $email); return; } $customer_name = $first_name . ' ' . $last_name; error_log('[Billin Sync] Cliente validado: ' . $customer_name . ' <' . $email . '>'); // Preparar líneas para Billin con cálculo de impuestos desglosado $lines = []; foreach ($order['line_items'] as $item) { $quantity = floatval($item['quantity']); $total_with_tax = floatval($item['total']); // Total con IVA incluido if ($quantity == 0) continue; // Suponemos IVA 21% $unit_price = round($total_with_tax / 1.21, 2); $tax_amount = round($total_with_tax - ($unit_price * $quantity), 2); $lines[] = [ 'name' => $item['name'], 'quantity' => $quantity, 'unitPrice' => $unit_price, 'totalAmount' => $total_with_tax, 'discountAmount' => 0, 'taxKey' => 'TAXTYPE_21', 'taxAmount' => $tax_amount, 'salesEqTaxAmount' => 0 ]; } $factura = [ 'contact' => [ 'fiscalName' => $customer_name, 'email' => $email ], 'lines' => $lines, 'currency' => $order['currency'] ]; error_log('[Billin Sync] Payload factura: ' . json_encode($factura)); // Enviar a Billin $billin_api_url = 'https://api.billin.net/v1/invoices'; $response_billin = wp_remote_post($billin_api_url, [ 'headers' => [ 'Authorization' => 'Bearer ' . $access_token, 'Content-Type' => 'application/json' ], 'body' => json_encode($factura) ]); if (is_wp_error($response_billin)) { error_log('[Billin Sync] Error al enviar factura: ' . $response_billin->get_error_message()); } else { $code = wp_remote_retrieve_response_code($response_billin); if ($code >= 200 && $code < 300) { error_log('[Billin Sync] Factura enviada correctamente a Billin para pedido #' . $order_id); } else { $body_billin = wp_remote_retrieve_body($response_billin); error_log('[Billin Sync] Error en respuesta Billin: HTTP ' . $code . ' - ' . $body_billin); } } } https://www.homewater.es/page-sitemap.xml 2025-03-29T07:25:30+00:00 https://www.homewater.es/product-sitemap.xml 2025-03-04T09:54:55+00:00 https://www.homewater.es/fusion_tb_category-sitemap.xml 2025-04-16T06:59:20+00:00 https://www.homewater.es/product_cat-sitemap.xml 2025-03-04T09:54:55+00:00 https://www.homewater.es/product_tag-sitemap.xml 2025-03-04T09:54:55+00:00 https://www.homewater.es/element_category-sitemap.xml 2025-03-29T07:25:30+00:00