0

I am having project in magento enterprise version :2.3.7-p3

I'm trying to update Magento 2 invoice state via Rest API call : {{magento_api_url}}/V1/invoices/ and method is post below are payload for api

{ "entity": { "entity_id": 8147, "state": 2 } }

but I am getting this error : Fatal error: Uncaught Error: Call to a member function getId() on null in /var/www/html/vendor/magento/module-sales/Model/ResourceModel/Order/Invoice.php:58

Could some one help me?

1 Answer 1

0

As by default megento issue, So we can not make changes in core file, So for this I have make one alternative solution...

I have created one custom invoice interface in my custom module with same API end point : {{magento_api_url}}/V1/invoices/ in webapi.xml file and defined the our custom model with preference in di.xml file and update the invoice state successfully.

Below are the code snippet

Custom Interface

interface InvoiceCustomInterface

{ /** * save api * @param \Magento\Sales\Api\Data\InvoiceInterface $entity Invoice interface * @return \Magento\Sales\Api\Data\InvoiceInterface Invoice interface */

public function save($entity);

}

Webapi.xml

<route url="/V1/invoices/" method="POST">
    <service class="Vendor\Module_Nmae\Api\InvoiceCustomInterface" method="save"/>
    <resources>
        <resource ref="Vendor_Module_Nmae::Module_Nmae_invoice" />
    </resources>
</route>

di.xml

<preference for="Vendor\Module_Nmae\Api\InvoiceCustomInterface" type="Vendor\Module_Nmae\Model\Api\Invoice"/>

Model file

class Invoice implements InvoiceCustomInterface

{ protected $logger;

/**
 * @var InvoiceRepositoryInterface
 */
private $invoiceRepository;

public function __construct(
    LoggerInterface $logger,
    InvoiceRepositoryInterface $invoiceRepository
)
{
    $this->invoiceRepository = $invoiceRepository;
    $this->logger = $logger;
}

/**
 * @inheritdoc
 * @param $entity
 */

public function save($entity)
{
    try {
        $invoiceRepo = $this->invoiceRepository->get($entity->getEntityId());
        $invoiceRepo->setState($entity->getState());
        $this->invoiceRepository->save($invoiceRepo);
    } catch (\Exception $e) {
        $this->logger->info($e->getMessage());
    }
    return $invoiceRepo;
}

}

So this solution will resolved the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.