How to create Magento payment module

How to create Magento payment module

Here is small example which will explain how to create a simple Magento payment module.

I hope that you know how to create magento module and I will skip this step.
First of all, you have to create in etc folder config.xml file with next content:

< ?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<inchoo_mycheckout>
<version>1.0.0</version>
</inchoo_mycheckout>
</modules>
 
<global>
<models>
<mycheckout>
<class>Inchoo_Mycheckout_Model</class>
</mycheckout>
</models>
<helpers>
<mycheckout>
<class>Inchoo_Mycheckout_Helper</class>
</mycheckout>
 
</helpers>
<blocks>
<mycheckout>
<class>Inchoo_Mycheckout_Block</class>
</mycheckout>
</blocks>
</global>
 
<default>
<payment>
<mycheckout>
<model>mycheckout/standard</model>// very important thing, here you select the model for your payment method
<active>1</active>
<order_status>pending</order_status>
<title>CUSTOM CARD</title>
<payment_action>sale</payment_action>
<submit_url>https://someurl.com</submit_url>
<merchant_id>Insert merchant id</merchant_id>
<allowspecific>0</allowspecific>
<sort_order>1</sort_order>
</mycheckout>
</payment>
</default>
 
<frontend>
<routers>
<mycheckout>
<use>standard</use>
<args>
<module>Inchoo_Mycheckout</module>
<frontname>customcard</frontname>
</args>
</mycheckout>
</routers>
</frontend>
</config>

Next step: you have to create system.xml file also in etc folder with next content:

< ?xml version="1.0"?>
<config>
<sections>
<payment>
<groups>
<mycheckout translate="label comment" module="paypal">
<label>Custom CARD MyCheckOut</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>
<submit_url>
<label>Gateway URL</label>
<frontend_type>text</frontend_type>
<sort_order>58</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</submit_url>
<merchant_id>
<label>Merchant ID</label>
<frontend_type>text</frontend_type>
<sort_order>59</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</merchant_id>
<allowspecific translate="label">
<label>Payment Applicable From</label>
<frontend_type>select</frontend_type>
<sort_order>60</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><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>
 
</fields>
</mycheckout>
</groups>
</payment>
 
</sections>
</config>

In this system.xml file we create options for this payment method. These options you will see in admin section under payment method, also if you need additional options you can add them.

Last step: is creating model in folder model with file-name standard.php. This model is defined in config.xml file. Here is example:

< ?php
 
class Inchoo_Mycheckout_Model_Standard extends Mage_Payment_Model_Method_Abstract
{
 
protected $_code = 'mycheckout';
 
protected $_isInitializeNeeded      = true;
protected $_canUseInternal          = false;
protected $_canUseForMultishipping  = false;
 
/**
* Return Order place redirect url
*
* @return string
*/
public function getOrderPlaceRedirectUrl()
{
//when you click on place order you will be redirected on this url, if you don't want this action remove this method
return Mage::getUrl('customcard/standard/redirect', array('_secure' => true));
}
 
}

If your method redirect when customer click on checkout button place order you have to create (customcard/standard/redirect) standard controller with method redirectAction and etc. This page will create html form which will send POST data to payment gateway. All data which you need for creating this form you can get from Magento model $session = Mage::getSingleton(‘checkout/session’); and etc.

When you finish everything you will get next option in admin section:

I hope that will be helpful 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

How I learned to stop worrying and love 3rd party modules for Magento Luka Rajcevic
Luka Rajcevic, | 2

How I learned to stop worrying and love 3rd party modules for Magento

Adding custom credit card type to your payment method in Magento Toni Peric
Toni Peric, | 2

Adding custom credit card type to your payment method in Magento

Remind customers to place their first order in Magento Marko Martinovic
Marko Martinovic, | 17

Remind customers to place their first order in Magento

