0

We're using Authorization, then Capture method for Credit Card payments. After we've quickly glanced over a batch of orders we'd like to invoice them all at once.

Step 1: Invoicing is easy. "Sales" -> "Orders". Click on the orders you'd like to invoice. Go to "Actions [Invoice]". "Submit". Multiple orders have just been invoiced!

Step 2 (Capturing) is the problem -- none of the invoices get Captured and the created invoices are given the status "Pending". From the "Sales"->"Invoices" lists, we can manually click into each Invoice, we can click the option "Capture" and that works . We can also go to "Sales" -> "Orders" and click "Invoice" and that will capture as well. But clicking into the orders manually kind of defeats the purpose of Step 1.

Is there some sort of Pending Invoice scheduler that captures pending CC invoices and I'm not aware of it??

1

Currently, this functionality doesn't exist but there are plugins that do it. However, if you're feeling fancy you can run some code that will capture all invoices from orders that are in a pending status:

<?php

require('app/Mage.php');
Mage::app();

$_helper = Mage::helper('core');

$orders = Mage::getModel('sales/order')
            ->getCollection()
            ->addAttributeToFilter('status',array('eq'=>'pending'));

foreach($orders as $order){

    try {
        if(!$order->canInvoice()){
            Mage::throwException($_helper->__('Cannot create an invoice.'));
        }
        $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
        if (!$invoice->getTotalQty()){
            Mage::throwException($_helper->__('Cannot create an invoice without products.'));
        }

        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
        $invoice->register();

        $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($invoice)
            ->addObject($invoice->getOrder());

        $transactionSave->save();
    } catch (Mage_Core_Exception $e) {
        //do something meaningful here?
        Mage::logException($e->getMessage());
    }

}

Source (invoice capture portion) Inchoo: http://inchoo.net/ecommerce/magento/how-to-create-magento-invoice-from-order/

| improve this answer | |
  • Thanks for confirming Phil. I found plugins that you were talking about and the code that does it is really helpful. I also discovered one of a tools we're using for accounting integration ( Ecc Webgility ) does it really efficiently too. It's nice to have options. – JustinP Jan 21 '14 at 11:49
0

I was trying to capture a lot of orders at the same time with my own code, and kept getting this error no matter what I tried:

.

We were not able to find the order you are making this action for. Please make sure you are calling for the correct order and try again.

.

Turns out Magento wasn't quick enough, so I actually had to (yes this is true) add a 1 second timeout between iterations. My complete code as follows:

.

<?php
/**
 * this method was part of a console script, that's why i echo stuff.
 */
public function fixOrders()
{
    foreach ($this->orderArray as $incrementId) {
        try {
            /** @var Mage_Sales_Model_Order $orderModel */
            $orderModel->loadByIncrementId($incrementId);

            if ($orderModel->getIncrementId() == $incrementId) {
                /** @var Mage_Sales_Model_Order_Invoice $invoice */
                $invoice = $orderModel->getInvoiceCollection()->getFirstItem();
                if (!$invoice->canCapture()) {
                    throw new Exception("Could not capture invoice for order ".$incrementId);
                }
                $invoice->capture();
                $invoice->getOrder()->setIsInProcess(true);

                Mage::getModel('core/resource_transaction')
                    ->addObject($invoice)
                    ->addObject($invoice->getOrder())
                    ->save();

                echo "Invoiced order ".$incrementId."\n";
                sleep(1);
            } else {
                throw new Exception("Failed to load order ".$incrementId);
            }
        } catch (Exception $e) {
            echo "--- d'oh! Failed on: ".$incrementId." with msg -> ".$e->getMessage()."\n";
        }
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.