Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTR: do not send tracking data without code #834

Merged
merged 1 commit into from
Sep 11, 2024
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
14 changes: 12 additions & 2 deletions src/Service/MollieApi/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ public function shipOrder(string $mollieOrderId, string $salesChannelId, array $
$options = [];

if ($tracking instanceof ShipmentTrackingInfoStruct) {
$options['tracking'] = $tracking->toArray();
$trackingData = $tracking->toArray();

/** Make sure that tracking data is only set when the code has a value */
if ($trackingData['code'] !== '') {
$options['tracking'] = $trackingData;
}
}

$mollieOrder = $this->orderApiService->getMollieOrder($mollieOrderId, $salesChannelId);
Expand Down Expand Up @@ -105,7 +110,12 @@ public function shipItem(string $mollieOrderId, string $salesChannelId, string $
];

if ($tracking instanceof ShipmentTrackingInfoStruct) {
$options['tracking'] = $tracking->toArray();
$trackingData = $tracking->toArray();

/** Make sure that tracking data is only set when the code has a value */
if ($trackingData['code'] !== '') {
$options['tracking'] = $trackingData;
}
}

$mollieOrder = $this->orderApiService->getMollieOrder($mollieOrderId, $salesChannelId);
Expand Down
20 changes: 12 additions & 8 deletions src/Service/TrackingInfoStructFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function create(string $trackingCarrier, string $trackingCode, string $tr
private function createInfoStruct(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct
{
if (empty($trackingCarrier) && empty($trackingCode)) {
$this->logger->info('No tracking information provided for shipment.');
$this->logger->debug('No tracking information provided for shipment.');
return null;
}

Expand All @@ -113,22 +113,22 @@ private function createInfoStruct(string $trackingCarrier, string $trackingCode,
throw new \InvalidArgumentException('Missing Argument for Tracking Code!');
}

$this->logger->info('Creating tracking information for shipment.', [
$this->logger->debug('Creating tracking information for shipment.', [
'trackingCarrier' => $trackingCarrier,
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl
]);

// determine if the provided tracking code is actually a tracking URL
if (empty($trackingUrl) === true || $this->urlParsingService->isUrl($trackingCode)) {
$this->logger->info('Tracking code is a URL, parsing tracking code from URL.', [
$this->logger->debug('Tracking code is a URL, parsing tracking code from URL.', [
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl
]);

[$trackingCode, $trackingUrl] = $this->urlParsingService->parseTrackingCodeFromUrl($trackingCode);

$this->logger->info('Parsed tracking code from URL.', [
$this->logger->debug('Parsed tracking code from URL.', [
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl
]);
Expand All @@ -137,22 +137,26 @@ private function createInfoStruct(string $trackingCarrier, string $trackingCode,
# we just have to completely remove those codes, so that no tracking happens, but a shipping works.
# still, if we find multiple codes (because separators exist), then we use the first one only
if (mb_strlen($trackingCode) > self::MAX_TRACKING_CODE_LENGTH) {
$this->logger->info('Tracking code is too long, truncating.', ['trackingCode' => $trackingCode]);
$this->logger->debug('Tracking code is too long, truncating.', ['trackingCode' => $trackingCode]);
if (strpos($trackingCode, ',') !== false) {
$trackingCode = trim(explode(',', $trackingCode)[0]);
} elseif (strpos($trackingCode, ';') !== false) {
$trackingCode = trim(explode(';', $trackingCode)[0]);
}

$this->logger->info('Truncated tracking code.', ['trackingCode' => $trackingCode]);
$this->logger->debug('Truncated tracking code.', ['trackingCode' => $trackingCode]);

# if we are still too long, then simply remove the code
if (mb_strlen($trackingCode) > self::MAX_TRACKING_CODE_LENGTH) {
$this->logger->info('Tracking code is still too long, removing.', ['trackingCode' => $trackingCode]);
return new ShipmentTrackingInfoStruct($trackingCarrier, '', '');
$this->logger->warning('Tracking code is still too long, removing.', ['trackingCode' => $trackingCode]);
return null;
}
}

if (mb_strlen($trackingCode) === 0) {
$this->logger->warning('Tracking Code is empty');
return null;
}

# had the use case of this pattern, and it broke the sprintf below
if ($this->stringContains($trackingUrl, '%s%')) {
Expand Down
Loading