diff --git a/app/Http/Controllers/ShippingController.php b/app/Http/Controllers/ShippingController.php index 58d7acc..fbfd483 100644 --- a/app/Http/Controllers/ShippingController.php +++ b/app/Http/Controllers/ShippingController.php @@ -227,11 +227,12 @@ class ShippingController extends Controller $order = collect($orders)->first(fn ($order) => (string) ($order['OrderLineId'] ?? '') === $orderLineId); $hash = $order ? $parcel2Go->labelHash($order) : null; abort_unless($hash, 404); - $pdf = $parcel2Go->labelPdf($orderLineId, $hash); + $size = $request->query('size') === '4x6' ? '4x6' : 'a4'; + $pdf = $parcel2Go->labelPdf($orderLineId, $hash, $size === '4x6' ? 'Label4x6' : 'A4'); return response($pdf, 200, [ 'Content-Type' => 'application/pdf', - 'Content-Disposition' => 'attachment; filename="parcel2go-'.$orderLineId.'.pdf"', + 'Content-Disposition' => 'attachment; filename="parcel2go-'.$orderLineId.'-'.$size.'.pdf"', 'Cache-Control' => 'private, no-store', ]); } diff --git a/app/Services/Parcel2Go.php b/app/Services/Parcel2Go.php index 0cead32..195ff29 100644 --- a/app/Services/Parcel2Go.php +++ b/app/Services/Parcel2Go.php @@ -182,13 +182,14 @@ class Parcel2Go return filled($query['hash'] ?? null) ? (string) $query['hash'] : null; } - public function labelPdf(string $reference, string $hash): string + /** @param 'A4'|'Label4x6' $media */ + public function labelPdf(string $reference, string $hash, string $media = 'A4'): string { $token = $this->profileConnected() ? $this->profileAccessToken() : $this->accessToken(); $data = Http::withToken($token)->acceptJson()->connectTimeout(3)->timeout(20) ->get($this->url('labels/'.$reference), [ 'referenceType' => 'OrderLineId', 'detailLevel' => 'Labels', - 'labelMedia' => 'A4', 'labelFormat' => 'PDF', 'hash' => $hash, + 'labelMedia' => $media, 'labelFormat' => 'PDF', 'hash' => $hash, ])->throw()->json(); $encoded = $data['Base64EncodedLabels'][0] ?? ''; $pdf = is_string($encoded) ? base64_decode($encoded, true) : false; diff --git a/resources/views/shipping.blade.php b/resources/views/shipping.blade.php index 6a63bfe..bb3bb84 100644 --- a/resources/views/shipping.blade.php +++ b/resources/views/shipping.blade.php @@ -35,7 +35,7 @@

Existing shipments

Current Parcel2Go progress and labels from your account history.

@forelse(($orders['Orders'] ?? []) as $order) - + @empty@endforelse
OrderCourierServiceProgressEstimated deliveryRecipientDestinationLabel
{{ $order['OrderLineId'] ?? '—' }}{{ $order['Courier'] ?? '—' }}{{ $order['Service'] ?? '—' }} $order['Tracking']['Delivered'] ?? false, 'bg-sky-500/10 text-sky-300' => ! ($order['Tracking']['Delivered'] ?? false)])>{{ ($order['Tracking']['Delivered'] ?? false) ? 'Delivered' : 'In transit' }}{{ app(\App\Services\Display::class)->dateTime($order['EstimatedDeliveryDate'] ?? null) }}{{ $order['DeliveryAddress']['ContactName'] ?? '—' }}{{ app(\App\Services\Display::class)->flag($order['DeliveryAddress'] ?? []) }}@if(app(\App\Services\Parcel2Go::class)->labelHash($order) && ! empty($order['OrderLineId']))Download PDF@elseUnavailable@endif
{{ $order['OrderLineId'] ?? '—' }}{{ $order['Courier'] ?? '—' }}{{ $order['Service'] ?? '—' }} $order['Tracking']['Delivered'] ?? false, 'bg-sky-500/10 text-sky-300' => ! ($order['Tracking']['Delivered'] ?? false)])>{{ ($order['Tracking']['Delivered'] ?? false) ? 'Delivered' : 'In transit' }}{{ app(\App\Services\Display::class)->dateTime($order['EstimatedDeliveryDate'] ?? null) }}{{ $order['DeliveryAddress']['ContactName'] ?? '—' }}{{ app(\App\Services\Display::class)->flag($order['DeliveryAddress'] ?? []) }}@if(app(\App\Services\Parcel2Go::class)->labelHash($order) && ! empty($order['OrderLineId']))4×6A4@elseUnavailable@endif
{{ $historyAvailable ? 'No shipments in Parcel2Go account history.' : 'Shipment history is unavailable. Check the Client Credentials in Settings.' }}
@endsection diff --git a/tests/Feature/IntegrationsTest.php b/tests/Feature/IntegrationsTest.php index f926e40..14710de 100644 --- a/tests/Feature/IntegrationsTest.php +++ b/tests/Feature/IntegrationsTest.php @@ -210,6 +210,21 @@ class IntegrationsTest extends TestCase Http::assertSent(fn ($request) => str_contains($request->url(), '/labels/123') && str_contains($request->url(), 'hash=link%2Bhash%3D')); } + public function test_shipping_label_can_be_requested_in_4x6_format(): void + { + Http::preventStrayRequests(); + Http::fake([ + 'https://auth.example.test/token' => Http::response(['access_token' => 'test-token']), + 'https://shipping.example.test/api/me/orders/detail' => Http::response(['Orders' => [['OrderLineId' => 123, 'Hash' => 'order-hash']]]), + 'https://shipping.example.test/api/labels/123*' => Http::response(['Base64EncodedLabels' => [base64_encode('%PDF-1.4 test label')]]), + ]); + config()->set('services.parcel2go', ['client_id' => 'id', 'client_secret' => 'secret', 'auth_url' => 'https://auth.example.test/token', 'base_url' => 'https://shipping.example.test/api']); + + $this->actingAs(User::factory()->create(['role' => 'admin']))->get(route('shipping.label', [123, 'size' => '4x6'])) + ->assertOk()->assertHeader('content-disposition', 'attachment; filename="parcel2go-123-4x6.pdf"'); + Http::assertSent(fn ($request) => str_contains($request->url(), '/labels/123') && str_contains($request->url(), 'labelMedia=Label4x6')); + } + public function test_collection_booking_uses_quoted_service_and_available_date(): void { $this->pendingEtsyOrder();