Magento 1.9 – Display Multiple Payment Methods Using One Module

magento-1.9payment-methods

I have to implement an easy payment scheme for my site. In the checkout frontend i need to display 3 payment options such as 6 months , 12 months and 24 months. Also in the payment gateway you can set only one redirect url for all 3. So without creating 3 separate payment modules i was thinking of handling it using one module as only one parameter differs (installment) from the rest of the code. Is it possible in magento? And if possible how can i achieve it?

Best Answer

Custom/Pay/Model/Custom_pay_Model_Paymodel.php

<?php
class Custom_Pay_Model_paymodel extends Mage_Payment_Model_Method_Abstract {
    protected $_code = 'pay';

    protected $_isInitializeNeeded      = true;
    protected $_canUseInternal          = true;
    protected $_canUseForMultishipping  = false;

    // define variable to add a custom form for payment method in checkout
    protected $_formBlockType = 'pay/form_pay';

    //this is the function to get the data in the custom form
    public function assignData($data)
    {
        Mage::log($data->getPayInstallment());
    }
    public function getOrderPlaceRedirectUrl() {
        return Mage::getUrl('pay/payment/redirect', array('_secure' => true));
    }       
}

Custom/pay/Block/Form/Pay.php

<?php
class Custom_Pay_Block_Form_Pay extends Mage_Payment_Block_Form
{
protected function _construct()
{
    parent::_construct();
    $this->setTemplate('pay/form/pay.phtml');
}
}

design/frontend/base/template/pay/form/pay.phtml

<ul class="form-list" id="payment_form_<?php echo $this->getMethodCode() ?>" style="display:none;">
<li>
    &nbsp
</li>    
<li>     
    <input type="radio" style="margin-left: 20px;" id="paysix" name="payment[pay_installment]" value="6" checked>
    <label for="paysix" style="float:left">6 Months</label>    

    <input type="radio" style="margin-left: 10px;" id="paytwelve" name="payment[pay_installment]" value="12">
    <label for="paytwelve">12 Months</label>    
</li>
</ul>

Using the above code i showed a custom form for the payment method with 2 radio buttons. Then in the assign data function i added it to the Mage::Registry and took it from the block which i created the request form and created the form according to the selected values.

Related Topic