Skip to content

Commit

Permalink
feat: allow sender with feature header (#517)
Browse files Browse the repository at this point in the history
* feat: allow sender with feature header

INT-781

* feat: allow sender with feature header

INT-781

* refactor: remove debug statement

* fix: use groupBy correctly eliminating the need for a new method

* refactor: make compatible with php7.1

* refactor: make compatible with php7.1

* refactor: add expected type in group callable

* fix: implement feedback
  • Loading branch information
joerivanveen authored Jan 15, 2025
1 parent 14a7fc6 commit e80cd42
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
13 changes: 11 additions & 2 deletions src/Helper/MyParcelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,24 @@ public function createConcepts(): self
$newConsignments = $this->where('consignment_id', '!=', null)->toArray();
$this->addMissingReferenceId();

$grouped = $this->where('consignment_id', null)->groupBy(function(AbstractConsignment $item) {
return $item->getApiKey() . ($item->hasSender() ? '-sender' : '');
});

/* @var MyParcelCollection $consignments */
foreach ($this->where('consignment_id', null)->groupBy('api_key') as $consignments) {
foreach ($grouped as $consignments) {
$headers = MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT;
if ($consignments->first()->hasSender()) {
$headers += MyParcelRequest::HEADER_SET_CUSTOM_SENDER;
}

$data = (new CollectionEncode($consignments))->encode();
$request = (new MyParcelRequest())
->setUserAgents($this->getUserAgent())
->setRequestParameters(
$consignments->first()->apiKey,
$data,
MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT
$headers
)
->sendRequest();

Expand Down
34 changes: 34 additions & 0 deletions src/Model/Consignment/AbstractConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierFactory;
use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem;
use MyParcelNL\Sdk\src\Model\Recipient;
use MyParcelNL\Sdk\src\Services\CountryCodes;
use MyParcelNL\Sdk\src\Services\CountryService;
use MyParcelNL\Sdk\src\Support\Str;
Expand Down Expand Up @@ -522,6 +523,11 @@ abstract class AbstractConsignment
*/
private $state;

/**
* @var null|\MyParcelNL\Sdk\src\Model\Recipient
*/
private $sender;

/**
* @throws \Exception
*/
Expand All @@ -532,6 +538,34 @@ public function __construct()
: null;
}

/**
* If you set a sender, a feature header will be added to the request.
* Exporting the consignment to MyParcel will throw an error if your shop (by apikey) does not have this permission.
*
* @param Recipient $sender
* @return $this
*/
public function setSender(Recipient $sender): AbstractConsignment
{
$this->sender = $sender;

return $this;
}

public function hasSender(): bool
{
return isset($this->sender);
}

public function getSender(): ?Recipient
{
if (!$this->hasSender()) {
return null;
}

return $this->sender;
}

/**
* @return null|\MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier
*/
Expand Down
1 change: 1 addition & 0 deletions src/Model/MyParcelRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MyParcelRequest
];
public const HEADER_ACCEPT_APPLICATION_PDF = ['Accept' => 'application/pdf'];
public const HEADER_CONTENT_TYPE_RETURN_SHIPMENT = ['Content-Type' => 'application/vnd.return_shipment+json; charset=utf-8'];
public const HEADER_SET_CUSTOM_SENDER = ['x-dmp-set-custom-sender' => 'true'];

/* @deprecated use HEADER_CONTENT_TYPE_SHIPMENT, HEADER_ACCEPT_APPLICATION_PDF or HEADER_CONTENT_TYPE_RETURN_SHIPMENT */
public const REQUEST_HEADER_SHIPMENT = 'Content-Type: application/vnd.shipment+json;charset=utf-8;version=1.1';
Expand Down
4 changes: 2 additions & 2 deletions src/Services/CollectionEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ private function groupMultiColloConsignments()
{
return $this->consignments->groupBy(function (AbstractConsignment $consignment) {
if ($consignment->isPartOfMultiCollo()) {
return $consignment->getReferenceId();
return $consignment->getReferenceIdentifier();
}

return 'random_to_prevent_multi_collo_' . uniqid();
return 'random_to_prevent_multi_collo_' . uniqid('', true);
})->toArray();
}
}
13 changes: 9 additions & 4 deletions src/Services/ConsignmentEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,17 @@ private function encodeBase(): self
'carrier' => $consignment->getCarrierId(),
];

if ($consignment->getReferenceId()) {
$this->consignmentEncoded['reference_identifier'] = $consignment->getReferenceId();

if (($sender = $consignment->getSender())) {
$this->consignmentEncoded['sender'] = $sender->toArrayWithoutNull();
}

if (($id = $consignment->getReferenceIdentifier())) {
$this->consignmentEncoded['reference_identifier'] = $id;
}

if ($consignment->getCompany()) {
$this->consignmentEncoded['recipient']['company'] = $consignment->getCompany();
if (($company = $consignment->getCompany())) {
$this->consignmentEncoded['recipient']['company'] = $company;
}

return $this;
Expand Down

0 comments on commit e80cd42

Please sign in to comment.