UNPKG

aramex-pillvery

Version:

A simple wrapper for Aramex (JSON) API to manage shipments, tracking, and printing labels.

193 lines (186 loc) 5.75 kB
// aramex.js const axios = require('axios'); class Aramex { /** * Initialize the Aramex API client. * @param {Object} config - Configuration for the Aramex API. * @param {string} config.UserName - Aramex API username. * @param {string} config.Password - Aramex API password. * @param {string} config.AccountNumber - Aramex account number. * @param {string} config.AccountPin - Aramex account pin. * @param {boolean} [config.isTesting=true] - Use the testing environment if true. */ constructor({ UserName, Password, AccountNumber, AccountPin, isTesting = true }) { this.ClientInfo = { UserName, Password, AccountNumber, AccountPin, Version: "v1", AccountEntity: "AMM", AccountCountryCode: "JO", Source: 24, }; this.url = isTesting ? "https://ws.sbx.aramex.net/ShippingAPI.V2/Shipping/Service_1_0.svc/json/CreateShipments" : "https://ws.aramex.net/ShippingAPI.V2/Shipping/Service_1_0.svc/json/CreateShipments"; } /** * Creates a shipment with the Aramex API. * @param {Object} params - The input parameters. * @param {string} params.client_order_id - Client order ID. * @param {number} params.value - Value of the shipment. * @param {string} params.customer_name - Customer name. * @param {string} params.customer_phone - Customer phone number. * @param {number} params.pickup_lat - Pickup location latitude. * @param {number} params.pickup_lng - Pickup location longitude. * @param {number} params.lat - Destination latitude. * @param {number} params.lng - Destination longitude. * @param {number} [params.preparation_time=1] - Preparation time in minutes. * @param {number} [params.payment_type=1] - Payment type (1 = CASH, 2 = CREDIT). * @returns {Promise<Object>} - The response from the Aramex API. */ async createShipment({ client_order_id, value, customer_name, customer_phone, pickup_lat, pickup_lng, lat, lng, preparation_time = 1, payment_type = 1, }) { // Calculate shipping date time const shippingDateTime = "\/Date("+(Date.now() + preparation_time * 60000)+")\/" // Overwrite value if payment_type is CREDIT if (payment_type === 2) { value = 0; } const requestBody = { ClientInfo: this.ClientInfo, Shipments: [ { Reference1: client_order_id, Reference2: "", Reference3: "", Shipper: { Reference1: "", Reference2: "", AccountNumber: this.ClientInfo.AccountNumber, PartyAddress: { Line1: "Pillvery", Line2: "", Line3: "", City: "AMMAN", StateOrProvinceCode: "", PostCode: "", CountryCode: "JO", Longitude: pickup_lng, Latitude: pickup_lat, BuildingNumber: null, BuildingName: null, Floor: null, Apartment: null, POBox: null, Description: null, }, Contact: { Department: "", PersonName: "Pillvery", Title: "", CompanyName: "Pillvery", PhoneNumber1: "962799647647", PhoneNumber1Ext: "", PhoneNumber2: "", PhoneNumber2Ext: "", FaxNumber: "", CellPhone: "962799647647", EmailAddress: "zaid@pillvery.com", Type: "", }, }, Consignee: { Reference1: "", Reference2: "", AccountNumber: "", PartyAddress: { Line1: "Amman", Line2: "", Line3: "", City: "AMMAN", StateOrProvinceCode: "", PostCode: "", CountryCode: "JO", Longitude: lng, Latitude: lat, BuildingNumber: "", BuildingName: "", Floor: "", Apartment: "", POBox: null, Description: "", }, Contact: { Department: "", PersonName: customer_name, Title: "", CompanyName: "Pillvery", PhoneNumber1: customer_phone, PhoneNumber1Ext: "", PhoneNumber2: "", PhoneNumber2Ext: "", FaxNumber: "", CellPhone: customer_phone, Type: "", }, }, Details: { Dimensions: { Length: 10, Width: 10, Height: 10, Unit: "CM", }, ActualWeight: { Unit: "KG", Value: 0.5, }, ChargeableWeight: { Unit: "KG", Value: 0.5, }, CashOnDeliveryAmount: payment_type === 1 ? { CurrencyCode: "JOD", Value: 10, }: null, ProductGroup: "DOM", ProductType: "ONP", PaymentType: "P", }, ShippingDateTime: shippingDateTime, }, ], LabelInfo: { ReportID: 9729, ReportType: "URL", }, Transaction: { Reference1: "", Reference2: "", Reference3: "", Reference4: "", Reference5: "", }, }; try { const response = await axios.post(this.url, requestBody); return response.data; } catch (error) { console.error("Error creating shipment:", error); throw error; } } } module.exports = Aramex;