import axios from "axios"; export class SteadfastAdapter { courierName = "steadfast"; client; enabled; constructor(apiKey, secretKey, baseUrl) { this.enabled = Boolean(apiKey && secretKey); const resolvedBase = baseUrl || "https://portal.steadfast.com.bd/api/v1"; this.client = axios.create({ baseURL: resolvedBase, headers: { "Api-Key": apiKey, "Secret-Key": secretKey, "Content-Type": "application/json", }, timeout: 10000, }); } isConfigured() { return this.enabled; } async createParcel(req) { if (!this.enabled) { throw new Error("Steadfast Courier credentials (API-Key / Secret-Key) are not configured."); } const payload = { invoice: req.invoice, recipient_name: req.recipient_name, recipient_phone: req.recipient_phone, recipient_address: req.recipient_address, cod_amount: req.cod_amount, note: req.note || "Aura AI automated dispatch", }; const response = await this.client.post("/create_order", payload); const data = response.data; if (data.status !== 200 && data.status !== "success") { throw new Error(`Steadfast API error: ${JSON.stringify(data.errors || data.message || data)}`); } const consignment = data.consignment || {}; return { success: true, courier: "steadfast", tracking_code: consignment.tracking_code || String(consignment.consignment_id), consignment_id: consignment.consignment_id, invoice: consignment.invoice || req.invoice, status: consignment.status || "in_review", cod_amount: req.cod_amount, delivery_fee: consignment.delivery_fee, created_at: consignment.created_at || new Date().toISOString(), raw_response: data, }; } async trackParcel(trackingCode) { if (!this.enabled) { throw new Error("Steadfast Courier credentials are not configured."); } const response = await this.client.get(`/status_by_trackingcode/${encodeURIComponent(trackingCode)}`); const data = response.data; return { success: true, courier: "steadfast", tracking_code: trackingCode, status: data.delivery_status || "unknown", updated_at: data.updated_at || new Date().toISOString(), raw_response: data, }; } async getBalance() { if (!this.enabled) { throw new Error("Steadfast Courier credentials are not configured."); } const response = await this.client.get("/get_balance"); const data = response.data; return { success: true, courier: "steadfast", current_balance: Number(data.current_balance || 0), raw_response: data, }; } /** * 100% Real Live Steadfast Fraud Check API * Endpoint: GET /fraud_check/{phone} * Returns nationwide parcel history across Bangladesh: total_parcels, total_delivered, total_cancelled */ async fraudCheck(phone) { if (!this.enabled) { throw new Error("Steadfast Courier credentials are not configured."); } const cleanPhone = phone.replace(/\D/g, ""); const response = await this.client.get(`/fraud_check/${encodeURIComponent(cleanPhone)}`); return response.data; } }