فهرست مستندات پروژه
دستورپخت
مقایسه چند شرکت حمل
$ersal = new Ersal();
$request = new QuoteRequest(origin: $o, destination: $d, parcel: $p);
$cheapest = null;
foreach ([
['tipax', new TipaxConfig('tipax-token')],
['chapar', new ChaparConfig('chapar-key')],
['post', new PostConfig(username: 'u', password: 'p', contractCode: 'c')],
] as [$alias, $config]) {
$provider = $ersal->create($alias, $config);
try {
foreach ($provider->quote($request) as $quote) {
if ($cheapest === null || $quote->cost->lessThan($cheapest->cost)) {
$cheapest = $quote;
}
}
} catch (ProviderException $e) {
// این شرکت این مسیر را سرویسدهی نمیکند — رد شو
}
}
echo "ارزانترین: {$cheapest->providerName} — {$cheapest->cost->inToman()} تومان";
ثبت با پسکرایه
if ($provider instanceof SupportsCOD) {
$booking = (new BookingRequest(
origin: $o, destination: $d, parcel: $p,
orderId: 'ORDER-123',
))->withCashOnDelivery(Amount::fromToman(250_000));
$shipment = $provider->createShipment($booking);
}
برنامهریزی جمعآوری پیک
if ($provider instanceof SupportsPickup) {
$pickup = new PickupRequest(
windowStart: new DateTimeImmutable('tomorrow 10:00'),
windowEnd: new DateTimeImmutable('tomorrow 12:00'),
instructions: 'در زدن کافی است، پلاک ۱۰۰ واحد ۳',
);
$shipment = $provider->schedulePickup($shipment->getId(), $pickup);
}
دانلود برچسب ارسال
if ($provider instanceof SupportsLabel) {
$label = $provider->getLabel($shipment->getId());
if ($label->hasBytes()) {
file_put_contents("labels/{$shipment->getId()}.{$label->format}", $label->bytes);
} elseif ($label->hasUrl()) {
header('Location: ' . $label->url);
}
}
یافتن نزدیکترین شعبه
if ($provider instanceof SupportsBranches) {
$branches = $provider->listBranches('تهران');
foreach ($branches as $branch) {
printf("%s — %s\n", $branch->name, $branch->address);
}
}
Polling تا تحویل
use Eram\Ersal\Shipment\ShipmentStatus;
while (true) {
$shipment = $provider->track($shipmentId);
if ($shipment->getStatus()->isTerminal()) {
break;
}
sleep(900); // ۱۵ دقیقه — محدودیت نرخ شرکت را رعایت کنید
}
if ($shipment->getStatus() === ShipmentStatus::Delivered) {
$this->notifyCustomerOfDelivery($shipment);
}
محاسبه وزن محاسبهپذیر
$parcel = new Parcel(
weightGrams: 500, // سبک ولی حجیم
lengthMm: 500, widthMm: 400, heightMm: 300,
);
echo $parcel->weightGrams; // ۵۰۰
echo $parcel->volumetricWeightGrams(); // ۱۲,۰۰۰
echo $parcel->chargeableWeightGrams(); // ۱۲,۰۰۰ — بابت حجم شارژ میشوید
ادغام با فریمورک (Laravel)
// config/services.php
return [
'tipax' => ['token' => env('TIPAX_TOKEN')],
];
// در Service Provider
$this->app->singleton(Ersal::class, function () {
return new Ersal(
logger: new MonologAdapter($this->app['log']),
eventDispatcher: new LaravelDispatcher($this->app['events']),
);
});
// در Controller
public function ship(Request $request, Ersal $ersal)
{
$provider = $ersal->create('tipax', new TipaxConfig(
token: config('services.tipax.token'),
));
// ...
}⁅ersal new ersal request new quoterequest origin o destination d parcel p cheapest null foreach tipax new tipaxconfig tipax token chapar new chaparconfig chapar key post new postconfig username u password p contractcode c as alias config provider ersal create alias config try foreach provider quote request as quote if cheapest null quote cost lessthan cheapest cost cheapest quote catch providerexception e این شرکت این مسیر را سرویسدهی سرویس دهی نکرد رد شو echo ارز cheapest providername cheapest cost intoman توم if provider instanceof supportscod booking new bookingrequest origin o destination d parcel p orderid order 123 withcashondelivery amount fromtoman 250 000 shipment provider createshipment booking if provider instanceof supportspickup pickup new pickuprequest windowstart new datetimeimmutable tomorrow 10 00 windowend new datetimeimmutable tomorrow 12 00 instructions در زد کافی است پلاک 100 واحد 3 shipment provider schedulepickup shipment getid pickup if provider instanceof supportslabel label provider getlabel shipment getid if label hasbytes file put contents labels shipment getid label format label bytes elseif label hasurl header location label url if provider instanceof supportsbranches branches provider listbranches تهران foreach branches as branch printf s s n branch name branch address use eram ersal shipment shipmentstatus while true shipment provider track shipmentid if shipment getstatus isterminal break sleep 900 15 دقیقه محدودیت نرخ شرکت را رعایت کرد if shipment getstatus shipmentstatus delivered this notifycustomerofdelivery shipment parcel new parcel weightgrams 500 سبک ولی حجی lengthmm 500 widthmm 400 heightmm 300 echo parcel weightgrams 500 echo parcel volumetricweightgrams 12 000 echo parcel chargeableweightgrams 12 000 بابت حجم شارژ شد config services php return tipax token env tipax token در service provider this app singleton ersal class function return new ersal logger new monologadapter this app log eventdispatcher new laraveldispatcher this app events در controller public function ship request request ersal ersal provider ersal create tipax new tipaxconfig token config services tipax token⁆
⁅دستورپخت⁆
⁅مقایسه چند شرکت حملثبت با پسکرایهبرنامهریزی پس کرایهبرنامه ریزی جمعآوری جمعاوری جمع آوری پیکدانلود برچسب ارسالیافتن نزدیک شعبهpolling تا تحویلمحاسبه وزن محاسبهپذیرادغ محاسبهپذیرادغا محاسبه پذیرادغا با فریمورک فریم ورک laravel⁆