85 comments

  1. I want to upload a zip file and enzip it in my module. I have upload success but don’t know how to enzip.

    Any help please!!!!!!

  2. Hello ,
    I get Notice: Trying to get property of non-object in /var/www/magento/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php on line 568
    when I try to access Payment Methods on admin side . Any help please. I can’t figure out, what’s wrong.

  3. Remember, when working on a local dev machine i.e Windows, it WILL NOT care about case on file names. If you use standard.php on a Linux box the payment method will not show on the frontend. Make SURE you capitalize the first letter of any files within your module folder, the correct file name should be Standard.php. This one drove me crazy! Tute is great thanks Inchoo.

  4. I need little different from this tutorial hope some on can help me i need a payment method with some cash handling charges e.g Cash Handling which include in total amount of order and mentioned separately on order review page. hope some one can help me

  5. how can I get order information to send these variable to passrelle with GET method.
    Help me please
    Thanks for advence.

  6. hi,

    If i want to create a payment method that support recurring payment, what should i do?

    Have you got any suggestions on this?

  7. You are getting basic file not found msg. You need to define a helper class for the payment module if the module is gonna utilize helpers if not then remove included helpers from the xml.

  8. Hi,

    Thank you for this useful post. I tried your codes but i am receiving the following error..

    Fatal error: Class ‘Payment_Ccavenue_Helper_Data’ not found in C:\wamp\www\ccmagento\app\Mage.php on line 516

    Any help

  9. i have to make folder as App->code->local->Ebs->Epay

    and under Epay folder create a new folder etc where two file config and system.. files are update… same as your written notbook,

    and under app/code/core/mage/paypal/model/standered.php i have to put php code…
    but still only advance module have show only… no view in payment method.

    i need urgent.. and i have to put EBS pament gateway under it.. how these possible.

    Thanks

  10. When you finish everything you will get next option in admin section:

    my module shows in the list of modules (configuration>advanced) but not in the Payment Methods ,help ?

  11. Hi,

    Can you suggest some post on integrating CCAvenue in magento? This will certainly help a lot of developers here in south asian region.

    Thanks
    Vikrant

  12. Hey Guys!
    If the module does not appear in the frontend check if the model class in standard.php and config.xml are named the same

  13. How can I add custom tax to this payment module?
    Add custom tax to subtotal and retrieved that on review

  14. I want to use payment gateway which fetch users credit card details there only. I just need to post them certain parameters and redirect. Please elaborate more on below mentioned lines in your article. ( file location and exact code etc)
    ————————
    “If your method redirect when customer click on checkout button place order you have to create (customcard/standard/redirect) standard controller with method redirectAction and etc…”

  15. you did not tell us where we going to create file and how it will appear in the admin , no file structure you provide.
    please provide us the file structure .

  16. public function getOrderPlaceRedirectUrl()
    {
    //when you click on place order you will be redirected on this url, if you don't want this action remove this method
    return Mage::getUrl('customcard/standard/redirect', array('_secure' => true));
    }

    how magento knows to call this function in model.
    can i post request to payment gateway in the above function

  17. Hello,
    I have integrate my payment module and can redirect to payment site from front end order create.
    Now i want to redirect to payment site when we manually place order from admin…

    Is there any way to redirect payment site on order create from admin?

    Thanks in advance,
    Pritesh Modi

  18. Hi very thankful for u r blog.
    but the module label is not showing up in front end ,only the radio button is showing up.

    thank u

  19. I recently started building a payment module for my website to do credit card authorization for a local bank. I read several articles including this one. I am at a point I need an intro to controllers as whether authorization declined or approved I land on the default post checkout page. I would appreciate from anyone out there. If I can peek into some sample code I can learn thus you can point to some open source module that I can peek into or your own code. Many thanks…

  20. @Steet Photography Zine

    Um you comment makes no sense. They just gave instructions on how to make a base template for a payment module.

    You need to write your own controllers, since no payment method has the same controller. Since they all aren’t the same it makes no sense including one.

    But yes the tutorial is missing a controller that you need to write up on your own.

  21. hello sir my payment option is not being displayed in front end .i have followed all the steps in tutorial.my module is being displayed in backend and also in payment option but not on front end .Any body please help.

  22. Great article. Thanks a lot.

    There does seem to be a bug in the system.xml provided. The sort_order tag is closed on line 79 when it should instead be closed on line 83. This lead to a “trying to call a method on a non-object” for me that took a while for me to debug.

  23. Thank you Nered.
    I have tried with two different payment gateway worldpay and EBS.
    When I try with single page checkout I am getting error:
    “There was an error processing your order. Please try after some time” as javascript alert.
    And when try with multiple shipping and then instead of redirect to the gateway url the page goes to checkout/multishipping/success/.
    Can You help me whats the problem?
    Thanks.

  24. Pati the gateway you described is very similar to paypal. If i were you i wuld dupe the paypal module and reconfigure it to your purpose.

    Because paypal already has a working IPN that will save you some time. Reconfigure your action controler by apending the data required by your payment gateway like price, customer info, transaction emblems etc..

    When i was developing eonmega pos for magento i duped paypal module and reconfigured it saved me a lot of time. In adition check out Zend tutorials once you get in to OOP it will be a pice of cacke.

  25. Um, you need to understand this alone is not a functioning payment gateway. This is a say “blank template” on wich you can further develop your own gateway. Basicly this gives you a template and you can rewrite your procesing controler to colmpleate any actions and append any needed information to it.

  26. Thank you for Reply.
    The payment gateway I am using is Billdesk.
    When the customer click on “Place Order’ he will be redirected to the bill desk payment gateway web site. There he paid from visa , debit card etc. After successful payment he is redirect to my website.
    How to do this . I am trying to add controllers/StandardController.php
    But always getting page not found.
    Help me. Thanks.

  27. Thank You , Great post, its help me. But I am getting page not found error while redirecting.
    Please Help me how to do this . i want to redirect to a payment gateway url and after payment back to my website.
    Please help me by replying.

  28. this is really helpfull. Some times Before I was created a payment but for some reason when i created a another payment module i was unable to configure that but for your help i did that back again.
    Anyway thanks for the good tutorial.
    Thanks

  29. Hey…. good tutorial. Actually successfully made a module with it, for credit cards that are authenticated using soap calls..

    but am stuck on the re-direct part… could you elaborate on that please?

  30. Hi Nered,

    I also encountered your problem but what i just did on that is I click the “Save Config” in System>Configuration>Advanced and holla! . .my module was displayed and also my payment module. Hope this will help. =)

  31. // Nothing happens in me. I also try different instructions on books but still nothing. I’m running it in localhost. Any help? Thank you //
    P.S. This is a reference to creating a module. Anyway:

    You need to create a codepool xml file in your app/etc/modules/

    true
    community // Or local depends where you stored the module.

    Also to change the title go here: Module/Mycheckout/etc/system.xml look line 6-7.

  32. Nothing happens in me. I also try different instructions on books but still nothing. I’m running it in localhost. Any help? Thank you

  33. Tnx for the showcase but this will not work on its own. You also need the following:

    1. CodePool XML which goes in app/etc/modules/
    This is where you would have to define coodepool for magento. This would be the file that tells magento where the module is located and weather its activated or not.

    2. For a processing you would also need controller, where you would need to define functions such as startAction(), cancelAction(), void etc..

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.