Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pignion opened this issue Jul 2, 2024 · 1 comment
Assignees
Labels
Bug report Indicates that issue has been marked as a possible bug

Comments

@pignion
Copy link

pignion commented Jul 2, 2024

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;
   }
@pignion pignion added the Bug report Indicates that issue has been marked as a possible bug label Jul 2, 2024
@khushboo-singhvi
Copy link
Contributor

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

@khushboo-singhvi khushboo-singhvi changed the title Cannot create second capture invoice until webhook responds to first capture invoice {ECP-9281]Cannot create second capture invoice until webhook responds to first capture invoice Jul 23, 2024
@khushboo-singhvi khushboo-singhvi self-assigned this Jul 23, 2024
@candemiralp candemiralp changed the title {ECP-9281]Cannot create second capture invoice until webhook responds to first capture invoice [ECP-9281] Cannot create second capture invoice until webhook responds to first capture invoice Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug report Indicates that issue has been marked as a possible bug
Projects
None yet
Development

No branches or pull requests

2 participants