Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/v4/Endpoint/Booking/BookingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,31 @@ public static function single(
/** @return array<string, mixed> */
public function toArray(): array
{
// Bring's Booking API expects customerNumber inside each
// consignment's product object — not at the request root. Posting
// it at the root produces a per-consignment BOOK-INPUT-019
// ("Customer number must be provided") even though the SDK
// constructor enforces a non-empty value, because Bring reads it
// from the product slot and finds nothing there.
//
// We model customerNumber once on the request (it's per-request in
// practice) and inject it into each consignment's product at
// serialization time, so the public SDK shape stays unchanged.
$customerNumber = $this->customerNumber;
$consignments = array_map(
static function (Consignment $c) use ($customerNumber): array {
$arr = $c->toArray();
$arr['product']['customerNumber'] = $customerNumber;

return $arr;
},
$this->consignments,
);

return [
'schemaVersion' => $this->schemaVersion,
'consignments' => array_map(
static fn (Consignment $c): array => $c->toArray(),
$this->consignments,
),
'consignments' => $consignments,
'testIndicator' => $this->testIndicator,
'customerNumber' => $this->customerNumber,
];
}
}
3 changes: 2 additions & 1 deletion tests/v4/Endpoint/Booking/BookingApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public function testBookSerialisesAndPostsToCorrectUrl(): void

$body = json_decode((string) $req->getBody(), true);
self::assertSame('1', $body['schemaVersion']);
self::assertSame('PARCELS_NORWAY-10001234567', $body['customerNumber']);
self::assertArrayNotHasKey('customerNumber', $body, 'customerNumber must not be at request root — Bring expects it under consignments[].product');
self::assertSame('PARCELS_NORWAY-10001234567', $body['consignments'][0]['product']['customerNumber']);
self::assertTrue($body['testIndicator']);
self::assertSame(Product::HOME_DELIVERY_PARCEL->value, $body['consignments'][0]['product']['id']);
self::assertSame('EVARSLING', $body['consignments'][0]['product']['additionalServices'][0]['id']);
Expand Down