127 lines
4.9 KiB
JavaScript
127 lines
4.9 KiB
JavaScript
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.packzy.com/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 endpoints = [
|
|
`/status_by_trackingcode/${encodeURIComponent(trackingCode)}`,
|
|
`/status_by_cid/${encodeURIComponent(trackingCode)}`,
|
|
`/status_by_invoice/${encodeURIComponent(trackingCode)}`,
|
|
];
|
|
let lastErr = null;
|
|
for (const ep of endpoints) {
|
|
try {
|
|
const response = await this.client.get(ep);
|
|
const data = response.data;
|
|
if (data && (data.status === 200 || data.delivery_status)) {
|
|
return {
|
|
success: true,
|
|
courier: "steadfast",
|
|
tracking_code: trackingCode,
|
|
status: data.delivery_status || "in_review",
|
|
updated_at: data.updated_at || new Date().toISOString(),
|
|
raw_response: data,
|
|
};
|
|
}
|
|
}
|
|
catch (err) {
|
|
lastErr = err;
|
|
}
|
|
}
|
|
// Steadfast Packzy returns HTTP 401/404 when a consignment ID does not belong to the connected merchant account
|
|
const status = lastErr?.response?.status;
|
|
if (status === 401 || status === 404 || status === 400) {
|
|
return {
|
|
success: false,
|
|
courier: "steadfast",
|
|
tracking_code: trackingCode,
|
|
status: "not_found_in_steadfast_account",
|
|
updated_at: new Date().toISOString(),
|
|
raw_response: {
|
|
live_api_status: "Packzy API Queried Live",
|
|
http_status: status,
|
|
message: "Consignment / tracking code was not found under this merchant's Steadfast account.",
|
|
},
|
|
};
|
|
}
|
|
throw lastErr || new Error("Steadfast tracking lookup failed");
|
|
}
|
|
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;
|
|
}
|
|
}
|