I have the following custom payment plugin controller. After purchase, it does not enable a downloadable product. I am wondering if there is a simple line of code I could add so that this controller will also make the downloadable product in the order Available.
//Get data from Gateway postback $data = $this->getRequest()->getPost(); $orderId = $data['orderid']; if ($data['reasonForDeclineCode'] == '') { // Payment was successful, so update the order's state, send order email and move to the success page $order = Mage::getModel('sales/order'); $order->loadByIncrementId($orderId); $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Gateway has authorized the payment.'); $order->setStatus('processing'); $order->sendNewOrderEmail(); $order->setEmailSent(true); $order->save(); Mage::getSingleton('checkout/session')->unsQuoteId(); Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure'=>true)); } else { // Payment was declined, so update the order's state, send order email and move to the success page $order = Mage::getModel('sales/order'); $order->loadByIncrementId($orderId); $order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, 'Gateway has declined the payment.'); //$order->sendNewOrderEmail(); //$order->setEmailSent(true); $order->save(); Mage::getSingleton('checkout/session')->unsQuoteId(); Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failed', array('_secure'=>true)); }
I ended up "solving" the problem by adding the following code to invoice successful orders after $order->setStatus('processing');
and before $order->sendNewOrderEmail();
however now I have a problem where every time an order is placed, the status on previous successful orders once again gets updated with processing and complete. This happens many more times. So far it's just an annoyance and bad for my database but not hurting anything. I will look into it later but if someone has a hint.
//create invoice for the order $invoice = $order->prepareInvoice() ->setTransactionId($order->getId()) ->addComment("Invoice created by payment processor plugin.") ->register() ->pay(); $transaction_save = Mage::getModel('core/resource_transaction') ->addObject($invoice) ->addObject($invoice->getOrder()); $transaction_save->save(); //now create shipment //after creation of shipment, the order auto gets status COMPLETE $shipment = $order->prepareShipment(); if( $shipment ) { $shipment->register(); $order->setIsInProcess(true); $transaction_save = Mage::getModel('core/resource_transaction') ->addObject($shipment) ->addObject($shipment->getOrder()) ->save(); }