custom/plugins/MolliePayments/src/Subscriber/PaymentMethodSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Mollie\Api\Exceptions\ApiException;
  4. use Mollie\Api\MollieApiClient;
  5. use Mollie\Api\Resources\Profile;
  6. use Shopware\Core\Checkout\Payment\PaymentEvents;
  7. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class PaymentMethodSubscriber implements EventSubscriberInterface
  16. {
  17.     /** @var MollieApiClient $apiClient */
  18.     private $apiClient;
  19.     /** @var EntityRepositoryInterface $paymentRepository */
  20.     private $paymentRepository;
  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.             PaymentEvents::PAYMENT_METHOD_WRITTEN_EVENT => 'onPaymentMethodChanged'
  43.         ];
  44.     }
  45.     /**
  46.      * Creates a new instance of PaymentMethodSubscriber.
  47.      *
  48.      * @param MollieApiClient $apiClient
  49.      * @param EntityRepositoryInterface $paymentRepository
  50.      */
  51.     public function __construct(
  52.         MollieApiClient $apiClient,
  53.         EntityRepositoryInterface $paymentRepository
  54.     )
  55.     {
  56.         $this->apiClient $apiClient;
  57.         $this->paymentRepository $paymentRepository;
  58.     }
  59.     /**
  60.      * Activates a payment method at Mollie.
  61.      *
  62.      * @param EntityWrittenEvent $args
  63.      * @throws ApiException
  64.      */
  65.     public function onPaymentMethodChanged(EntityWrittenEvent $args)
  66.     {
  67.         foreach ($args->getPayloads() as $payload) {
  68.             $id null;
  69.             $active null;
  70.             // Get the payment method ID
  71.             if (isset($payload['id'])) {
  72.                 $id $payload['id'];
  73.             }
  74.             // Get whether the payment method is active
  75.             if (isset($payload['active'])) {
  76.                 $active $payload['active'];
  77.             }
  78.             // Activate payment method at Mollie
  79.             if ($id !== null && (bool) $active === true) {
  80.                 $paymentMethod null;
  81.                 try {
  82.                     /** @var PaymentMethodEntity $paymentMethod */
  83.                     $paymentMethod $this->getPaymentMethodById($id);
  84.                 } catch (InconsistentCriteriaIdsException $e) {
  85.                     //
  86.                 }
  87.                 if ($paymentMethod !== null &&
  88.                     strpos($paymentMethod->getHandlerIdentifier(), 'MolliePayments') !== false) {
  89.                     // Get custom fields of payment method
  90.                     $customFields $paymentMethod->getCustomFields();
  91.                     // Check if Mollie's payment method name is set
  92.                     if (!isset($customFields['mollie_payment_method_name'])) {
  93.                         continue;
  94.                     }
  95.                     $profile null;
  96.                     try {
  97.                         /** @var Profile $profile */
  98.                         $profile $this->apiClient->profiles->get('me');
  99.                     } catch (ApiException $e) {
  100.                         //
  101.                     }
  102.                     if ($profile !== null) {
  103.                         $profile->enableMethod($customFields['mollie_payment_method_name']);
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.     }
  109.     /**
  110.      * Get payment method by ID.
  111.      *
  112.      * @param $id
  113.      * @return PaymentMethodEntity
  114.      * @throws InconsistentCriteriaIdsException
  115.      */
  116.     private function getPaymentMethodById($id) : ?PaymentMethodEntity
  117.     {
  118.         // Fetch ID for update
  119.         $paymentCriteria = new Criteria();
  120.         $paymentCriteria->addFilter(new EqualsFilter('id'$id));
  121.         // Get payment methods
  122.         $paymentMethods $this->paymentRepository->search($paymentCriteriaContext::createDefaultContext());
  123.         if ($paymentMethods->getTotal() === 0) {
  124.             return null;
  125.         }
  126.         return $paymentMethods->first();
  127.     }
  128. }