custom/plugins/MolliePayments/src/Subscriber/CustomerRegistrationSubscriber.php line 76

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Kiener\MolliePayments\Service\CustomerService;
  4. use Kiener\MolliePayments\Service\LoggerService;
  5. use Kiener\MolliePayments\Service\SettingsService;
  6. use Mollie\Api\Exceptions\ApiException;
  7. use Mollie\Api\MollieApiClient;
  8. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CustomerRegistrationSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var MollieApiClient */
  14.     private $apiClient;
  15.     /** @var CustomerService */
  16.     private $customerService;
  17.     /** @var SettingsService */
  18.     private $settingsService;
  19.     /** @var LoggerService */
  20.     private $loggerService;
  21.     /**
  22.      * Returns an array of event names this subscriber wants to listen to.
  23.      *
  24.      * The array keys are event names and the value can be:
  25.      *
  26.      * * The method name to call (priority defaults to 0)
  27.      * * An array composed of the method name to call and the priority
  28.      * * An array of arrays composed of the method names to call and respective
  29.      *   priorities, or 0 if unset
  30.      *
  31.      * For instance:
  32.      *
  33.      * * array('eventName' => 'methodName')
  34.      * * array('eventName' => array('methodName', $priority))
  35.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  36.      *
  37.      * @return array The event names to listen to
  38.      */
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             'customer.written' => 'onCustomerRegistration',
  43.         ];
  44.     }
  45.     /**
  46.      * Creates a new instance of PaymentMethodSubscriber.
  47.      *
  48.      * @param MollieApiClient $apiClient
  49.      */
  50.     public function __construct(
  51.         MollieApiClient $apiClient,
  52.         CustomerService $customerService,
  53.         SettingsService $settingsService,
  54.         LoggerService $loggerService
  55.     ) {
  56.         $this->apiClient $apiClient;
  57.         $this->customerService $customerService;
  58.         $this->settingsService $settingsService;
  59.         $this->loggerService $loggerService;
  60.     }
  61.     /**
  62.      * Refunds the transaction at Mollie if the payment state is refunded.
  63.      *
  64.      * @param CustomerRegisterEvent $customerRegisterEvent
  65.      */
  66.     public function onCustomerRegistration(EntityWrittenEvent $entityWrittenEvent): void
  67.     {
  68.         $context $entityWrittenEvent->getContext();
  69.         if ($context === null) {
  70.             return;
  71.         }
  72.         $source $context->getSource();
  73.         if ($source === null | !method_exists($source'getSalesChannelId')) {
  74.             return;
  75.         }
  76.         $settings $this->settingsService->getSettings($source->getSalesChannelId());
  77.         if ($settings->createNoCustomersAtMollie()) {
  78.             return;
  79.         }
  80.         foreach ($entityWrittenEvent->getPayloads() as $payload) {
  81.             if (
  82.                 \array_key_exists('customFields'$payload) &&
  83.                 \array_key_exists(CustomerService::CUSTOM_FIELDS_KEY_MOLLIE_CUSTOMER_ID$payload['customFields'])
  84.             ) {
  85.                 // Escape if the payload already writes the mollie Id to the custom Fields
  86.                 continue;
  87.             }
  88.             if (!isset($payload['firstName'], $payload['lastName'], $payload['email'], $payload['id'], $payload['guest'])) {
  89.                 // Escape if we do not have the information mollie wants
  90.                 continue;
  91.             }
  92.             $id $payload['id'];
  93.             $name = \sprintf('%s %s'$payload['firstName'], $payload['lastName']);
  94.             $email $payload['email'];
  95.             try {
  96.                 $mollieCustomer $this->apiClient->customers->create(
  97.                     [
  98.                         'name'  => $name,
  99.                         'email' => $email,
  100.                     ]
  101.                 );
  102.                 $this->customerService->saveCustomerCustomFields(
  103.                     $id,
  104.                     ['customer_id' => $mollieCustomer->id],
  105.                     $entityWrittenEvent->getContext()
  106.                 );
  107.             } catch (ApiException $e) {
  108.                 $this->loggerService->addEntry('Customer could not be registered.'$context$e);
  109.             }
  110.         }
  111.     }
  112. }