I'm currently facing a weird result with my script when running it by cron. This script is creating the invoice and capture the amount online.

if($order->canInvoice()) {

  $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();




It works fine when I call the cron by Magento backend AEOScheduler, then "RunNow".

The problem is when the cron is executed automatically by crontab. The invoice is created successfully but the event "sales_order_invoice_pay" seems not fired. This is the event catched by my payment processor extension for capturing correctly the amount ONLINE.

Any idea/suggestion would be appreciate. Thank you.

2 Answers 2


Try below code:


if (!$order->canInvoice()) {
    Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));

$invoice = Mage::getModel('sales/service_order', $order)

if (!$invoice->getTotalQty()) {
        Mage::helper('core')->__('Cannot create an invoice without products.')


$invoice->addComment('Invoice genrated automatically');

$transactionSave = Mage::getModel('core/resource_transaction')


try {
} catch (Exception $e) {
        ->addError($this->__('Unable to send the invoice email.'));

You need to call the capture method of the invoice instead the pay method in order to get the amount captured. The pay method just sets the invoice to paid but capture gets in touch with you PSP and captures the desired amount.

  • If you look at the register method in invoice model, you'll see that capture method is called if setRequestedCaptureCase is setted as ONLINE (that's my case). When I run my cron through backend AOEScheduler, the amount is captured online successfully. But not with "real" cron. Do you have an idea why?
    – DarkCowboy
    Commented Apr 23, 2015 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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