56 lines
2.3 KiB
PHP
56 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Modules\Core\Shipping\Http\Controllers;
|
||
|
|
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Routing\Controller;
|
||
|
|
use Modules\Core\Shipping\Contracts\CarrierFulfillmentInterface;
|
||
|
|
use Modules\Core\Shipping\Models\Shipment;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Streams a carrier's raw label bytes (CarrierFulfillmentInterface::
|
||
|
|
* printLabel() — already whatever file format the carrier's own API
|
||
|
|
* returns, e.g. a PDF for both ACS and Box Now today) straight to the
|
||
|
|
* browser. Only reachable via a short-lived signed URL (see
|
||
|
|
* Modules\Core\Shipping\Extensions\OrderShipmentsExtension's "Print
|
||
|
|
* Label" action) — the same auth model Lunar's own vendor
|
||
|
|
* DownloadPdfController uses for order PDFs (a valid signature IS the
|
||
|
|
* auth check; there is no separate staff-session check here, matching
|
||
|
|
* that precedent), so the link only works for the few minutes it's
|
||
|
|
* actually open in a browser tab.
|
||
|
|
*
|
||
|
|
* Looks the Shipment up manually from a plain {shipment} id rather than
|
||
|
|
* relying on implicit route-model-binding — this route is registered via
|
||
|
|
* loadRoutesFrom() with no middleware group (see
|
||
|
|
* Modules\Core\Providers\ShippingServiceProvider::boot()), so
|
||
|
|
* SubstituteBindings never runs and a type-hinted Shipment parameter
|
||
|
|
* silently resolves to an empty, non-existent model instead of 404ing.
|
||
|
|
*
|
||
|
|
* Sets label_printed_at as a side effect of a successful stream — this is
|
||
|
|
* the first place in the codebase that actually delivers a label's bytes
|
||
|
|
* to a human; the existing Modules\Core\Shipping\Filament\Pages\
|
||
|
|
* ManagePickupManifests "Print" action calls printLabel() too, but only
|
||
|
|
* to mark the timestamp, discarding the returned bytes entirely (no
|
||
|
|
* download route existed until this one).
|
||
|
|
*/
|
||
|
|
class DownloadShipmentLabelController extends Controller
|
||
|
|
{
|
||
|
|
public function __invoke(Request $request, int $shipment)
|
||
|
|
{
|
||
|
|
if (! $request->hasValidSignature()) {
|
||
|
|
abort(401);
|
||
|
|
}
|
||
|
|
|
||
|
|
$shipment = Shipment::findOrFail($shipment);
|
||
|
|
|
||
|
|
$service = app(CarrierFulfillmentInterface::class, ['carrier' => $shipment->carrier]);
|
||
|
|
|
||
|
|
$bytes = $service->printLabel($shipment);
|
||
|
|
|
||
|
|
return response($bytes, 200, [
|
||
|
|
'Content-Type' => 'application/pdf',
|
||
|
|
'Content-Disposition' => 'inline; filename="shipment-'.$shipment->tracking_reference.'.pdf"',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|