custom/plugins/MyPaShopware/src/Subscriber/OrderPlacedSubscriber.php line 103

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MyPa\Shopware\Subscriber;
  3. use MyPa\Shopware\Core\Content\ShippingOption\ShippingOptionEntity;
  4. use MyPa\Shopware\Defaults;
  5. use MyPa\Shopware\Service\Order\OrderService;
  6. use MyPa\Shopware\Service\ShippingOptions\ShippingOptionsService;
  7. use MyPa\Shopware\Service\WebhookBuilder\WebhookBuilder;
  8. use MyParcelNL\Sdk\src\Exception\AccountNotActiveException;
  9. use MyParcelNL\Sdk\src\Exception\ApiException;
  10. use MyParcelNL\Sdk\src\Exception\MissingFieldException;
  11. use MyParcelNL\Sdk\src\Services\Web\Webhook\ShipmentStatusChangeWebhookWebService;
  12. use Psr\Log\LoggerInterface;
  13. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class OrderPlacedSubscriber implements EventSubscriberInterface
  17. {
  18.     private const PARAM_MY_PARCEL          'my_parcel';
  19.     private const PARAM_DELIVERY_DATE      'delivery_date';
  20.     private const PARAM_DELIVERY_TYPE      'delivery_type';
  21.     private const PARAM_PACKAGE_TYPE       'package_type';
  22.     private const PARAM_REQUIRES_AGE_CHECK 'requires_age_check';
  23.     private const PARAM_REQUIRES_SIGNATURE 'requires_signature';
  24.     private const PARAM_ONLY_RECIPIENT     'only_recipient';
  25.     private const PARAM_RETURN_IF_NOT_HOME 'return_if_not_home';
  26.     private const PARAM_LARGE_FORMAT       'large_format';
  27.     private const PARAM_SHIPPING_METHOD_ID 'shipping_method_id';
  28.     private const PARAM_DELIVERY_LOCATION  'delivery_location';
  29.     private const PARAM_PICKUP_DATA        'pickup_point_data';
  30.     /**
  31.      * @var LoggerInterface
  32.      */
  33.     protected $logger;
  34.     /**
  35.      * @var OrderService
  36.      */
  37.     private $orderService;
  38.     /**
  39.      * @var ShippingOptionsService
  40.      */
  41.     private $shippingOptionsService;
  42.     /**
  43.      * @var SystemConfigService
  44.      */
  45.     private $configService;
  46.     /**
  47.      * @var ShipmentStatusChangeWebhookWebService
  48.      */
  49.     private $shipmentStatusChangeWebhookWebService;
  50.     /**
  51.      * @var WebhookBuilder
  52.      */
  53.     private $builder;
  54.     /**
  55.      * Creates a new instance of the order placed subscriber.
  56.      *
  57.      * @param OrderService                          $orderService
  58.      * @param ShippingOptionsService                $shippingOptionService
  59.      * @param SystemConfigService                   $configService
  60.      * @param LoggerInterface                       $logger
  61.      * @param ShipmentStatusChangeWebhookWebService $shipmentStatusChangeWebhookWebService
  62.      * @param WebhookBuilder                        $builder
  63.      */
  64.     public function __construct(
  65.         OrderService                          $orderService,
  66.         ShippingOptionsService                $shippingOptionService,
  67.         SystemConfigService                   $configService,
  68.         LoggerInterface                       $logger,
  69.         ShipmentStatusChangeWebhookWebService $shipmentStatusChangeWebhookWebService,
  70.         WebhookBuilder                        $builder
  71.     )
  72.     {
  73.         $this->orderService $orderService;
  74.         $this->shippingOptionsService $shippingOptionService;
  75.         $this->configService $configService;
  76.         $this->logger $logger;
  77.         $this->shipmentStatusChangeWebhookWebService $shipmentStatusChangeWebhookWebService;
  78.         $this->builder $builder;
  79.     }
  80.     /**
  81.      * Returns an array of event names this subscriber wants to listen to.
  82.      *
  83.      * @return array
  84.      */
  85.     public static function getSubscribedEvents()
  86.     {
  87.         return [
  88.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  89.         ];
  90.     }
  91.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
  92.     {
  93.         //Get order
  94.         $order $event->getOrder();
  95.         //Get order custom fields with key
  96.         if (!empty($order->getCustomFields()[Defaults::MYPARCEL_DELIVERY_OPTIONS_KEY])) {
  97.             //Start the webhook subscriber for updates
  98.             $this->subscribeToWebhook($event->getSalesChannelId());
  99.             $myparcelOptions $order->getCustomFields()[Defaults::MYPARCEL_DELIVERY_OPTIONS_KEY];
  100.             // Add the order to the shipping options
  101.             $myparcelOptions[ShippingOptionEntity::FIELD_ORDER] = [
  102.                 'id'        => $order->getId(),
  103.                 'versionId' => $order->getVersionId(),
  104.             ];
  105.             //Store shipping options in the database
  106.             $this->shippingOptionsService->createOrUpdateShippingOptions($myparcelOptions$event->getContext());
  107.             //Update custom fields on the order
  108.             $this->orderService->createOrUpdateOrder([
  109.                 'id'           => $order->getId(),
  110.                 'versionId'    => $order->getVersionId(),
  111.                 'customFields' => [
  112.                     self::PARAM_MY_PARCEL => json_encode($myparcelOptions),
  113.                 ],
  114.             ], $event->getContext());
  115.         }
  116.     }
  117.     /**
  118.      * Subscribes to the webhook
  119.      */
  120.     public function subscribeToWebhook(string $salesChannelId)
  121.     {
  122.         $apiKey = (string)$this->configService->get('MyPaShopware.config.myParcelApiKey'$salesChannelId);
  123.         try {
  124.             $subID $this->shipmentStatusChangeWebhookWebService->setApiKey($apiKey)
  125.                 ->subscribe($this->builder->buildWebhook());
  126.             $this->logger->debug('Hooked to myparcel', [
  127.                 'Hook id' => $subID,
  128.             ]);
  129.         }
  130.         catch (AccountNotActiveException|MissingFieldException|ApiException $e) {
  131.             $this->logger->error('Error subscribing to webhook', ['error' => $e]);
  132.         }
  133.     }
  134. }