Skip to content
Open
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
35 changes: 21 additions & 14 deletions Gateway/Command/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ public function execute(array $commandSubject)
$this->getValidator()::ACTION_TYPE_CAPTURE
);

$hasTracking = !empty($requestData['tracking']);

// if tracking info is invalid, stop capture
if (!$this->isTrackingInfoValid($requestData['tracking'] ?? null)) {
if ($hasTracking && !$this->isTrackingInfoValid($requestData['tracking'])) {
return null;
}

Expand All @@ -122,7 +124,7 @@ public function execute(array $commandSubject)
$requestData = $requestData->toArray();
}

if ($this->isProcessingShipment($requestData, $response)) {
if ($hasTracking && $this->isProcessingShipment($requestData, $response)) {
$this->addShippingInfoToCapture(
$response->getCaptureId(),
$klarnaOrder->getReservationId(),
Expand Down Expand Up @@ -154,9 +156,20 @@ private function addShippingInfoToCapture($captureId, $klarnaOrderId, $trackingD
$invoice->addComment("Shipping info sent to Klarna API", false, false);
return;
}
foreach ($response->getErrorMessages() as $message) {
$invoice->addComment($message, false, false);

$errorMessages = $response->getErrorMessages();

if (!$errorMessages) {
return;
}

$errorMessages = \implode('. ', $errorMessages);

$invoice->addComment(
"Received error(s) when sending shipping info to Kustom API: {$errorMessages}.",
notify: false,
visibleOnFront: false
);
}

/**
Expand All @@ -166,13 +179,11 @@ private function addShippingInfoToCapture($captureId, $klarnaOrderId, $trackingD
* @param DataObject $response
* @return bool
*/
private function isProcessingShipment(array $requestData, DataObject
$response): bool
private function isProcessingShipment(array $requestData, DataObject $response): bool
{
if (isset($requestData['invoice']['do_shipment'])
&& $requestData['invoice']['do_shipment'] === "1"
&& $response->getCaptureId()
&& $this->isTrackingInfoValid($requestData['tracking'] ?? null)) {
&& $response->getCaptureId()) {
return true;
}
return false;
Expand All @@ -181,15 +192,11 @@ private function isProcessingShipment(array $requestData, DataObject
/**
* Validate tracking info
*
* @param array|null $trackingInformation
* @param array $trackingInformation
* @return bool
*/
private function isTrackingInfoValid(?array $trackingInformation): bool
private function isTrackingInfoValid(array $trackingInformation): bool
{
if ($trackingInformation === null) {
return true;
}

foreach ((array) $trackingInformation as $info) {
if (empty($info['carrier_code'])
|| empty($info['title'])
Expand Down
4 changes: 3 additions & 1 deletion Model/Api/OrderManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ private function prepareShippingInfo(array $shippingInfo)
/**
* Get Api Accepted shipping method,For merchant who implement this feature
* Create Plugin to overwrite this default method code
* Allowed values matches (PickUpStore|Home|BoxReg|BoxUnreg|PickUpPoint|Own)
* Allowed values matches (PickUpStore|Home|BoxReg|BoxUnreg|PickUpPoint|Own|Postal|DHLPackstation|Digital
* |Undefined|PickUpWarehouse|ClickCollect|PalletDelivery)
* Doc: https://docs.kustom.co/contents/api/order-management/orders/appendordershippinginfo
*
* @param array $shipping
* @return string
Expand Down
Loading