Saturday, September 08, 2012

How to create custom magento payment module - Part 1

Magento custom payment is  very simple.If you have knowledge in magento its very simple to implement.
See the following steps to implement custom payment in magento.

Step 1:
Folder structure like this
app->etc->modules->Mycustomcard_Mycustom.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mycustomcard_Mycustom>
            <active>true</active>
            <codePool>local</codePool>
        </Mycustomcard_Mycustom>
    </modules>
</config>





Check this path in the back end.

System->configuration->Advanced->Disable Modules Output

In this page the "Mycustomcard_Mycustom" is make as enabled.



Step 2:
Then create folders like this.


app\code\local\Mycustomcard\Mycustom
app\code\local\Mycustomcard\Mycustom\Block\Form\Mycustom.php

class Mycustomcard_Mycustom_Block_Form_Mycustom extends Mage_Payment_Block_Form
{
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('mycustom/form/mycustom.phtml');
    }
}

app\code\local\Mycustomcard\Mycustom\Block\Index\Redirect.php

class Mycustomcard_Mycustom_Block_Index_Redirect extends Mage_Core_Block_Abstract
{
    protected function _toHtml()
    {
            $session_LastRealOrderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();   
             $totalamount=Mage::getSingleton('core/session')->gettotamount();
            $cardnum = Mage::getSingleton('core/session')->getcardnum();       
             $expdate = "2100-12-31 23:59";

  $html = '<form action="http://customshop.com/index.php/mycustom/index/postvalues" id="requestpayment" name="requestpayment" method="post">
<div>
                                <input id="Code" name="Code" type="hidden" value="'.time().'" />
                            <input id="Payee" name="Payee" type="hidden" value="'.$payee.'" />
                            <input id="Description" name="Description" type="hidden" value="Pembayaran kredit rumah ke 1397" />
                            <input id="Currency" name="Currency" value="'.$currency.'" type="hidden">
                            <input id="Amount" name="Amount" type="hidden" value="'.$totalamount.'" />
                            <input id="ExpireAt" name="ExpireAt" type="hidden" value="'.$expdate.'" />
                            <input id="SendResultTo" name="SendResultTo" type="hidden" value="http://customshop.com/index.php/mycustom/index/store" />
                            <input id="RedirectTo" name="RedirectTo" type="hidden" value="http://customshop.com/index.php/mycustom/index/success" />
                            <input id="CustomCard" name="CustomCard" type="hidden" value="'.$cardnum.'" />
                            <input id="PartnerCode" name="PartnerCode" type="hidden" value="'.$partner_code.'" />
                            <input id="session_LastRealOrderId" name="session_LastRealOrderId" type="hidden" value="'.$session_LastRealOrderId.'" />   
                            <input id="signature" name="signature" type="hidden" value="" />
                            </form>';
        $html.= $this->__('You will be redirected to the custompayment website in a few seconds.');
        $html.= '<script type="text/javascript">
        document.getElementById("requestpayment").submit();</script>';
      

        return $html;
 
    }
}

app\code\local\Mycustomcard\Mycustom\Block\Info\Mycustom.php

If you select the Custom card in the checkout page "Payment informations" section.
The Card Number and Expire at values as you fill and the infos display in the right side..

class Mycustomcard_Mycustom_Block_Info_Mycustom extends Mage_Payment_Block_Info
{
    protected function _prepareSpecificInformation($transport = null)
    {
        if (null !== $this->_paymentSpecificInformation) {
            return $this->_paymentSpecificInformation;
        }
        $info = $this->getInfo();
        $transport = new Varien_Object();
        $transport = parent::_prepareSpecificInformation($transport);
        $transport->addData(array(
            Mage::helper('payment')->__('Card Number') => $info->getCheckNo()
            Mage::helper('payment')->__('Expire At') => $info->getCheckDate()
        ));
        return $transport;
    }
}


\app\code\local\Mycustomcard\Mycustom\controllers\IndexController.php

class Mycustomcard_Mycustom_IndexController extends Mage_Core_Controller_Front_Action
{
  protected $_order;
   
     public function  postvaluesAction()
    {
               // code here
    }

  public function storeAction()
    {
         // code here
    }

public function cancelAction($error_mess)
        {
               if (Mage::getSingleton('checkout/session')->getLastRealOrderId())
                 {
            $order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
            if($order->getId())
                        {
                                    // Flag the order as 'cancelled' and save it
                                $order->cancel()->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, $error_mess)->save();
                        }
                 }

        }

public function redirectAction()
    {   
        $session = Mage::getSingleton('checkout/session');
        $session->setPaypalStandardQuoteId($session->getQuoteId());          $this->getResponse()->setBody($this->getLayout()->createBlock('mycustom/index_redirect')->toHtml());
        $session->unsQuoteId();
        $session->unsRedirectUrl();
      
    }
       
         public function  successAction()
    {
        $session = Mage::getSingleton('checkout/session');
        $session->setQuoteId($session->getPaypalStandardQuoteId(true));
        Mage::getSingleton('checkout/session')->getQuote()->setIsActive(false)->save();
        $this->_redirect('checkout/onepage/success', array('_secure'=>true));
    }
}

Go to part 2 for the continues....

1 comment:

  1. Thanks Admin for sharing this code.
    Faced a minor error while executing this code., but now it's working fine.
    Also try this ready made advanced partial payment extension released by FME - http://www.fmeextensions.com/magento-layaway-partial-payments.html

    ReplyDelete