title: Magento custom payment notification
body: <p>I have successfully integrated a custom payment solution for magento with a lot of help from this tutorial <a href="http://www.junaidbhura.com/how-to-make-a-custom-magento-payment-extension-for-an-external-gateway/" rel="nofollow">http://www.junaidbhura.com/how-to-make-a-custom-magento-payment-extension-for-an-external-gateway/</a></p>
  
  <p>I am at the last stage of notifying the website if a payment has been successful or not.</p>
  
  <p>I have a PaymentController.php file below but am not sure how to link this to the payment gateway notification.</p>
  
  <p>The payment gateway provide a notification to the server via an HTTP GET request, letting you know if a payment was accepted or declined. This is below</p>
  
  <p><a href="http://www.websitedomain.co.uk/mygateway/payment/response?Operator=&amp;SessionID=&amp;Note=&amp;Tariff=&amp;Status=&amp;Mobile=" rel="nofollow">http://www.websitedomain.co.uk/mygateway/payment/response?Operator=&amp;SessionID=&amp;Note=&amp;Tariff=&amp;Status=&amp;Mobile=</a></p>
  
  <p>The code for the payment controller where I need to enter the code is below</p>
  
  <pre><code>class Myname_Mygateway_PaymentController extends Mage_Core_Controller_Front_Action {
  // The redirect action is triggered when someone places an order
  public function redirectAction() {
      $this-&gt;loadLayout();
      $block = $this-&gt;getLayout()-&gt;createBlock('Mage_Core_Block_Template','mygateway',array('template' =&gt; 'mygateway/redirect.phtml'));
      $this-&gt;getLayout()-&gt;getBlock('content')-&gt;append($block);
      $this-&gt;renderLayout();
  }
  
  // The response action is triggered when your gateway sends back a response after processing the customer's payment
  public function responseAction() {
      if($this-&gt;getRequest()-&gt;isPost()) {
  
          /*
          /* Your gateway's code to make sure the reponse you
          /* just got is from the gatway and not from some weirdo.
          /* This generally has some checksum or other checks,
          /* and is provided by the gateway.
          /* For now, we assume that the gateway's response is valid
          */
  
          $validated = true;
          $orderId = ''; // Generally sent by gateway
  
          if($validated) {
              // Payment was successful, so update the order's state, send order email and move to the success page
              $order = Mage::getModel('sales/order');
              $order-&gt;loadByIncrementId($orderId);
              $order-&gt;setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Gateway has authorized the payment.');
  
              $order-&gt;sendNewOrderEmail();
              $order-&gt;setEmailSent(true);
  
              $order-&gt;save();
  
              Mage::getSingleton('checkout/session')-&gt;unsQuoteId();
  
              Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure'=&gt;true));
          }
          else {
              // There is a problem in the response we got
              $this-&gt;cancelAction();
              Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure'=&gt;true));
          }
      }
      else
          Mage_Core_Controller_Varien_Action::_redirect('');
  }
  
  // The cancel action is triggered when an order is to be cancelled
  public function cancelAction() {
      if (Mage::getSingleton('checkout/session')-&gt;getLastRealOrderId()) {
          $order = Mage::getModel('sales/order')-&gt;loadByIncrementId(Mage::getSingleton('checkout/session')-&gt;getLastRealOrderId());
          if($order-&gt;getId()) {
              // Flag the order as 'cancelled' and save it
              $order-&gt;cancel()-&gt;setState(Mage_Sales_Model_Order::STATE_CANCELED, true, 'Gateway has declined the payment.')-&gt;save();
          }
      }
  }
  </code></pre>
  
  <p>}</p>