Question

Sorry, I'm very new to Magento.

I am following this tutorial to create a new payment method. It provides full sample code here.

In the tutorial, when speaking of StandardController.php::successAction(), it says,

If everything checks out, we can change the state of the order from STATE_PENDING_PAYMENT to STATE_NEW, so its ready for someone on the store to ship and complete. This is also where we'll convert the quote into an invoice and mark it as paid.

I am able to find that first sentence in the code, however I can't find the second part, which is to "convert the quote into an invoice and mark it as paid":

if($state == Mage_Sales_Model_Order::STATE_PENDING_PAYMENT){

    $msg = 'Payment completed via MockPay.';
    $order->setState(Mage_Sales_Model_Order::STATE_NEW ,true,$msg,false);
    $order->save();

    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $quote->setIsActive(false)->save();

}

Perhaps the author forgot to include that part of the code. I tried reading the Magento docs and could only manage to convert to quote into an Order, not an Invoice:

    $convertQuote = Mage::getSingleton('sales/convert_quote');
    $order = $convertQuote->toOrder($quote);

What should be the code here to "convert the Quote into an Invoice, and mark it as Paid"?

Was it helpful?

Solution

You cannot convert a quote to an invoice, you have to convert the quote to an order and then create an invoice. Also the payment method should not convert the quote to an order, this is done by the save order action during checkout.

To create an invoice you could use:

$invoice = $order->prepareInvoice();
$invoice->register()->capture();

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

You can add this code in the successAction in the StandardController.php of your module.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top