3 * Integrates with WHMCS API to fetch customers, domains, products, etc.
5 * API Documentation: https://developers.whmcs.com/api/
8const axios = require('axios');
18 this.baseUrl = process.env.WHMCS_URL;
19 this.identifier = process.env.WHMCS_IDENTIFIER;
20 this.secret = process.env.WHMCS_SECRET;
22 if (!this.baseUrl || !this.identifier || !this.secret) {
23 throw new Error('WHMCS credentials not configured in .env');
28 * Make API request to WHMCS
29 * @param {string} action - WHMCS API action
30 * @param {object} params - Additional parameters
31 * @returns {Promise<object>} - API response
33 async apiRequest(action, params = {}) {
37 identifier: this.identifier,
43 // WHMCS API endpoint is at /includes/api.php relative to admin directory
44 const apiUrl = this.baseUrl + '/includes/api.php';
45 console.log(`[WHMCS] API Request: ${action} -> ${apiUrl}`);
47 const response = await axios.post(
49 new URLSearchParams(postData).toString(),
52 'Content-Type': 'application/x-www-form-urlencoded'
57 if (response.data.result === 'error') {
58 throw new Error(response.data.message || 'WHMCS API error');
63 console.error(`[WHMCS] API Error (${action}):`, error.message);
69 * Get all clients from WHMCS
70 * @param {object} options - Filter options
71 * @returns {Promise<Array>} - Array of clients
73 async getClients(options = {}) {
78 status = '' // Active, Inactive, Closed
81 const response = await this.apiRequest('GetClients', {
85 ...(status && { status })
88 return response.clients?.client || [];
92 * Get detailed information for a specific client
93 * @param {number} clientId - WHMCS client ID
94 * @returns {Promise<object>} - Client details
96 async getClientDetails(clientId) {
97 const response = await this.apiRequest('GetClientsDetails', {
106 * Get all domains from WHMCS
107 * @param {object} options - Filter options
108 * @returns {Promise<Array>} - Array of domains
110 async getDomains(options = {}) {
118 const response = await this.apiRequest('GetClientsDomains', {
121 ...(clientid && { clientid }),
122 ...(domainname && { domainname })
125 return response.domains?.domain || [];
129 * Get all products/services from WHMCS
130 * @param {object} options - Filter options
131 * @returns {Promise<Array>} - Array of products
133 async getProducts(options = {}) {
141 const response = await this.apiRequest('GetClientsProducts', {
144 ...(clientid && { clientid }),
145 ...(serviceid && { serviceid })
148 return response.products?.product || [];
152 * Get all invoices from WHMCS
153 * @param {object} options - Filter options
154 * @returns {Promise<Array>} - Array of invoices
156 async getInvoices(options = {}) {
161 status = '' // Unpaid, Paid, Cancelled, Refunded
164 const response = await this.apiRequest('GetInvoices', {
167 ...(userid && { userid }),
168 ...(status && { status })
171 return response.invoices?.invoice || [];
175 * Get all tickets from WHMCS
176 * @param {object} options - Filter options
177 * @returns {Promise<Array>} - Array of tickets
179 async getTickets(options = {}) {
184 status = '' // Open, Answered, Customer-Reply, Closed
187 const response = await this.apiRequest('GetTickets', {
190 ...(clientid && { clientid }),
191 ...(status && { status })
194 return response.tickets?.ticket || [];
198 * Get custom client fields
199 * @returns {Promise<Array>} - Array of custom fields
201 async getCustomFields() {
202 const response = await this.apiRequest('GetCustomFields', {
206 return response.customfields?.customfield || [];
210 * Export all clients with related data
211 * @returns {Promise<object>} - Complete export
213 async exportAllData() {
214 console.log('[WHMCS] Starting full data export...');
216 const clients = await this.getClients({ status: 'Active' });
217 console.log(`[WHMCS] Found ${clients.length} active clients`);
219 const allDomains = await this.getDomains();
220 console.log(`[WHMCS] Found ${allDomains.length} domains`);
222 const allProducts = await this.getProducts();
223 console.log(`[WHMCS] Found ${allProducts.length} products`);
225 const customFields = await this.getCustomFields();
226 console.log(`[WHMCS] Found ${customFields.length} custom fields`);
231 products: allProducts,
233 exportedAt: new Date().toISOString()
238 * Test API connection
239 * @returns {Promise<boolean>} - Connection status
241 async testConnection() {
243 const response = await this.apiRequest('GetClients', {
247 console.log('[WHMCS] ✅ API connection successful');
250 console.error('[WHMCS] ❌ API connection failed:', error.message);
256module.exports = WHMCSService;