Components/Sepa/Plentymarkets/HandleSepaPaymentCommandHandler.php
public function handle(CommandInterface $command)
    {
        /**
         * @var Payment $payment
         */
        $payment = $command->getPayload();

        /**
         * @var SepaPaymentData $data
         */
        $data = $payment->getPaymentData();

        if (!($data instanceof SepaPaymentData)) {
            return $this->parentCommandHandler->handle($command);
        }

        $identity = $this->identityService->findOneBy([
            'objectIdentifier' => $payment->getIdentifier(),
            'objectType' => Payment::TYPE,
            'adapterName' => PlentymarketsAdapter::NAME,
        ]);

        if (null !== $identity) {
            return true;
        }

        $orderIdentity = $this->identityService->findOneBy([
            'objectIdentifier' => $payment->getOrderIdentifer(),
            'objectType' => Order::TYPE,
            'adapterName' => PlentymarketsAdapter::NAME,
        ]);

        if (null === $orderIdentity) {
            throw new NotFoundException('could not find order for bank account handling - ' . $payment->getOrderIdentifer());
        }

        $contactId = $this->getContactIdentifier($orderIdentity);

        if (null === $contactId) {
            throw new NotFoundException('could not find contact for bank account handling - ' . $payment->getOrderIdentifer());
        }

        $bankAccounts = $this->client->request('GET', 'accounts/contacts/' . $contactId . '/banks');

        $possibleBankAccounts = array_filter($bankAccounts, function (array $bankAccount) use ($data, $orderIdentity) {
            return $bankAccount['iban'] === $data->getIban() && $bankAccount['orderId'] === (int) $orderIdentity->getAdapterIdentifier();
        });

        if (!empty($possibleBankAccounts)) {
            $possibleBankAccounts = array_shift($possibleBankAccounts);

            $this->identityService->insert(
                $payment->getIdentifier(),
                Payment::TYPE,
                (string) $possibleBankAccounts['id'],
                PlentymarketsAdapter::NAME
            );

            return true;
        }

        $sepaPaymentDataParams = [
            'lastUpdateBy' => 'import',
            'accountOwner' => $data->getAccountOwner(),
            'iban' => $data->getIban(),
            'bic' => $data->getBic(),
            'orderId' => $orderIdentity->getAdapterIdentifier(),
            'contactId' => $contactId,
        ];

        $paymentResult = $this->client->request('POST', 'accounts/contacts/banks', $sepaPaymentDataParams);

        $this->identityService->insert(
            $payment->getIdentifier(),
            Payment::TYPE,
            (string) $paymentResult['id'],
            PlentymarketsAdapter::NAME
        );

        return true;
    }
public function handle(CommandInterface $command)
    {
        /**
         * @var Payment $payment
         */
        $payment = $command->getPayload();

        /**
         * @var SepaPaymentData $data
         */
        $data = $payment->getPaymentData();

        if (!($data instanceof SepaPaymentData)) {
            return $this->parentCommandHandler->handle($command);
        }

        $identity = $this->identityService->findOneBy([
            'objectIdentifier' => $payment->getIdentifier(),
            'objectType' => Payment::TYPE,
            'adapterName' => PlentymarketsAdapter::NAME,
        ]);

        if (null !== $identity) {
            return true;
        }

        $orderIdentity = $this->identityService->findOneBy([
            'objectIdentifier' => $payment->getOrderIdentifer(),
            'objectType' => Order::TYPE,
            'adapterName' => PlentymarketsAdapter::NAME,
        ]);

        if (null === $orderIdentity) {
            throw new NotFoundException('could not find order for bank account handling - ' . $payment->getOrderIdentifer());
        }

        $contactId = $this->getContactIdentifier($orderIdentity);

        if (null === $contactId) {
            throw new NotFoundException('could not find contact for bank account handling - ' . $payment->getOrderIdentifer());
        }

        $bankAccounts = $this->client->request('GET', 'accounts/contacts/' . $contactId . '/banks');

        $possibleBankAccounts = array_filter($bankAccounts, function (array $bankAccount) use ($data, $orderIdentity) {
            return $bankAccount['iban'] === $data->getIban() && $bankAccount['orderId'] === (int) $orderIdentity->getAdapterIdentifier();
        });

        if (!empty($possibleBankAccounts)) {
            $possibleBankAccounts = array_shift($possibleBankAccounts);

            $this->identityService->insert(
                $payment->getIdentifier(),
                Payment::TYPE,
                (string) $possibleBankAccounts['id'],
                PlentymarketsAdapter::NAME
            );

            return true;
        }

        $sepaPaymentDataParams = [
            'lastUpdateBy' => 'import',
            'accountOwner' => $data->getAccountOwner(),
            'iban' => $data->getIban(),
            'bic' => $data->getBic(),
            'orderId' => $orderIdentity->getAdapterIdentifier(),
            'contactId' => $contactId,
        ];

        $paymentResult = $this->client->request('POST', 'accounts/contacts/banks', $sepaPaymentDataParams);

        $this->identityService->insert(
            $payment->getIdentifier(),
            Payment::TYPE,
            (string) $paymentResult['id'],
            PlentymarketsAdapter::NAME
        );

        return true;
    }
{@inheritdoc}

@var TransferObjectCommand $command