0

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();
}
0

I've never used downloadable products, but looking at the code, it seems that the downloadable module hooks into the event sales_order_save_commit_after in order to change the status of the download. Since your code calls save on an order object the event itself should be triggered, which means Mage_Downloadable_Model_Observer::setLinkStatus should be called.

Looking at the code in there, it seems that you can change whether an item is set as available by changing the setting System > Configuration > Catalog > Downloadable Product Options > Order Item Status to Enable Downloads. By default it seems to be Invoiced. Looking at your code it doesn't seem to invoice the order so I'm guessing this is why they don't become available. Changing it to Pending may yield the behaviour you expect.

Disclaimer: I'm not 100% sure of the repercussions of doing this, but looking at the code I can't imagine there would be anything untoward.

  • So you could also go with generating a invoice, than it would be just fine. – Jeroen Mar 4 '15 at 20:27
  • Thank you for taking a look @Cags. The problem with changing Downloadable Availability from Invoiced to Pending is that if the customer manually visits /checkout/onepage/success after being redirected to my payment processors page, the downloads will become available without them needing to pay. @jeroen-boersma When I manually generate an invoice, the downloads do become available. I noticed this a while back and tried to use PlumRocket's Auto Invoice plugin but then the orders would become invoiced without activating downloads. I will look some more but this is driving me nuts... ty again – TayKimchi Mar 5 '15 at 23:00

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.