/** * Prepare the collection for the report * * @return $this|Mage_Adminhtml_Block_Widget_Grid */ protected function _prepareCollection() { // Add in a new collection $collection = new Varien_Data_Collection(); // Init the wrapper $wrapper = Mage::getModel('gene_braintree/wrapper_braintree'); // Validate the credentials if ($wrapper->validateCredentials()) { // Grab all transactions $transactions = Braintree_Transaction::search($this->_prepareBraintreeSearchQuery()); // Retrieve the order IDs $orderIds = array(); /* @var $transaction Braintree_Transaction */ foreach ($transactions as $transaction) { $orderIds[] = $transaction->orderId; } // Retrieve all of the orders from a collection $orders = Mage::getResourceModel('sales/order_collection')->addAttributeToFilter('increment_id', array('in' => $orderIds)); /* @var $transaction Braintree_Transaction */ foreach ($transactions as $transaction) { // Create a new varien object $transactionItem = new Varien_Object(); $transactionItem->setData((array) $transaction->_attributes); // Grab the Magento order from the previously built collection /* @var $magentoOrder Mage_Sales_Model_Order */ $magentoOrder = $orders->getItemByColumnValue('increment_id', $transaction->orderId); // Set the Magento Order ID into the collection // Not all transactions maybe coming from Magento if ($magentoOrder && $magentoOrder->getId()) { $transactionItem->setMagentoOrderId($magentoOrder->getId()); $transactionItem->setOrderStatus($magentoOrder->getStatus()); } else { $transactionItem->setOrderStatus('<em>Unknown</em>'); } // Add the item into the collection $collection->addItem($transactionItem); } } else { // If the Braintree details aren't valid take them to the configuration page Mage::getSingleton('adminhtml/session')->addError(Mage::helper('gene_braintree')->__('You must enter valid details into the Braintree v.zero - Configuration payment method before viewing transactions.')); // Send the users on their way Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl('/system_config/edit/section/payment') . '#payment_gene_braintree-head'); Mage::app()->getResponse()->sendResponse(); // Stop processing this method return false; } $this->setCollection($collection); parent::_prepareCollection(); return $this; }