public function indexAction()
 {
     $logger = Mage::helper('worldpay/logger');
     if (!$this->getRequest()->isPost()) {
         $logger->log('Auth/Capture notifications must be POST');
         echo '[OK]';
         return;
     }
     $worldpay = Mage::getModel('worldpay/paymentMethods_creditCards')->setupWorldpay();
     try {
         $response = file_get_contents('php://input');
         $originalNotification = Worldpay::handleResponse($response);
     } catch (Exception $e) {
         http_response_code(500);
         $logger->log('Error getting webhook');
         echo '[OK]';
         return;
     }
     try {
         $notification = $worldpay->getOrder($originalNotification['orderCode']);
     } catch (Exception $e) {
         http_response_code(500);
         $logger->log('Error validating webhook');
         echo '[OK]';
         return;
     }
     if ($originalNotification['paymentStatus'] != $notification['paymentStatus']) {
         http_response_code(500);
         $logger->log('Order status does not match');
         echo '[OK]';
         return;
     }
     //Get order by quote id, add payment as success
     $order = Mage::getModel('sales/order')->loadByAttribute('ext_order_id', $notification['orderCode']);
     $payment = $order->getPayment();
     if (!$payment->getId()) {
         $payment = Mage::getModel('sales/order_payment')->getCollection()->addAttributeToFilter('last_trans_id', array('eq' => $notification['orderCode']))->getFirstItem();
         $order = Mage::getModel('sales/order')->load($payment->getParentId(), 'entity_id');
     }
     if (!$payment->getId()) {
         http_response_code(500);
         $logger->log('Payment ' . $notification['orderCode'] . ' not found!');
         echo '[OK]';
         return;
     }
     if (!$order) {
         http_response_code(500);
         $logger->log('Order ' . $notification['orderCode'] . ' not found!');
         echo '[OK]';
         return;
     }
     $amount = false;
     if ($notification['amount']) {
         $amount = $notification['amount'] / 100;
     }
     Mage::getModel('worldpay/paymentMethods_creditCards')->updateOrder($notification['paymentStatus'], $notification['orderCode'], $order, $payment, $amount);
     // give worldpay confirmation
     echo '[OK]';
 }
 /**
  *
  */
 public function indexAction()
 {
     $logger = Mage::helper('worldpay/logger');
     if (!$this->getRequest()->isPost()) {
         $logger->log('Auth/Capture notifications must be POST');
         /**
          * send OK to whoever tries to play with us
          */
         echo '[OK]';
         return;
     }
     require_once Mage::getModuleDir('', 'Worldpay_Payments') . DS . 'lib' . DS . 'worldpay.php';
     try {
         $response = file_get_contents('php://input');
         $notification = Worldpay::handleResponse($response);
     } catch (Exception $e) {
         http_response_code(500);
         $logger->log('Error getting webhook');
         echo '[OK]';
         return;
     }
     $payment = Mage::getModel('sales/order_payment')->getCollection()->addAttributeToFilter('last_trans_id', array('eq' => $notification['orderCode']))->getFirstItem();
     if (!$payment) {
         http_response_code(500);
         $logger->log('Payment ' . $notification['orderCode'] . ' not found!');
         echo '[OK]';
         return;
     }
     $order = Mage::getModel('sales/order')->load($payment->getParentId(), 'entity_id');
     if (!$order) {
         http_response_code(500);
         $logger->log('Order ' . $notification['orderCode'] . ' not found!');
         echo '[OK]';
         return;
     }
     $payment = $order->getPayment();
     $amount = $order->getGrandTotal();
     if ($notification['paymentStatus'] === 'REFUNDED') {
         $payment->setIsTransactionClosed(1);
         $payment->setStatus(Worldpay_Payments_Model_PaymentMethods_Abstract::STATUS_VOID);
         $order->setSate(Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW, true, 'Payment Refunded');
     } else {
         if ($notification['paymentStatus'] === 'FAILED') {
             $order->cancel()->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, 'Gateway has declined the payment.')->save();
             $payment->setStatus(Worldpay_Payments_Model_PaymentMethods_Abstract::STATUS_DECLINED);
         } else {
             if ($notification['paymentStatus'] === 'SETTLED') {
                 $payment->setIsTransactionClosed(1);
                 $payment->setStatus(Worldpay_Payments_Model_PaymentMethods_Abstract::STATUS_SUCCESS);
             } else {
                 if ($notification['paymentStatus'] === 'SUCCESS') {
                     $payment->setTransactionId($notification['orderCode'])->setShouldCloseParentTransaction(1)->setIsTransactionClosed(1)->registerCaptureNotification($amount);
                 } else {
                     // Other status, magento doesn't handle.
                     $payment->setStatus(Worldpay_Payments_Model_PaymentMethods_Abstract::STATUS_UNKNOWN);
                     $order->addStatusHistoryComment('Worldpay Payment Status:' . $notification['paymentStatus']);
                     $order->setSate(Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW, true, $notification['paymentStatus']);
                 }
             }
         }
     }
     $order->save();
     // give worldpay confirmation
     echo '[OK]';
 }