In this tutorial, I’m going to show you How to add a new payment module (Payment Gateway) to your existing Magento Installation which will accept credit cards when the order is placed and capture order ID in payment record.
Lets start by creating the following folders:
app/code/local/My/Pant/etc
app/code/local/My/Pant/controllers
app/code/local/My/Pant/Helper
app/code/local/My/Pant/Model
app/design/frontend/rwd/default/template/mygateway/
create a file called config.xml
path: app/code/local/My/Pant/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<My_pant>
<version>0.1.0</version>
</My_pant>
</modules>
<global>
<models>
<pant>
<class>My_Pant_Model</class>
</pant>
</models>
<helpers>
<pant>
<class>My_Pant_Helper</class>
</pant>
</helpers>
<blocks>
<pant>
<class>My_Pant_Block</class>
</pant>
</blocks>
</global>
<default>
<payment>
<pant>
<model>pant/standard</model>
<active>1</active>
<order_status>pending</order_status>
<title>My New Gateway</title>
<payment_action>sale</payment_action>
<allowspecific>0</allowspecific>
<sort_order>1</sort_order>
</pant>
</payment>
</default>
<frontend>
<routers>
<pant>
<use>standard</use>
<args>
<module>My_Pant</module>
<frontName>pant</frontName>
</args>
</pant>
</routers>
</frontend>
</config>
Admin Panel Configuration Options
To view the configuration options in Magento admin panel under System => Configuration.
create a file called system.xml path: app/code/local/My/Pant/etc/system.xml
<?xml version="1.0"?>
<config>
<sections>
<payment>
<groups>
<pant translate="label comment" module="paygate">
<label>My New Gateway</label>
<frontend_type>text</frontend_type>
<sort_order>0</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 Applicable From</label>
<frontend_type>select</frontend_type>
<sort_order>61</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>Countries Payment Applicable From</label>
<frontend_type>multiselect</frontend_type>
<sort_order>70</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>
<depends>
<allowspecific>1</allowspecific>
</depends>
</specificcountry>
<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>
</pant>
</groups>
</payment>
</sections>
</config>
Next step is to tell Magento to load our module and to do this we need to create a file My_Pant.xml
in /app/etc/modules and paste the following code:
<?xml version="1.0"?>
<config>
<modules>
<My_Pant>
<active>true</active> // is active or not
<codePool>local</codePool> //location of the module i.e inside the local folder
</My_Pant>
</modules>
</config>
Next step is to create a file called Data.php with the Helper folder (/app/code/local/My/Pant/ Helper/ Data.php).
Paste the following code in Data.php
<?php
class My_Pant_Helper_Data extends Mage_Core_Helper_Abstract
{}
?>
create a file called PaymentController.php under the controllers folder (/app/code/local/My/Pant/controllers/).
& Paste the following code in PaymentController.php
<?php
class My_Pant_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()) {
/* gateway's code to make sure the reponse you
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->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();
}
}
}
}
create a file called Standard.php under the Model folder (/app/code/local/My/Pant/Model/).
Paste the following code in Standard.php
<?php
class My_Pant_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));
}
}
?>
Now, create a file called redirect.phtml
Path: (/app/design/frontend/rwd/default/template/mygateway/redirect.phtml)
& Paste the following code.
<?php
$_order = new Mage_Sales_Model_Order();
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$_order->loadByIncrementId($orderId);
?>
<form name="mygatewayform" method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="orderid" value="<?php echo $orderId; ?>">
<input type="hidden" name="grandtotal" value="<?php echo $_order->getBaseGrandTotal(); ?>">
</form>
<script type="text/javascript">
document.mygatewayform.submit();
</script>
0 Comment(s)