0

Due to PayPal Error, i am updating the following function in app/code/Magento/Paypal/Model/Api/Nvp.php, not edit the entire code just update a single function.
Function Name : function _applyStreetAndRegionWorkarounds()

My Preference:

app/code/Gta/PayPalPreference/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Gta_PayPalPreference',
    __DIR__
);

app/code/Gta/PayPalPreference/etc/module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
        <module name="Gta_PayPalPreference" schema_version="0.0.1" setup_version="0.0.1"></module>
</config>

app/code/Gta/PayPalPreference/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"
>
 <preference
     for="Magento\Paypal\Model\Api\Nvp"
     type="Gta\PayPalPreference\Model\Api\Nvp"
 />
</config>

app/code/Gta/PayPalPreference/Model/Api/Nvp.php

<?php
declare(strict_types=1);

namespace Gta\PayPalPreference\Model\Api;

use Magento\Store\Model\StoreManagerInterface;

class Nvp extends \Magento\Paypal\Model\Api\Nvp
{
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    public function __construct(
        \Magento\Customer\Helper\Address $customerAddress,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Payment\Model\Method\Logger $customLogger,
        \Magento\Framework\Locale\ResolverInterface $localeResolver,
        \Magento\Directory\Model\RegionFactory $regionFactory,
        \Magento\Directory\Model\CountryFactory $countryFactory,
        \Magento\Paypal\Model\Api\ProcessableExceptionFactory $processableExceptionFactory,
        \Magento\Framework\Exception\LocalizedExceptionFactory $frameworkExceptionFactory,
        \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
        StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($customerAddress, $logger, $customLogger, $localeResolver, $regionFactory, $countryFactory, $processableExceptionFactory, $frameworkExceptionFactory, $curlFactory, $data);

        $this->storeManager = $storeManager;
    }

    protected function _applyStreetAndRegionWorkarounds(DataObject $address)
    {
        // e.g. if you do not want to run the code on website id = 2
        if ($this->storeManager->getWebsite()->getId() == 1) {
            parent::_applyStreetAndRegionWorkarounds($address);
            return;
        }

        if ($address->getData('street2') !== null) {
            $address->setStreet(implode("\n", [$address->getData('street'), $address->getData('street2')]));
            $address->unsetData('street2');
        }
        if ($address->getCountryId() && $address->getRegion()) {
            $regions = $this->_countryFactory->create()
                ->loadByCode($address->getCountryId())
                ->getRegionCollection()
                ->addRegionCodeOrNameFilter($address->getRegion())
                ->setPageSize(1);
            if ($regions->count()) {
                $regionItems = $regions->getItems();
                $region = array_shift($regionItems);
                $address->setRegionId($region->getId());
                $address->setExportedKeys(array_merge($address->getExportedKeys(), ['region_id']));
            }
        }
    }
}

I am getting the following error during the compile command,

Warning: Declaration of Gta\PayPalPreference\Model\Api\Nvp::_applyStreetAndRegionWorkarounds(Gta\PayPalPreference\Model\Api\DataObject $address) should be compatib le with Magento\Paypal\Model\Api\Nvp::_applyStreetAndRegionWorkarounds(Magento\Framework\DataObject $address) in /var/www/html/test/app/code/Gta/PayPalPreferenc e/Model/Api/Nvp.php on line 59

Note: website filter included.

How to solve this error?

2 Answers 2

0

app/code/Gta/PayPalPreference/Model/Api/Nvp.php

use Magento\Framework\DataObject;
use Magento\Store\Model\StoreManagerInterface;
0
0

I think the error is with the type of DataObject you're passing in _applyStreetAndRegionWorkarounds(...). Try adding use Magento\Framework\DataObject; on top of your file.

8
  • Fatal error: Cannot use Magento\Store\Model\StoreManagerInterface as StoreManagerInterface because the name is already in use in /var/www/html/test/app/code/Gta/PayPalPreference/Model/Api/Nvp.php on line 8
    – zus
    Nov 4, 2020 at 10:52
  • maybe you're adding Magento\Store\Model\StoreManagerInterface twice? Nov 4, 2020 at 10:54
  • sorry to mkae silly mistake.
    – zus
    Nov 4, 2020 at 10:56
  • no worries. See if that solved your actual problem. Nov 4, 2020 at 10:57
  • After enabling the module, if i try to add a product to the add-to-cart i am getting the following error, Invalid block type: Magento\Paypal\Block\Express\InContext\Minicart\SmartButton How to solve the error?
    – zus
    Nov 5, 2020 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.