Adyen / adyen-magento2

Adyen Payment plugin for Magento2
MIT License
150 stars 196 forks source link

[ECP-9246] Unable to create Credit Memos for orders containing deleted products. #2636

Open SamJUK opened 1 month ago

SamJUK commented 1 month ago

Describe the bug Unable to create a credit memo for orders containing deleted products. Getting the following Error message An error has happened during application run. See exception log for details. With the following log message

[2024-05-29T12:01:30.491360+00:00] main.CRITICAL: Error: Call to a member function getId() on null in /vendor/adyen/module-payment/Helper/OpenInvoice.php:190

To Reproduce Steps to reproduce the behavior:

  1. Create a product
  2. Place an order using said product
  3. Invoice & Ship the order
  4. Delete the product
  5. Attempt to create a credit memo for the order

Expected behavior Credit memo to create successfully.

Magento version 2.4.5-p7

Plugin version 9.5.2

Additional Information It should be expected that calling getProduct on a order item could return null, in the case the original product no longer exists in the catalog. Either should be looking to use the data baked into the line item, or have logic to handle cases where the original item no longer exists.

SamJUK commented 1 month ago

We are running the following composer patch in production now, and thats allowing Credit Memos to be processed successfully now.

--- Helper/OpenInvoice.php
+++ Helper/OpenInvoice.php
@@ -127,7 +127,7 @@
         $product = $item->getProduct();
         $imageUrl = "";

-        if ($image = $product->getSmallImage()) {
+        if ($product && $image = $product->getSmallImage()) {
             $imageUrl = $this->imageHelper->init($product, 'product_page_image_small')
                 ->setImageFile($image)
                 ->getUrl();
@@ -185,16 +185,18 @@
         $formattedTaxPercentage = $this->adyenHelper->formatAmount($item->getTaxPercent(), $currency);

         $product = $item->getProduct();
+        $productId = $product ? $product->getId() : $item->getProductId();
+        $productUrl = $product ? $product->getUrlModel()->getUrl($product) : '';

         return [
-            'id' => $product->getId(),
+            'id' => $productId,
             'amountExcludingTax' => $formattedPriceExcludingTax,
             'amountIncludingTax' => $formattedPriceIncludingTax,
             'taxAmount' => $formattedTaxAmount,
             'description' => $item->getName(),
             'quantity' => (int) ($qty ?? $item->getQty()),
             'taxPercentage' => $formattedTaxPercentage,
-            'productUrl' => $product->getUrlModel()->getUrl($product),
+            'productUrl' => $productUrl,
             'imageUrl' => $this->getImageUrl($item)
         ];
     }
RokPopov commented 1 month ago

Hi @SamJUK,

Thank you for reporting this issue and sharing the patch with us. We have opened an internal investigation to find alternative ways of finding the ID to be used to populate the line items.

Checking your patch, with regards to the error being thrown in this line -> 'id' => $product->getId(),, wouldn't it be necessary to change the way this ID is being fetched?

Kind regards, Rok

SamJUK commented 1 month ago

Hi @RokPopov ,

Yes your correct, I uploaded an incomplete version of the patch. I've updated it now to the full version, essentially just replacing both id and productUrl in the return array with the variables defined above.

Thanks Sam

RokPopov commented 1 month ago

Hi @SamJUK,

Thank you for adjusting the patch, much appreciated. Would you be willing to create a PR with the changes in the patch?

Kind regards, Rok

SamJUK commented 1 month ago

@RokPopov Sure thing, opened a PR with ID #2645