3

I am building an extension which is dependent on multiple third party payment gateways.

So I have created a special service class which gives the respective payment gateway class object depending on payment method code.

<?php

namespace Vendor\ModuleName\Model\Service;

use Magento\Framework\ObjectManagerInterface;

class PaymentService
{
    const MODEL_PATH = "Vendor\ModuleName\Model\Service\Payment\\";

    /**
     * @var ObjectManagerInterface
     */
    private $objectManager;

    private $classInfo = [
        'payment1' => 'payment_class_one',
        'payment2' => 'payment_class_two',
    ];

    /**
     * PaymentService constructor.
     * @param ObjectManagerInterface $objectManager
     */
    public function __construct(ObjectManagerInterface $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    /**
     * @param $order
     * @return mixed
     */
    public function get($order){
        $paymentMethod = $order->getPayment()->getMethod();
        if(isset($this->classInfo[$paymentMethod])){
            return $this->objectManager->create($this->getClassByCode($paymentMethod));
        }
    }

    public function getByPaymentCode($code){
        if(isset($this->classInfo[$code])){
            return $this->objectManager->create($this->getClassByCode($code));
        }
    }

    /**
     * @param $paymentMethod
     * @return string
     */
    private function getClassByCode($paymentMethod){
        return self::MODEL_PATH . $this->classInfo[$paymentMethod];
    }
}

payment_class_one

<?php
namespace Vendor\ModuleName\Model\Service\Payment;

use Magento\Framework\Encryption\EncryptorInterface;
use CustomModule\PaymentModule\Model\ResourceModel\Cards\CollectionFactory;

class PaymentClassOne
{
    private $order;
    /**
     * @var EncryptorInterface
     */
    private $encryptor;
    /**
     * @var CollectionFactory
     */
    private $cardCollectionFactory;

    private $data;

    public function __construct
    (
        EncryptorInterface $encryptor,
        CollectionFactory $cardCollectionFactory,
        $order,
        array $data = []
    )
    {
        $this->encryptor = $encryptor;
        $this->cardCollectionFactory = $cardCollectionFactory;
        $this->order = $order;
        $this->data = $data;
    }

    public function getPaymentToken(){
        $additionalInfo = $this->getPaymentInformation()->getAdditionalInformation();
        return $this->encryptor->encrypt($additionalInfo['token']);
    }

    public function getMethodCode(){
        return $this->getPaymentInformation()->getMethod();
    }

    public function getCardInfo(){
        $token = $this->encryptor->decrypt($this->data['token']);
        $collection = $this->cardCollectionFactory->create()
            ->addFieldToFilter('token', $token);
        if($collection->getSize()){
            return $collection->getFirstItem()->getData('cc_last_4');
        }
    }

    private function getPaymentInformation(){
        return $this->order->getPayment();
    }
}

So every thing works fine as the following class of payment gateway will be only called when the order has been made from that payment method. And in that case the third party extension will exists so it won't have any issue.

Problem?

Ok my problem is when I don't have the third party module as I don't wish to use it. I want to use the default magento payment modes. In that case when I di compile magento throws with an error

CustomModule\PaymentModule\Model\ResourceModel\Cards\CollectionFactory does not exists

In this case can proxy handle this type of issues? I tried this but this does not seems to work?

<type name="Vendor\ModuleName\Model\Service\Payment\PaymentClassOne">
    <arguments>
        <argument name="cardCollectionFactory" xsi:type="object">CustomModule\PaymentModule\Model\ResourceModel\Cards\CollectionFactory\Proxy</argument>
    </arguments>
</type>

I can handle this by using Object Manager. But my question is can this be handled by proxies? or any other way without Object Manager?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.