1

I'm using Magento 1.9.0.1!

I've created a custom module which is adding an extra payment method. All what i want now is to display the instructions text from the admin panel bellow the payment method when it is selected in the checkout page.

Similar to the Bank Transfer method. When you select it, bellow appears a simple text which is giving you the instructions mentioned in the admin panel.

Here is all what i've done:

The code i have in /MyName/Mygateway/etc/config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <Myname_Mygateway>
      <version>0.1.0</version>
    </Myname_Mygateway>
  </modules>
  <global>
    <models>
      <mygateway>
        <class>Myname_Mygateway_Model</class>
      </mygateway>
    </models>
    <helpers>
      <mygateway>
        <class>Myname_Mygateway_Helper</class>
      </mygateway>
    </helpers>
    <blocks>
      <mygateway>
        <class>Myname_Mygateway_Block</class>
      </mygateway>
    </blocks>
  </global>
  <default>
    <payment>
      <mygateway>
        <model>mygateway/standard</model>
        <active>1</active>
        <order_status>pending</order_status>
        <title>My Gateway</title>
        <payment_action>sale</payment_action>
        <allowspecific>0</allowspecific>
        <sort_order>1</sort_order>
      </mygateway>
    </payment>
  </default>
  <frontend>
    <routers>
      <mygateway>
        <use>standard</use>
        <args>
          <module>Myname_Mygateway</module>
          <frontName>mygateway</frontName>
        </args>
      </mygateway>
    </routers>
  </frontend>
</config>

The code i have in /MyName/Mygateway/etc/system.xml:

<?xml version="1.0"?>
<config>
  <sections>
    <payment>
      <groups>
        <mygateway translate="label comment" module="paygate">
          <label>Second Additional Payment Method By SuperWeb.BG!</label>
          <frontend_type>text</frontend_type>
          <sort_order>2</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
          <fields>
            <active translate="label">
              <label>Enabled</label>
              <frontend_type>select</frontend_type>
              <source_model>adminhtml/system_config_source_yesno</source_model>
              <sort_order>10</sort_order>
              <show_in_default>1</show_in_default>
              <show_in_website>1</show_in_website>
              <show_in_store>0</show_in_store>
            </active>
            <title translate="label">
              <label>Title</label>
              <frontend_type>text</frontend_type>
              <sort_order>20</sort_order>
              <show_in_default>1</show_in_default>
              <show_in_website>1</show_in_website>
              <show_in_store>1</show_in_store>
            </title>
            <order_status translate="label">
              <label>New Order Status</label>
              <frontend_type>select</frontend_type>
              <source_model>adminhtml/system_config_source_order_status</source_model>
              <sort_order>50</sort_order>
              <show_in_default>1</show_in_default>
              <show_in_website>1</show_in_website>
              <show_in_store>0</show_in_store>
            </order_status>
            <allowspecific translate="label">
                            <label>Payment from Applicable Countries</label>
                            <frontend_type>allowspecific</frontend_type>
                            <sort_order>50</sort_order>
                            <source_model>adminhtml/system_config_source_payment_allspecificcountries</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </allowspecific>
                        <specificcountry translate="label">
                            <label>Payment from Specific Countries</label>
                            <frontend_type>multiselect</frontend_type>
                            <sort_order>51</sort_order>
                            <source_model>adminhtml/system_config_source_country</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                            <can_be_empty>1</can_be_empty>
                        </specificcountry>
                        <instructions translate="label">
                            <label>Instructions</label>
                            <frontend_type>textarea</frontend_type>
                            <sort_order>62</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </instructions>
                        <min_order_total translate="label">
                            <label>Minimum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>98</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </min_order_total>
                        <max_order_total translate="label">
                            <label>Maximum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>99</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </max_order_total>
                        <sort_order translate="label">
                            <label>Sort Order</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>100</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </sort_order>
          </fields>
        </mygateway>
      </groups>
    </payment>
  </sections>
</config>

The code i have in: /MyName/Mygateway/Block/Form.php:

<?php
class Myname_Mygateway_Block_Form extends Mage_Core_Block_Template
{
    /**
     * Retrieve payment method model
     *
     * @return Mage_Payment_Model_Method_Abstract
     */
    public function getMethod()
    {
        $method = $this->getData('method');

        if (!($method instanceof Mage_Payment_Model_Method_Abstract)) {
            Mage::throwException($this->__('Cannot retrieve the payment method model object.'));
        }
        return $method;
    }

    /**
     * Retrieve payment method code
     *
     * @return string
     */
    public function getMethodCode()
    {
        return $this->getMethod()->getCode();
    }

    /**
     * Retrieve field value data from payment info object
     *
     * @param   string $field
     * @return  mixed
     */
    public function getInfoData($field)
    {
        return $this->escapeHtml($this->getMethod()->getInfoInstance()->getData($field));
    }

    /**
     * Check whether current payment method can create billing agreement
     *
     * @return bool
     */
    public function canCreateBillingAgreement()
    {
        return $this->getMethod()->canCreateBillingAgreement();
    }
}

The code i have in: /MyName/Mygateway/controllers/PaymentController.php:

