Adyen/adyen-magento2

[ECP-9281] Cannot create second capture invoice until webhook responds to first capture invoice

Opened this issue · 1 comments

Describe the bug
Cannot create second capture invoice until webhook responds to first capture invoice.

To Reproduce
Steps to reproduce the behavior:

  1. Create a Magento order with multiple items or qtys
  2. Wait for the AUTHORIZE webhoook to make the order able to be invoiced and captured
  3. Submit an invoice to the Magento web API (or create one in the admin) with:
    • capture set to true
    • only some items in the order invoiced
  4. Immediately attempt to use the Magento Web API to send a second invoice for additional items for the same order, before the CAPTURE webhoook from the first invoice has yet been called

Expected behavior
A second invoice is created, a second capture request is sent to Adyen

Actual behavior
The following validation error message:

An invoice cannot be created when an order has a status of payment_pending

This means an automated invoicing system (such as ours) that calls Magento's Web APIs for invoicing would not be able to know whether it is safe to call the Web API, because it would not know whether the order is still waiting for a CAPTURE webhook. Sending multiple invoices in close time proximity for a single order is not uncommon, and a trial and error approach to this is not feasible in our environment, which processes incoming Magento Web API calls asynchronously.

Magento version
2.4.5-p5

Plugin version
9.4.1

Additional context
When an invoice that is set to capture is submitted (via webservice or admin), Adyen’s module sets the state of the order to payment_pending until Adyen calls a CAPTURE webhoook.

vendor/adyen/module-payment/Gateway/Response/PaymentCaptureDetailsHandler.php:66

if ($response["status"] === TransactionCapture::CAPTURE_RECEIVED) {
    $this->setInvoiceToPending($payment);
}

Once the CAPTURE webhoook is called, if the order hasn’t been completely invoiced, the status is set back to its pre-invoice state (this isn't particularly desired behavior either).

vendor/adyen/module-payment/Helper/Order.php:310

/*
 * Set order status back to pre_payment_authorized if the order state is payment_review.
 * Otherwise, capture-cancel-refund is not possible.
 */
if ($order->getState() === MagentoOrder::STATE_PAYMENT_REVIEW) {
    $order = $this->setPrePaymentAuthorized($order);
}

Submitting another invoice before receiving the CAPTURE webhook for the previous invoice prevents Magento from being able to create the invoice because payment_pending is not a state where magento allows invoices to be created:

vendor/magento/module-sales/Model/Order/Validation/CanInvoice.php:42

private function isStateReadyForInvoice(OrderInterface $order)
   {
       if ($order->getState() === Order::STATE_PAYMENT_REVIEW ||
           $order->getState() === Order::STATE_HOLDED ||
           $order->getState() === Order::STATE_CANCELED ||
           $order->getState() === Order::STATE_COMPLETE ||
           $order->getState() === Order::STATE_CLOSED
       ) {
           return false;
       }

       return true;
   }

Hello @pignion,

Thank you for reporting this issue and providing detailed steps to reproduce. We've investigated the problem and considered various potential solutions.

After careful consideration, we've decided to maintain the current implementation of setting the order state to payment_review. This approach, while causing some limitations, is crucial for preventing order fulfillment before successful captures are confirmed. This is especially important as capture requests might fail, and our plugin can only be notified of the outcome via webhooks.

We are open for more suggestions. Please let us know if you have any other proposed solution to the issue.

Regards!
Khushboo Singhvi