Magento: Create New Payment Method -> Cash On Delivery
About: How To Create a Magento Custom Module and A “Cash On Delivery” Payment Method
Who’s Interested: Informative to the semi-technically savvy
What: Custom Magento Payment Method
So I’ve seen more and more people raising awareness about desire to create a new payment method that allows orders to be paid via “cash on delivery” or “on pickup” by customer. Is this helpful to companies who sell to local clients? Absolutely.
So in offering a solution, I’ll go ahead and outline what files need to be created and why with hopes to help educate the intigued learner in how to create a Magento Custom Module as well. The benefit in knowing how to do this is modifying existing Magento functionality in a way that it will not be overwritten upon a successful Magento upgrade.
Thus, I’ll jump in. The following 5 files will be created (relative to one’s Magento root folder):
- confix.xml
- system.xml
- PaymentMethod.php
- mysql4-install-0.1.0.php
- NewModule.xml
Here are their contents (with comments) and relative paths:
app/code/local/Mage/NewModule/etc/config.xml (below)
<?xml version="1.0"?>
<!--
/**
* Elias Interactive
*
* @title Magento -> Custom Payment Module for Cash On Delivery
* @category Mage
* @package Mage_Local
* @author Lee Taylor / Elias Interactive -> lee [at] eliasinteractive [dot] com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<config>
<global>
<!-- declare model group for new module -->
<models>
<!-- model group alias to be used in Mage::getModel('newmodule/...') -->
<newmodule>
<!-- base class name for the model group -->
<class>Mage_NewModule_Model</class>
</newmodule>
</models>
<!-- declare resource setup for new module -->
<resources>
<!-- resource identifier -->
<newmodule_setup>
<!-- specify that this resource is a setup resource and used for upgrades -->
<setup>
<!-- which module to look for install/upgrade files in -->
<module>Mage_NewModule</module>
</setup>
<!-- specify database connection for this resource -->
<connection>
<!-- do not create new connection, use predefined core setup connection -->
<use>core_setup</use>
</connection>
</newmodule_setup>
<newmodule_write>
<use>core_write</use>
</newmodule_write>
<newmodule_read>
<use>core_read</use>
</newmodule_read>
</resources>
</global>
<!-- declare default configuration values for this module -->
<default>
<!-- 'payment' configuration section (tab) -->
<payment>
<!-- 'newmodule' configuration group (fieldset) -->
<newmodule>
<!-- by default this payment method is inactive -->
<active>1</active>
<!-- model to handle logic for this payment method -->
<model>newmodule/paymentMethod</model>
<!-- order status for new orders paid by this payment method -->
<order_status>1</order_status>
<!-- default title for payment checkout page and order view page -->
<title>Cash On Delivery</title>
</newmodule>
</payment>
</default>
</config>
app/code/local/Mage/NewModule/etc/system.xml (below)
<?xml version="1.0"?> <!-- /** * Elias Interactive * * @title Magento -> Custom Payment Module for Cash On Delivery * @category Mage * @package Mage_Local * @author Lee Taylor / Elias Interactive -> lee [at] eliasinteractive [dot] com * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ --> <config> <sections> <!-- payment tab --> <payment> <groups> <!-- newmodule fieldset --> <newmodule translate="label" module="paygate"> <!-- will have title 'Cash On Delivery' --> <label>Cash On Delivery</label> <!-- position between other payment methods --> <sort_order>670</sort_order> <!-- do not show this configuration options in store scope --> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> <fields> <!-- is this payment method active for the website? --> <active translate="label"> <!-- label for the field --> <label>Enabled</label> <!-- input type for configuration value --> <frontend_type>select</frontend_type> <!-- model to take the option values from --> <source_model>adminhtml/system_config_source_yesno</source_model> <!-- field position --> <sort_order>1</sort_order> <!-- do not show this field in store scope --> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </active> <order_status translate="label"> <label>New order status</label> <frontend_type>select</frontend_type> <source_model>adminhtml/system_config_source_order_status</source_model> <!--<source_model>adminhtml/system_config_source_order_status_new</source_model>--> <!--<source_model>adminhtml/system_config_source_order_status_processing</source_model>--> <sort_order>4</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>1</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>1</show_in_store> </specificcountry> <title translate="label"> <label>Title</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>0</show_in_store> </title> </fields> </newmodule> </groups> </payment> </sections> </config>
app/code/local/Mage/NewModule/Model/PaymentMethod.php (below)
<?php
/**
* Elias Interactive
*
* @title Magento -> Custom Payment Module for Cash On Delivery
* @category Mage
* @package Mage_Local
* @author Lee Taylor / Elias Interactive -> lee [at] eliasinteractive [dot] com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Mage_NewModule_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
{
protected $_code = 'newmodule';
//protected $_formBlockType = 'payment/form_checkmo';
//protected $_infoBlockType = 'payment/info_cod';
/**
* Assign data to info model instance
*
* @param mixed $data
* @return Mage_Payment_Model_Method_Checkmo
*/
public function assignData($data)
{
$details = array();
if ($this->getPayableTo()) {
$details['payable_to'] = $this->getPayableTo();
}
if ($this->getMailingAddress()) {
$details['mailing_address'] = $this->getMailingAddress();
}
if (!empty($details)) {
$this->getInfoInstance()->setAdditionalData(serialize($details));
}
return $this;
}
public function getPayableTo()
{
return $this->getConfigData('payable_to');
}
public function getMailingAddress()
{
return $this->getConfigData('mailing_address');
}
}
app/code/local/Mage/NewModule/sql/newmodule_setup/mysql4-install-0.1.0.php (below)
<?php // here are the table creation/updates for this module
app/etc/modules/NewModule.xml (below)
<?xml version="1.0"?> <!-- /** * Elias Interactive * * @title Magento -> Custom Payment Module for Cash On Delivery * @category Mage * @package Mage_Local * @author Lee Taylor / Elias Interactive -> lee [at] eliasinteractive [dot] com * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ --> <config> <modules> <!-- declare Mage_NewModule module --> <Mage_NewModule> <!-- this is an active module --> <active>true</active> <!-- this module will be located in app/code/local code pool --> <codePool>local</codePool> <!-- specify dependencies for correct module loading order --> <depends> <Mage_Payment /> </depends> <!-- declare module's version information for database updates --> <version>0.1.0</version> </Mage_NewModule> </modules> </config>
Again, this serves as a basic configuration for a new payment method that allows Cash On Delivery and is written as a Magento Custom Module. Modifying these files may be necessary, though this should be a good outline to get you started. Any help needed, post here and we’ll get some dialogue going to find solutions for your needs.
Or simply download the files here:
Elias: Cash On Delivery (Magento Payment Method)











October 8th, 2008 at 11:40 pm
Great work, nice article. Informative and educational. Thank you for sharing it with community.
October 10th, 2008 at 8:46 am
Branko,
We’ve been excited about Magento for some time now. We’ve also had a chance to dialogue with you some and gain a better appreciation for your involvement in the Magento community.
Let us both keep up making progress with Magento on behalf of the community!
Thanks for the encouragement.
Hope to connect again soon,
Lee
October 17th, 2008 at 6:31 am
Hey man,
Many thanks for this tutorial. Looks very helpful to get started.
October 17th, 2008 at 8:08 am
Thanks Tomislav,
Great to hear from you, and I do hope this tutorial is beneficial in your endeavor to write a custom payment module.
I was wondering, would you suggest any other topics about Magento that you’d like to hear or know more about?
Let’s get a list rolling and begin writing on the most valuable topics the Magento community can benefit from.
Thanks for your input,
Lee Taylor
October 17th, 2008 at 8:16 am
Hehe. It was a coincidence that I found your article on Google, especially because Branko made the 1st comment. Two of us work at the same office in one small town in Osijek, Croatia and both of us came your way via separate paths
October 17th, 2008 at 8:31 am
That coincidence is fun to hear about
In that case since you’re working alongside Branko,I recognize much-needed topics that will benefit the Magento community have already been approached in an ongoing manner here: http://activecodeline.com/
We’re excited for your community involvement, and are glad for the opportunity to contribute as well.
Feel free to jot down thoughts on topics you’d like to hear about as they come to mind. Nonetheless, we’ll keep on blogging about the design, development, and marketing of Magento!
Lee
November 13th, 2008 at 7:32 am
hi timothy,
As Tomislav has no suggestion I have one, if I may
I’ve learned php with osc: I realize then now that I’m programing in a XXth century way. I’ve discovered Magento and love it and see this an oportunity for me for learning he XXIth century way of programing.
I’m studying now all I can find about OOP and Zend framework: that isn’t easy but surely worth it.
Here comes my request: where to start in order to discover Magento’s code? Is there some logic I should keep in mind when I need to find some piece of code I want to see/change?
You know, a tutorial about Magento’s code for beginers.
Thanks in advance. Cheers.
david
November 13th, 2008 at 8:37 am
Hi there “osdave”… In my modest opinion, Zend Framework is the way to go, especialy if you are interested in Magento. I too am on the path of learning and hopefully mastering Zend.
There are people who will say Zend is fat as framework, use CodeIgniter, or PHPCake or Symphony or… Whatever the case may be, Zend is recognized as Enterprise ready, it comes with full certification program, it’s documentation is great and so on… in short; you can’t go wrong if you choose to go with Zend.
November 13th, 2008 at 12:37 pm
hi Branko, thanks for your comment. Let’s go for Zend then
Your blog will also certainly helps me in this journey, see you there
November 13th, 2008 at 2:16 pm
Hi @osdave and Branko,
So great to have you both interacting here. I’m just catching onto the dialogue, and think Branko’s got a great perspective on jumping into Magento by initially learning the Zend framework.
I recognize a couple ways of going about Magento customization. For theme customizations, one can often l learn by comparing the code with the frontend of the theme (via Magento theme notes AND Firefox Firebug tool). In this manner, learning theme customization techniques can be self-learned automatically by adjusting the visual part of the Magento theme.
But I think you’re referring to Magento core code customization more specifically. And with our combined experience, we agree that learning the Zend Framework (which has made available some great tutorials and videos) is key.
As you get familiar more with Object Oriented Programming, I believe you’re going to see GREAT value in utilizing the concepts it has to offer. Here’s a link that might get you started actually: http://devzone.zend.com/node/view/id/627
As I understand it, Magento is written with the intention of not just utilizing the basic OOP concepts, but enhancing code re-usability and efficiency with the Zend Framework.
They’re still making changes, as we’re all growing alongside them. Let’s keep the dialogue up, and post all kinds of thoughts/questions here and there so we can help each other out.
All the best,
Lee
November 14th, 2008 at 3:29 am
hi Lee, thanks for answering.
The link to the devzone seems very interesting for me right now, as I’m revising the base of PHP. I’ll focus on parts 7 and 11 (OOP and XML) as these are both concept I am less confident with and very important for Magento (and I guess for good PHP programing).
So, thanks again for your time and I’ll check your future posts, keep up the good work
cheers, dave
November 15th, 2008 at 2:15 pm
Hi,
Thanks a lot for your work, but unfortunately the payment option doesn’s show up in frontend. It does in Admin->Configuration->Payment methods and I have enabled it. Also, I have refresed cache. Any idea what I did wrong? Thanks in advance!
November 17th, 2008 at 2:08 pm
Hi @Tonitobleroni,
Thanks for your inquiry. Can I ask what version of Magento you are using? Also, you can try to manually refresh the cache by removing all contents from the following path: var/cache/
Try that and mention what version of Magento you are using. Make sure the payment option is enabled, and we’ll see what we can troubleshoot from there.
Thanks,
Lee
November 17th, 2008 at 2:11 pm
@osdave,
That sounds like a great plan for getting started. Also, once you do get an idea for the architecture of Magento (and better seeing at a high-level how everything fits together), you can attempt to go through this payment module piece by piece to see why the code is there and how it interacts with the other files inside the module.
If you have any questions, post them here. Understanding the module may not seem as overwhelming, and it also plugs right into the Magento code with OOP concenpts (like overriding controllers with OOP practices in mind). Thanks, and all the best with your learning - as I’m doing it right with you every day =) It never stops!
- Lee
November 17th, 2008 at 2:33 pm
Hello Lee,
Thanks a lot for your response. I manually removed all content from the cache folder. I am using 1.1.6. I have enabled the payment option in Admin.
Thanks in advance!
November 17th, 2008 at 2:34 pm
…and, needless to say, it still doesn’t show up in frontend…
November 17th, 2008 at 3:00 pm
Hey @Tonitobleroni,
Please visit this post and read up about some solutions others have found when dealing with this type of issue: http://www.magentocommerce.com/boards/viewthread/832/P45/#t10489
Unfortunately, without being able to look into the code you have (as various setup options can be prohibiting this from showing up on the frontend), it is hard for me to troubleshoot. Also, some questions I might cover with you are already located within the forum post mentioned.
Lastly, if you don’t find any solution there, you may try a quick install of another Magento store and see if the module works from the fresh install.
I haven’t had time to go in and verify that the code be relevant for Magento version 1.1.6 yet, though I haven’t seen anyone else having a problem with this - so it’s not likely there is any upgrade issues involved.
Thanks,
Lee
November 17th, 2008 at 4:17 pm
Thanks Lee, I will dig through the post you provided. Thanks for your help!
November 18th, 2008 at 5:17 am
Hi,
Could you suggest me how to configure the this module in own magento application. when i putted the all files in my local system this is not overriding the default shipping method of magento.
November 18th, 2008 at 1:15 pm
Hi @sirvash,
I’d love to recommend how to configure this module in your own Magento application, though with all the possibilities, it would probably turn into a small project rather than a couple discussions.
In reference to the issue you mentioned, make sure you haven’t changed the name of the default shipping method of Magento. Also, I’d recommend going back and make double-checking that the files were copied into the correct folders (reference the paths in the download we have available).
Please contact us via our contact form below if you’d like help on a project, and we’d be glad to assist you.
Thanks!
Lee
November 18th, 2008 at 11:23 pm
Hi @Lee,
Special Thanks for quick solutions i am trying to update the title of ‘Cash on Delivery’ and updated the system.xml and config.xml but modification not visible on front.
Please assist me.
module working fine now can i update the shipping method i just wanna to save and extra value instead of the ‘Flat Tax’ on sales order table.
Thanks
Sirvash Sharma
November 19th, 2008 at 2:50 pm
Hi @Sirvash,
We’ve had someone else inquire as to a similar issue. My response can be found here: http://eliasinteractive.com/blog/magento-create-new-payment-method-cash-on-delivery/#comment-64
Thanks,
Lee