| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class PacketSubmitter |
| 4 |
* |
| 5 |
* @package Packetery\Module\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Api\InvalidRequestException; |
| 13 |
use Packetery\Core\Api\Soap; |
| 14 |
use Packetery\Core\Entity; |
| 15 |
use Packetery\Core\Log; |
| 16 |
use Packetery\Core\Rounder; |
| 17 |
use Packetery\Core\Validator; |
| 18 |
use Packetery\Core\Api\Soap\CreatePacketMapper; |
| 19 |
use Packetery\Module\Carrier\Options; |
| 20 |
use Packetery\Module\MessageManager; |
| 21 |
use Packetery\Module\ShippingMethod; |
| 22 |
use PacketeryNette\Http\Request; |
| 23 |
use WC_Order; |
| 24 |
use Packetery\Module; |
| 25 |
|
| 26 |
/** |
| 27 |
* Class PacketSubmitter |
| 28 |
* |
| 29 |
* @package Packetery\Module\Order |
| 30 |
*/ |
| 31 |
class PacketSubmitter { |
| 32 |
|
| 33 |
/** |
| 34 |
* SOAP API Client. |
| 35 |
* |
| 36 |
* @var Soap\Client SOAP API Client. |
| 37 |
*/ |
| 38 |
private $soapApiClient; |
| 39 |
|
| 40 |
/** |
| 41 |
* Order validator. |
| 42 |
* |
| 43 |
* @var Validator\Order |
| 44 |
*/ |
| 45 |
private $orderValidator; |
| 46 |
|
| 47 |
/** |
| 48 |
* ILogger. |
| 49 |
* |
| 50 |
* @var Log\ILogger |
| 51 |
*/ |
| 52 |
private $logger; |
| 53 |
|
| 54 |
/** |
| 55 |
* Order repository. |
| 56 |
* |
| 57 |
* @var Repository |
| 58 |
*/ |
| 59 |
private $orderRepository; |
| 60 |
|
| 61 |
/** |
| 62 |
* CreatePacketMapper. |
| 63 |
* |
| 64 |
* @var CreatePacketMapper |
| 65 |
*/ |
| 66 |
private $createPacketMapper; |
| 67 |
|
| 68 |
/** |
| 69 |
* Request. |
| 70 |
* |
| 71 |
* @var Request |
| 72 |
*/ |
| 73 |
private $request; |
| 74 |
|
| 75 |
/** |
| 76 |
* Message manager. |
| 77 |
* |
| 78 |
* @var MessageManager |
| 79 |
*/ |
| 80 |
private $messageManager; |
| 81 |
|
| 82 |
/** |
| 83 |
* Log page. |
| 84 |
* |
| 85 |
* @var Module\Log\Page |
| 86 |
*/ |
| 87 |
private $logPage; |
| 88 |
|
| 89 |
/** |
| 90 |
* Common logic. |
| 91 |
* |
| 92 |
* @var PacketActionsCommonLogic |
| 93 |
*/ |
| 94 |
private $commonLogic; |
| 95 |
|
| 96 |
/** |
| 97 |
* Options provider. |
| 98 |
* |
| 99 |
* @var Module\Options\Provider |
| 100 |
*/ |
| 101 |
private $optionsProvider; |
| 102 |
|
| 103 |
/** |
| 104 |
* OrderApi constructor. |
| 105 |
* |
| 106 |
* @param Soap\Client $soapApiClient SOAP API Client. |
| 107 |
* @param Validator\Order $orderValidator Order validator. |
| 108 |
* @param Log\ILogger $logger Logger. |
| 109 |
* @param Repository $orderRepository Order repository. |
| 110 |
* @param CreatePacketMapper $createPacketMapper CreatePacketMapper. |
| 111 |
* @param Request $request Request. |
| 112 |
* @param MessageManager $messageManager Message manager. |
| 113 |
* @param Module\Log\Page $logPage Log page. |
| 114 |
* @param PacketActionsCommonLogic $commonLogic Common logic. |
| 115 |
* @param Module\Options\Provider $optionsProvider Options provider. |
| 116 |
*/ |
| 117 |
public function __construct( |
| 118 |
Soap\Client $soapApiClient, |
| 119 |
Validator\Order $orderValidator, |
| 120 |
Log\ILogger $logger, |
| 121 |
Repository $orderRepository, |
| 122 |
CreatePacketMapper $createPacketMapper, |
| 123 |
Request $request, |
| 124 |
MessageManager $messageManager, |
| 125 |
Module\Log\Page $logPage, |
| 126 |
PacketActionsCommonLogic $commonLogic, |
| 127 |
Module\Options\Provider $optionsProvider |
| 128 |
) { |
| 129 |
$this->soapApiClient = $soapApiClient; |
| 130 |
$this->orderValidator = $orderValidator; |
| 131 |
$this->logger = $logger; |
| 132 |
$this->orderRepository = $orderRepository; |
| 133 |
$this->createPacketMapper = $createPacketMapper; |
| 134 |
$this->request = $request; |
| 135 |
$this->messageManager = $messageManager; |
| 136 |
$this->logPage = $logPage; |
| 137 |
$this->commonLogic = $commonLogic; |
| 138 |
$this->optionsProvider = $optionsProvider; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Process action |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public function processAction(): void { |
| 147 |
$order = $this->commonLogic->getOrder(); |
| 148 |
$redirectTo = $this->request->getQuery( PacketActionsCommonLogic::PARAM_REDIRECT_TO ); |
| 149 |
|
| 150 |
if ( null === $order ) { |
| 151 |
$record = new Log\Record(); |
| 152 |
$record->action = Log\Record::ACTION_PACKET_SENDING; |
| 153 |
$record->status = Log\Record::STATUS_ERROR; |
| 154 |
$record->orderId = null; |
| 155 |
$record->title = __( 'Packet submission error', 'packeta' ); |
| 156 |
$record->params = [ |
| 157 |
'referer' => (string) $this->request->getReferer(), |
| 158 |
'errorMessage' => 'Order not found', |
| 159 |
]; |
| 160 |
|
| 161 |
$this->logger->add( $record ); |
| 162 |
|
| 163 |
$this->messageManager->flash_message( __( 'Order not found', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 164 |
$this->commonLogic->redirectTo( $redirectTo, $order ); |
| 165 |
|
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
$this->commonLogic->checkAction( PacketActionsCommonLogic::ACTION_SUBMIT_PACKET, $order ); |
| 170 |
|
| 171 |
$submissionResult = $this->submitPacket( |
| 172 |
$this->orderRepository->getWcOrderById( (int) $order->getNumber() ), |
| 173 |
$this->optionsProvider->isOrderStatusAutoChangeEnabled(), |
| 174 |
$order |
| 175 |
); |
| 176 |
$resultsCounter = $submissionResult->getCounter(); |
| 177 |
$submissionResultMessages = $this->getTranslatedSubmissionMessages( $resultsCounter, (int) $order->getNumber() ); |
| 178 |
|
| 179 |
if ( $resultsCounter['success'] > 0 ) { |
| 180 |
$this->messageManager->flashMessageObject( |
| 181 |
Module\Message::create() |
| 182 |
->setText( $submissionResultMessages['success'] ) |
| 183 |
->setEscape( false ) |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
if ( $resultsCounter['ignored'] > 0 ) { |
| 188 |
$this->messageManager->flashMessageObject( |
| 189 |
Module\Message::create() |
| 190 |
->setText( $submissionResultMessages['ignored'] ) |
| 191 |
->setEscape( false ) |
| 192 |
->setType( MessageManager::TYPE_INFO ) |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
if ( $resultsCounter['errors'] > 0 ) { |
| 197 |
$this->messageManager->flashMessageObject( |
| 198 |
Module\Message::create() |
| 199 |
->setText( $submissionResultMessages['errors'] ) |
| 200 |
->setEscape( false ) |
| 201 |
->setType( MessageManager::TYPE_ERROR ) |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
$redirectTo = $this->request->getQuery( PacketActionsCommonLogic::PARAM_REDIRECT_TO ); |
| 206 |
$this->commonLogic->redirectTo( $redirectTo, $order ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Submits packet data to Packeta API. |
| 211 |
* |
| 212 |
* @param WC_Order $wcOrder WC order. |
| 213 |
* @param bool $updateOrderStatus Updates WC order status if packet was successfully created. Value is based on plugin settings. |
| 214 |
* @param Entity\Order|null $order Order. |
| 215 |
* |
| 216 |
* @return PacketSubmissionResult |
| 217 |
*/ |
| 218 |
public function submitPacket( |
| 219 |
WC_Order $wcOrder, |
| 220 |
bool $updateOrderStatus, |
| 221 |
?Entity\Order $order = null |
| 222 |
): PacketSubmissionResult { |
| 223 |
$submissionResult = new PacketSubmissionResult(); |
| 224 |
if ( null === $order ) { |
| 225 |
$order = $this->orderRepository->getByWcOrder( $wcOrder ); |
| 226 |
} |
| 227 |
if ( null === $order ) { |
| 228 |
$submissionResult->increaseIgnoredCount(); |
| 229 |
|
| 230 |
return $submissionResult; |
| 231 |
} |
| 232 |
|
| 233 |
$orderData = $wcOrder->get_data(); |
| 234 |
$shippingMethods = $wcOrder->get_shipping_methods(); |
| 235 |
$shippingMethod = reset( $shippingMethods ); |
| 236 |
|
| 237 |
$shippingMethodData = $shippingMethod->get_data(); |
| 238 |
$shippingMethodId = $shippingMethodData['method_id']; |
| 239 |
if ( ShippingMethod::PACKETERY_METHOD_ID === $shippingMethodId && ! $order->isExported() ) { |
| 240 |
try { |
| 241 |
$createPacketData = $this->preparePacketData( $order ); |
| 242 |
} catch ( InvalidRequestException $e ) { |
| 243 |
$record = new Log\Record(); |
| 244 |
$record->action = Log\Record::ACTION_PACKET_SENDING; |
| 245 |
$record->status = Log\Record::STATUS_ERROR; |
| 246 |
$record->title = __( 'Packet could not be created.', 'packeta' ); |
| 247 |
$record->params = [ |
| 248 |
'orderId' => $orderData['id'], |
| 249 |
'errorMessage' => $e->getMessage(), |
| 250 |
]; |
| 251 |
$record->orderId = $order->getNumber(); |
| 252 |
$this->logger->add( $record ); |
| 253 |
|
| 254 |
$submissionResult->increaseLogsCount(); |
| 255 |
$submissionResult->increaseErrorsCount(); |
| 256 |
|
| 257 |
return $submissionResult; |
| 258 |
} |
| 259 |
|
| 260 |
$response = $this->soapApiClient->createPacket( $createPacketData ); |
| 261 |
if ( $response->hasFault() ) { |
| 262 |
$record = new Log\Record(); |
| 263 |
$record->action = Log\Record::ACTION_PACKET_SENDING; |
| 264 |
$record->status = Log\Record::STATUS_ERROR; |
| 265 |
$record->title = __( 'Packet could not be created.', 'packeta' ); |
| 266 |
$record->params = [ |
| 267 |
'request' => $createPacketData, |
| 268 |
'errorMessage' => $response->getErrorsAsString(), |
| 269 |
]; |
| 270 |
$record->orderId = $order->getNumber(); |
| 271 |
$errorMessage = $response->getErrorsAsString( false ); |
| 272 |
|
| 273 |
$submissionResult->increaseErrorsCount(); |
| 274 |
} else { |
| 275 |
$order->setIsExported( true ); |
| 276 |
$order->setPacketId( (string) $response->getId() ); |
| 277 |
|
| 278 |
$record = new Log\Record(); |
| 279 |
$record->action = Log\Record::ACTION_PACKET_SENDING; |
| 280 |
$record->status = Log\Record::STATUS_SUCCESS; |
| 281 |
$record->title = __( 'Packet was successfully created.', 'packeta' ); |
| 282 |
$record->params = [ |
| 283 |
'request' => $createPacketData, |
| 284 |
'packetId' => $response->getId(), |
| 285 |
]; |
| 286 |
$record->orderId = $order->getNumber(); |
| 287 |
$errorMessage = null; |
| 288 |
|
| 289 |
$submissionResult->increaseSuccessCount(); |
| 290 |
} |
| 291 |
|
| 292 |
$submissionResult->increaseLogsCount(); |
| 293 |
$this->logger->add( $record ); |
| 294 |
$order->updateApiErrorMessage( $errorMessage ); |
| 295 |
$this->orderRepository->save( $order ); |
| 296 |
|
| 297 |
if ( $updateOrderStatus && false === $response->hasFault() ) { |
| 298 |
$this->updateOrderStatusOrLogError( $wcOrder, $order->getNumber(), $submissionResult ); |
| 299 |
} |
| 300 |
} else { |
| 301 |
$submissionResult->increaseIgnoredCount(); |
| 302 |
} |
| 303 |
|
| 304 |
return $submissionResult; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Prepares packet attributes. |
| 309 |
* |
| 310 |
* @param Entity\Order $order Order entity. |
| 311 |
* |
| 312 |
* @return array |
| 313 |
* @throws InvalidRequestException For the case request is not eligible to be sent to API. |
| 314 |
*/ |
| 315 |
private function preparePacketData( Entity\Order $order ): array { |
| 316 |
if ( ! $this->orderValidator->validate( $order ) ) { |
| 317 |
throw new InvalidRequestException( 'All required order attributes are not set.' ); |
| 318 |
} |
| 319 |
|
| 320 |
$createPacketData = $this->createPacketMapper->fromOrderToArray( $order ); |
| 321 |
if ( ! empty( $createPacketData['cod'] ) ) { |
| 322 |
$roundingType = Options::createByCarrierId( $order->getCarrierCode() )->getCodRoundingType(); |
| 323 |
$roundedCod = Rounder::roundByCurrency( $createPacketData['cod'], $createPacketData['currency'], $roundingType ); |
| 324 |
$createPacketData['cod'] = $roundedCod; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Allows to update CreatePacket request data. |
| 329 |
* |
| 330 |
* @since 1.4 |
| 331 |
* |
| 332 |
* @param array $createPacketData CreatePacket request data. |
| 333 |
*/ |
| 334 |
return (array) apply_filters( 'packeta_create_packet', $createPacketData ); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Gets translated messages by submission result. |
| 339 |
* |
| 340 |
* @param array $submissionResult Submission result. |
| 341 |
* @param int|null $orderId Order ID. |
| 342 |
* |
| 343 |
* @return array |
| 344 |
*/ |
| 345 |
public function getTranslatedSubmissionMessages( array $submissionResult, ?int $orderId ): array { |
| 346 |
$success = null; |
| 347 |
if ( is_numeric( $submissionResult['success'] ) && $submissionResult['success'] > 0 ) { |
| 348 |
if ( $submissionResult['logs'] > 0 ) { |
| 349 |
$success = sprintf( // translators: 1: link start 2: link end. |
| 350 |
esc_html__( 'Shipments were submitted successfully. %1$sShow logs%2$s', 'packeta' ), |
| 351 |
'<a href="' . $this->logPage->createLogListUrl( $orderId ) . '">', |
| 352 |
'</a>' |
| 353 |
); |
| 354 |
} else { |
| 355 |
$success = esc_html__( 'Shipments were submitted successfully.', 'packeta' ); |
| 356 |
} |
| 357 |
} |
| 358 |
$ignored = null; |
| 359 |
if ( is_numeric( $submissionResult['ignored'] ) && $submissionResult['ignored'] > 0 ) { |
| 360 |
if ( $submissionResult['logs'] > 0 ) { |
| 361 |
$ignored = sprintf( // translators: 1: total number of shipments 2: link start 3: link end. |
| 362 |
esc_html__( 'Some shipments (%1$s in total) were not submitted (these were submitted already or are not Packeta orders). %2$sShow logs%3$s', 'packeta' ), |
| 363 |
$submissionResult['ignored'], |
| 364 |
'<a href="' . $this->logPage->createLogListUrl( $orderId ) . '">', |
| 365 |
'</a>' |
| 366 |
); |
| 367 |
} else { |
| 368 |
$ignored = sprintf( // translators: %s is count. |
| 369 |
esc_html__( 'Some shipments (%s in total) were not submitted (these were submitted already or are not Packeta orders).', 'packeta' ), |
| 370 |
$submissionResult['ignored'] |
| 371 |
); |
| 372 |
} |
| 373 |
} |
| 374 |
$errors = null; |
| 375 |
if ( is_numeric( $submissionResult['errors'] ) && $submissionResult['errors'] > 0 ) { |
| 376 |
if ( $submissionResult['logs'] > 0 ) { |
| 377 |
$errors = sprintf( // translators: 1: total number of shipments 2: link start 3: link end. |
| 378 |
esc_html__( 'Some shipments (%1$s in total) failed to be submitted to Packeta. %2$sShow logs%3$s', 'packeta' ), |
| 379 |
$submissionResult['errors'], |
| 380 |
'<a href="' . $this->logPage->createLogListUrl( $orderId ) . '">', |
| 381 |
'</a>' |
| 382 |
); |
| 383 |
} else { |
| 384 |
$errors = sprintf( // translators: %s is count. |
| 385 |
esc_html__( 'Some shipments (%s in total) failed to be submitted to Packeta.', 'packeta' ), |
| 386 |
$submissionResult['errors'] |
| 387 |
); |
| 388 |
} |
| 389 |
} elseif ( isset( $submissionResult['errors'] ) ) { |
| 390 |
$errors = esc_html( $submissionResult['errors'] ); |
| 391 |
} |
| 392 |
|
| 393 |
if ( is_numeric( $submissionResult['statusUnchanged'] ) && $submissionResult['statusUnchanged'] > 0 ) { |
| 394 |
$errors = esc_html__( 'Some order statuses have not been automatically changed.', 'packeta' ); |
| 395 |
} |
| 396 |
|
| 397 |
return [ |
| 398 |
'success' => $success, |
| 399 |
'ignored' => $ignored, |
| 400 |
'errors' => $errors, |
| 401 |
]; |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Updates order status or logs error. |
| 406 |
* |
| 407 |
* @param WC_Order $order WC Order. |
| 408 |
* @param string $orderId Order ID. |
| 409 |
* @param PacketSubmissionResult $submissionResult Packet submission result. |
| 410 |
* |
| 411 |
* @return void |
| 412 |
*/ |
| 413 |
private function updateOrderStatusOrLogError( WC_Order $order, string $orderId, PacketSubmissionResult $submissionResult ):void { |
| 414 |
$autoOrderStatus = $this->optionsProvider->getValidAutoOrderStatus(); |
| 415 |
if ( '' === $autoOrderStatus ) { |
| 416 |
$record = new Log\Record(); |
| 417 |
$record->action = Log\Record::ACTION_PACKET_SENDING; |
| 418 |
$record->status = Log\Record::STATUS_ERROR; |
| 419 |
|
| 420 |
$record->title = sprintf( |
| 421 |
// translators: %s represents unknown order status. |
| 422 |
__( 'Order status has not been changed, status "%s" doesn\'t exist.', 'packeta' ), |
| 423 |
$this->optionsProvider->getAutoOrderStatus() |
| 424 |
); |
| 425 |
$record->orderId = $orderId; |
| 426 |
$this->logger->add( $record ); |
| 427 |
$submissionResult->increaseStatusUnchangedCount(); |
| 428 |
} else { |
| 429 |
$order->update_status( $autoOrderStatus ); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
} |
| 434 |
|