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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MyPa\Shopware\Subscriber;
  4. use MyPa\Shopware\Core\Content\ShippingOption\ShippingOptionEntity;
  5. use MyPa\Shopware\Defaults;
  6. use MyPa\Shopware\Service\Order\OrderService;
  7. use MyPa\Shopware\Service\ShippingOptions\ShippingOptionsService;
  8. use MyPa\Shopware\Service\WebhookBuilder\WebhookBuilder;
  9. use MyParcelNL\Sdk\src\Exception\AccountNotActiveException;
  10. use MyParcelNL\Sdk\src\Exception\ApiException;
  11. use MyParcelNL\Sdk\src\Exception\MissingFieldException;
  12. use MyParcelNL\Sdk\src\Services\Web\Webhook\ShipmentStatusChangeWebhookWebService;
  13. use Psr\Log\LoggerInterface;
  14. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class OrderPlacedSubscriber implements EventSubscriberInterface
  18. {
  19.     private const PARAM_MY_PARCEL          'my_parcel';
  20.     private const PARAM_DELIVERY_DATE      'delivery_date';
  21.     private const PARAM_DELIVERY_TYPE      'delivery_type';
  22.     private const PARAM_PACKAGE_TYPE       'package_type';
  23.     private const PARAM_REQUIRES_AGE_CHECK 'requires_age_check';
  24.     private const PARAM_REQUIRES_SIGNATURE 'requires_signature';
  25.     private const PARAM_ONLY_RECIPIENT     'only_recipient';
  26.     private const PARAM_RETURN_IF_NOT_HOME 'return_if_not_home';
  27.     private const PARAM_LARGE_FORMAT       'large_format';
  28.     private const PARAM_SHIPPING_METHOD_ID 'shipping_method_id';
  29.     private const PARAM_DELIVERY_LOCATION  'delivery_location';
  30.     private const PARAM_PICKUP_DATA        'pickup_point_data';
  31.     /**
  32.      * @var LoggerInterface
  33.      */
  34.     protected $logger;
  35.     /**
  36.      * @var OrderService
  37.      */
  38.     private $orderService;
  39.     /**
  40.      * @var ShippingOptionsService
  41.      */
  42.     private $shippingOptionsService;
  43.     /**
  44.      * @var SystemConfigService
  45.      */
  46.     private $configService;
  47.     /**
  48.      * @var ShipmentStatusChangeWebhookWebService
  49.      */
  50.     private $shipmentStatusChangeWebhookWebService;
  51.     /**
  52.      * @var WebhookBuilder
  53.      */
  54.     private $builder;
  55.     /**
  56.      * Creates a new instance of the order placed subscriber.
  57.      *
  58.      * @param OrderService                          $orderService
  59.      * @param ShippingOptionsService                $shippingOptionService
  60.      * @param SystemConfigService                   $configService
  61.      * @param LoggerInterface                       $logger
  62.      * @param ShipmentStatusChangeWebhookWebService $shipmentStatusChangeWebhookWebService
  63.      * @param WebhookBuilder                        $builder
  64.      */
  65.     public function __construct(
  66.         OrderService                          $orderService,
  67.         ShippingOptionsService                $shippingOptionService,
  68.         SystemConfigService                   $configService,
  69.         LoggerInterface                       $logger,
  70.         ShipmentStatusChangeWebhookWebService $shipmentStatusChangeWebhookWebService,
  71.         WebhookBuilder                        $builder
  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.         } catch (AccountNotActiveException MissingFieldException ApiException $e) {
  130.             $this->logger->error('Error subscribing to webhook', ['error' => $e]);
  131.         }
  132.     }
  133. }