Creating Invoice & Capturing on Shipment

Creating Invoice & Capturing on Shipment

September 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Creating Invoice & Capturing on Shipment

Resolving Invoice & Payment Status Issues for Magento Shipments

In your recent setup, you’ve successfully integrated APIs that periodically create shipments for orders. To automate the process further, you’ve written an observer to generate invoices and capture payments when a new shipment is created. The code seems well-structured and is hooked up to the sales_order_shipment_save_after event.

Let’s dive into your current implementation and address the status issue that’s preventing the order from being marked as “Complete” despite a successful invoice creation and payment capture.

Understanding the Current Implementation

Your observer function, autoInvoice, does an excellent job of capturing essential details:

  • It acquires the shipment object from the event.
  • Retrieves the associated order and items collection.
  • Prepares an array $qty containing ordered quantities for each item in the shipment.

The core logic involves creating a new invoice using the sales/order_invoice_api model, passing the order ID and quantities to the create function. After creating the invoice, you immediately capture the payment by calling the capture method on the invoice object.

The Status Limbo: Why Orders Remain “Pending”

The mystery lies in the order status remaining as “Pending” even after a successful shipment creation, invoice generation, and payment capture. This issue seems to stem from the database values not being updated properly.

Identifying the Potential Issue

Upon closer inspection, you’ve correctly observed that:

  • The items on the order have accurate quantities for both “Ordered” and “Shipped.”
  • However, the qty_invoiced field remains unaltered for each item in the sales_order_item table.
  • This discrepancy might be causing the order status to stay as “Pending.”

A Suspected Culprit: Reverted Quantities

Your speculation about the qty_invoiced field being reverted is quite insightful. It suggests that during the invoice creation process, the quantities allocated to the items might not be properly synced with the order’s status.

Potential Solutions and Further Investigation

To troubleshoot this issue effectively, consider the following steps:

1. Review Invoice Creation Logic

Ensure that your autoInvoice function is correctly updating the qty_invoiced field during the invoice creation process. Verify if the $invoice->create() call accurately reflects the actual quantities shipped.

2. Check for Database Write Operations

Inspect the database logs or use debugging tools like Xdebug to monitor SQL queries executed during the observer’s lifecycle. This will help you identify any failed or reverted database operations that might be causing the status inconsistency.

3. Verify Invoice and Payment Status Manually

After creating an invoice, manually check if the invoice_state is set to “paid” in the sales_flat_invoice table. Additionally, confirm whether the payment_amount field in the sales_payment_transaction table reflects the captured amount accurately.

4. Double-Check Event Sequence and Timing

Ensure that your observer is triggered at the correct point in the shipment creation process. If multiple observers are involved or if there’s a delay between events, it might cause discrepancies in updating order status and quantities simultaneously.

Conclusion and Recommendations

Addressing this issue requires a combination of thorough code review, database inspection, and potential refactoring of your invoice creation logic.

Here are some key takeaways:

  • Double-check the database operations during the autoInvoice function’s execution.
  • Verify that the qty_invoiced field is being updated correctly when creating invoices.
  • Manually inspect the invoice status and payment details in the Magento admin panel or database to confirm the captured amounts.

By following these recommendations, you should be able to identify the root cause of the “Pending” order status issue and implement the necessary fixes. If required, feel free to visit our Magento Development Guide for advanced troubleshooting techniques and best practices in Magento customization.

Happy coding! Let us know if you need further assistance or if there are other aspects of your Magento setup that you’d like to explore.

Database API Extensions Orders

Our network