-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* MOL-1196: Tracking * MOL-1196: fix pipeline * MOL-1196: extract to factory * MOL-1196: check tracking code lenght * MOL-1196: update comment --------- Co-authored-by: Vitalij Mik <[email protected]>
- Loading branch information
1 parent
c08c340
commit f52ae30
Showing
12 changed files
with
299 additions
and
240 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Service; | ||
|
||
use Kiener\MolliePayments\Struct\MollieApi\ShipmentTrackingInfoStruct; | ||
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity; | ||
|
||
class TrackingInfoStructFactory | ||
{ | ||
/** | ||
* Mollie throws an error with length >= 100 | ||
*/ | ||
const MAX_TRACKING_CODE_LENGTH = 99; | ||
|
||
public function createFromDelivery(OrderDeliveryEntity $orderDeliveryEntity): ?ShipmentTrackingInfoStruct | ||
{ | ||
$trackingCodes = $orderDeliveryEntity->getTrackingCodes(); | ||
$shippingMethod = $orderDeliveryEntity->getShippingMethod(); | ||
if ($shippingMethod === null) { | ||
return null; | ||
} | ||
/** | ||
* Currently we create one shipping in mollie for one order. one shipping object can have only one tracking code. | ||
* When we have multiple Tracking Codes, we do not know which tracking code we should send to mollie. So we just dont send any tracking information at all | ||
* | ||
* https://docs.mollie.com/reference/v2/shipments-api/create-shipment | ||
*/ | ||
if (count($trackingCodes) !== 1) { | ||
return null; | ||
} | ||
|
||
return $this->createInfoStruct((string)$shippingMethod->getName(), $trackingCodes[0], (string)$shippingMethod->getTrackingUrl()); | ||
} | ||
|
||
public function create(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct | ||
{ | ||
return $this->createInfoStruct($trackingCarrier, $trackingCode, $trackingUrl); | ||
} | ||
|
||
private function createInfoStruct(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct | ||
{ | ||
if (empty($trackingCarrier) && empty($trackingCode)) { | ||
return null; | ||
} | ||
|
||
if (empty($trackingCarrier)) { | ||
throw new \InvalidArgumentException('Missing Argument for Tracking Carrier!'); | ||
} | ||
|
||
if (empty($trackingCode)) { | ||
throw new \InvalidArgumentException('Missing Argument for Tracking Code!'); | ||
} | ||
|
||
# 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) { | ||
if (strpos($trackingCode, ',') !== false) { | ||
$trackingCode = trim(explode(',', $trackingCode)[0]); | ||
} elseif (strpos($trackingCode, ';') !== false) { | ||
$trackingCode = trim(explode(';', $trackingCode)[0]); | ||
} | ||
|
||
# if we are still too long, then simply remove the code | ||
if (mb_strlen($trackingCode) > self::MAX_TRACKING_CODE_LENGTH) { | ||
return new ShipmentTrackingInfoStruct($trackingCarrier, '', ''); | ||
} | ||
} | ||
|
||
|
||
$trackingUrl = trim(sprintf($trackingUrl, $trackingCode)); | ||
|
||
if (filter_var($trackingUrl, FILTER_VALIDATE_URL) === false) { | ||
$trackingUrl = ''; | ||
} | ||
|
||
/** | ||
* following characters are not allowed in the tracking URL {,},<,>,# | ||
*/ | ||
if (preg_match_all('/[{}<>#]/m', $trackingUrl)) { | ||
$trackingUrl = ''; | ||
} | ||
|
||
return new ShipmentTrackingInfoStruct($trackingCarrier, $trackingCode, $trackingUrl); | ||
} | ||
} |
Oops, something went wrong.