<?php
class Myname_Mygateway_PaymentController extends Mage_Core_Controller_Front_Action {
    // The redirect action is triggered when someone places an order
    public function redirectAction() {
        $this->loadLayout();
        $block = $this->getLayout()->createBlock('Mage_Core_Block_Template','mygateway',array('template' => 'mygateway/redirect.phtml'));
        $this->getLayout()->getBlock('content')->append($block);
        $this->renderLayout();
    }

    // The response action is triggered when your gateway sends back a response after processing the customer's payment
    public function responseAction() {
        if($this->getRequest()->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 = '123'; // 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->loadByIncrementId($orderId);
                $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Gateway has authorized the payment.');

                $order->sendNewOrderEmail();
                $order->setEmailSent(true);

                $order->save();

                Mage::getSingleton('checkout/session')->unsQuoteId();

                Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure'=>true));
            }
            else {
                // There is a problem in the response we got
                $this->cancelAction();
                Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure'=>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')->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, 'Gateway has declined the payment.')->save();
            }
        }
    }
}

The code i have in /MyName/Mygateway/Helper/Data.php:

<?php
class Myname_Mygateway_Helper_Data extends Mage_Core_Helper_Abstract
{

}

The code i have in /MyName/Mygateway/Model/Standart.php:

<?php
class Myname_Mygateway_Model_Standard extends Mage_Payment_Model_Method_Abstract {
    protected $_code = 'mygateway';

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

    public function getOrderPlaceRedirectUrl() {
        return Mage::getUrl('mygateway/payment/redirect', array('_secure' => true));
    }
}
?>

Here is what i've got in the design: /app/design/frontend/base/default/template/mygateway/redirect.phtml

<?php
class Myname_Mygateway_Model_Standard extends Mage_Payment_Model_Method_Abstract {
    protected $_code = 'mygateway';

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

    public function getOrderPlaceRedirectUrl() {
        return Mage::getUrl('mygateway/payment/redirect', array('_secure' => true));
    }
}
?>

Everything is working fine, but when i select this payment method the instructions which i've set in the admin panel are not showing up. How can i make this thing similar to the bank transfer payment method, when you select it to show the instructions text bellow ?

Thanks in advance!

3

First you need to declare form and info blocks in your payment model. Default ones are payment/form and payment/info declared in Mage_Payment_Model_Method_Abstract.

So in your model:

protected $_formBlockType = 'mygateway/form';
protected $_infoBlockType = 'mygateway/info';

Next, also in your payment model, you need to create method that will read config.

public function getInstructions()
{
    return trim($this->getConfigData('instructions'));
}

Once this is done, have a look at Mage_Payment_Block_Info_Banktransfer and payment/info/banktransfer.phtml template. It should be self explanatory at this point.

EDIT

Payment model:

<?php

class Myname_Mygateway_Model_Standard extends Mage_Payment_Model_Method_Abstract
{
    protected $_code = 'mygateway';

    protected $_formBlockType = 'mygateway/form';
    protected $_infoBlockType = 'mygateway/info';

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

    public function getInstructions()
    {
        return trim($this->getConfigData('instructions'));
    }

    public function getOrderPlaceRedirectUrl()
    {
        return Mage::getUrl('mygateway/payment/redirect', array('_secure' => true));
    }
}

Info Block:

<?php

    class Myname_Mygateway_Block_Info extends Mage_Payment_Block_Info
    {
        protected $_instructions;

        protected function _construct()
        {
            parent::_construct();
            $this->setTemplate('template/mygateway/info.phtml');
        }

        public function getInstructions()
        {
            if (is_null($this->_instructions)) {
                $this->_instructions = $this->getInfo()->getAdditionalInformation('instructions');
                if(empty($this->_instructions)) {
                    $this->_instructions = $this->getMethod()->getInstructions();
                }
            }
            return $this->_instructions;
        }
    }

Info Template:

<p><?php echo $this->escapeHtml($this->getMethod()->getTitle()) ?></p>
<?php if ($this->getInstructions()): ?>
<table>
    <tbody>
        <tr>
            <td><?php echo nl2br($this->getInstructions()) ?></td>
        </tr>
    </tbody>
</table>
<?php endif; ?>

Form Block:

<?php

    class Myname_Mygateway_Block_Form extends Mage_Payment_Block_Form
    {

        protected $_instructions;

        protected function _construct()
        {
            parent::_construct();
            $this->setTemplate('template/mygateway/form.phtml');
        }

        public function getInstructions()
        {
            if (is_null($this->_instructions)) {
                $this->_instructions = $this->getMethod()->getInstructions();
            }
            return $this->_instructions;
        }

    }

Form Template:

<?php if ($instructions = $this->getInstructions()): ?>
    <ul class="form-list checkout-agreements" id="payment_form_<?php echo $this->getMethodCode() ?>" style="display:none;">
        <li>
            <div class="<?php echo $this->getMethodCode() ?>-instructions-content agreement-content">
                <?php echo nl2br($instructions) ?>
            </div>
        </li>
    </ul>
<?php endif; ?>
  • Can you please edit your answer and show me how your code must look like in my code. Thanks! – Venelin Vasilev Jan 5 '15 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.