Archive for the ‘Tutorials’ Category

I’ve been doing some work with the Magento e-commerce platform lately and my task was to create a new payment method to use during the standard checkout process.

I’ve come to the conclusion that Magento is very annoying to develop for if you know nothing about the platform. Yes the platform is open source but they also have a commercial version, therefore it seems they have tried to make it hard for you to know the complete ins and outs of the system. I.E. if something seems like too much of a task to develop, a company would likely purchase the commercial version and get it developed by the platform developers rather than do it themselves.

You also have to remember that Magento is built on top of the Zend framework, so when you are developing for it documentation does not all reside on the Magento site, you will have to look into how the Zend framework works as well.

Finally, note that by default the Magento checkout process uses AJAX to submit the form, making this a very annoying module to debug. Thankfully Magento makes it easy to log values and throw exceptions which ‘pause’ the checkout process.

After much searching online, it seems that any available tutorials for custom payment methods either don’t work, are incomplete or worked in previous versions but are now updated. I developed this using version 1.3.2.2 of Magento.

I’m writing this tutorial with the intention of it being the definitive guide to creating a custom payment method.

* You’ll notice that I’m using PHP comments in XML code snippets. This is because WordPress isn’t letting me post normal XML comments 🙂 You’ll have to change/remove them if you plan to copy and paste.

There are a few things you need to note now to avoid confusion later down the line.

For your module, you need the main name and a simpler alias for it. I.E. if your module was called MyPaymentMethod, your alias could be mypayment. Keep the full name and the alias in mind. The full name will be used in class and folder names but the alias will be used when referencing the module anywhere else.

For this tutorial I will be using ModuleName for the full name, modulename for the module alias, and MyModules as the module namespace.

Magento names it’s classes after their path. I.E. you are going to have a class which handles all of this payment method logic, and it’s path will be something like app/code/local/MyModules/ModuleName/Model/PaymentLogic.php. It’s class name will be MyModules_ModuleName_Model_PaymentLogic. Remember this as it helps when trying to figure out where to find a referenced class etc.

#1 In Magento terms, MyModules would be the module’s namespace. I.E. you’ll likely have one namespace for multiple modules. The namespace can be called anything but should be UpperCamelCase. If you take a look in app/code/core you will see a folder called Mage – this is the namespace for most of Magento’s core modules.

#2 This is your actual module name. This should be UpperCamelCase.

#3 This folder is used to contain block classes that are used in your module.

#4 This is where your module configuration will go.

#5 The class that handles all of your payment gateway logic will go here. This is also a good place to put any third party libraries or custom classes you may use.

config.xml

Create the file config.xml in app/code/local/MyModules/ModuleName/config. This file is the main module configuration file. For now, you want it to look like this;

Now the module has it’s basic configuration but Magento doesn’t care because the module is not yet registered.

MyModules_All.xml

Create the file MyModules_All.xml in app/etc/modules. This is where we register the module. You want this file to look like this;

<?xml version="1.0"?>
<config>
<modules>
<MyModules_ModuleName>
<active>true</active>
/**
* for now we're telling the system that
* this module is just local, it's not a core
* or community module and resides in
* app/code/local.
*/
<codePool>local</codePool>
</MyModules_ModuleName>
</modules>
</config>

The module is now registered and Magento knows about it. But what can you do with it? Well, this is a payment method module, so we need to register it as a usable payment method. We also need to be able to set what fields are configured in the administration section that can be used when authenticating with the payment gateway etc.

Create the file system.xml in app/code/local/MyModules/ModuleName/etc. Here we will tell Magento to have a new section in the payment methods configuration area for configuring this module. We also set what fields we want there. For every field, we have an element like this;

As you will see below. To start with, have the file looking like this;

<?xml version="1.0"?>
<config>
<sections>
/**
* here we're telling the system we
* we want to add a new config section
* in the payment method area
*/
<payment>
<groups>
<modulename translate="label" module="paygate">
/**
* human readable module name
* which appears as the name of the
* payment method in the admin section
*/
<label>My Payment Module</label>
/**
* set where this method should appear
* in comparison to the other methods
* within the admin
*/
<sort_order>670</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
<fields>
/**
* you need this field if you want the user to
* be able to disable/enable this payment
* method. leave this out if you don't want
* this to be optional
*/
<active translate="label">
<label>Enabled</label>
<frontend_type>select</frontend_type>
/**
* we want to use the standard Magento
* yes/no select options
*/
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</active>
/**
* the below two fields are an example
* of fields you may need if integrating with
* a payment gateway's API. you will see how
* to retrieve these values in your code later
*/
<api_key translate="label">
<label>API Key</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>
</api_key>
<api_secret translate="label">
<label>API Secret</label>
<frontend_type>text</frontend_type>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</api_secret>
/**
* having this field here allows the user
* to set whether we want payments to be
* authorised or authorised AND captured
* if you don't want the user to choose you
* can leave this field out
*/
<payment_action translate="label">
<label>Payment Action</label>
<frontend_type>select</frontend_type>
/**
* we're using the source from the paygate
* module as it has the options we want. this
* is safe because the paygate module is a core
* Magento module and therefore will be there
* with any default install. if it makes you feel
* safer you can create your own field source model
* but that isn't covered in this tutorial
*/
<source_model>paygate/authorizenet_source_paymentAction</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>
</payment_action>
/**
* what do you want the status to be of
* new orders made using this payment
* method?
*/
<order_status translate="label">
<label>New order status</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_order_status_processing</source_model>
<sort_order>5</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>
/**
* this field lets the user choose the
* name of the payment method as it
* appears to the normal user on your site
*/
<title translate="label">
<label>Title</label>
<frontend_type>text</frontend_type>
<sort_order>6</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>
</modulename>
</groups>
</payment>
</sections>
</config>

Default configuration values

Before we go and test this, we should set some default values for the configuration fields we just created.

Open app/code/local/MyModules/ModuleName/etc/config.xml and add the following section;

<config>
...
<default>
<payment>
<modulename>
/**
* is this payment method enabled?
*/
<active>0</active>
/**
* this is where we tell the system what
* class to use to handle all of the payment
* logic. we call this class the payment
* adapter model class. you can change the
* paymentLogic bit to be what you want
* but it has to be lowerCamelCase and
* the class name will have to be changed later
* on as you will see
*/
<model>modulename/paymentLogic</model>
<order_status>pending</order_status>
<title>Credit Card (My Payment Method)</title>
<api_key>1234</api_key>
<api_secret>1234</api_secret>
/**
* this is the default set of allowed credit
* card types. leave this as it is for now
*/
<cctypes>AE,VI,MC,DI,SS</cctypes>
/**
* this should be authorize or authorize_capture
* you can probably guess that authorize just
* authorizes the payment but authorize_capture
* processes it as well
*/
<payment_action>authorize_capture</payment_action>
/**
* this field is used to say whether you only
* want this method to be used for certain
* countries but that is not covered by this
* tutorial
*/
<allowspecific>0</allowspecific>
</modulename>
</payment>
</default>
</config>

Of course, if you have any of the fields in the above file configurable as set in system.xml, the user set value will be used.

Test progress so far

OK so now if you log into the Magento administration and go to System -> Configuration -> Payment methods you should see your new payment method somewhere in that list.

If you enable it it should then also be available if you checkout on your site as normal. Don’t complete any orders using it yet as there will be problems because we haven’t finished! We still need to create the most important part, the class which will handle all of the logic.

If you don’t see your payment method, please make sure you have completed the above steps correctly. Check for any simple mistakes and try clearing the Magento cache as well. The basic rule is; if it doesn’t appear in the payment method list in the administration section, there’s a problem with system.xml. If it doesn’t appear as a usable payment method when checking out, there could be a problem with config.xml.

If the payment_action option of ‘Authorise’ has been selected, then only the authorize() method will be called and no payment can be taken automatically. If however the payment_action ‘Authorise and Capture’ was selected, then only the capture() method will be called and that method should probably authorise the payment before capturing the funds. A bit confusing and not explained anywhere but that’s how it works!

The Varien_Object $payment object holds all data to do with the transaction and also the order.

So that’s it! That’s the method I used to add a custom payment gateway to Magento. Please note: I don’t work with Magento anymore so if you have a complicated question which requires looking into Magento code, I probably won’t be able to answer it – but ask anyway just in case!

Comments Off on jQuery retrieving the data from an AJAX call into the global scope

Another quick one. Reading around it seems to be an issue. You make an AJAX call within a function and you want the call to be synchronous so that you can use the data from the call in the function scope.

AJAX calls are obviously asynchronous. jQuery has a ‘async: false’ option in AJAX calls but it is ignored by most (maybe all?) browsers. Using this method you can force a synchronous request and retrieve that data.

So .responseText retrieves the text value of the response from the AJAX call. And because we’re assigning to a variable we are forcing a synchronous request. You can now use that variable as you see fit.

This causes an issue when you are trying to retrieve a JSON object from an AJAX call into the function scope. The best way I’ve found so far is to retrieve the JSON as a string and use a jquery JSON plugin to convert it into an accessible object. Or you could just use the ‘eval’ method.

Quick one. A small snippet for performing custom SQL queries using symfony 1.2.3. The method to do this has varied slightly with the different symfony releases. The method found on the symfony site almost has a good example, this is just adding a bit to it (looping);

I was reading through some comments and posts on some incoming links and it seems it wasn’t working for anyone anymore. I just tested it myself and unfortunately it’s true (at least with with jQuery v1.3.1). However! All is not lost, there is another method which I’ve tested from one of the incoming links to that post which you can find here:

It’s definitely been too long since I posted here! I will try and post more regularly over the coming months…

Firstly, we came across this issue after writing a number of plugins for a symfony project. Each of these plugins had their own schema.yml files and therefore we didn’t need our own custom schema.yml file at that time.

Because of this, our config/schema.yml file looked like this:

propel:

Pretty, right? When we ran symfony propel:build-sql (after the other usual build tasks) we got the error:

Database "" does not exist.

In symfony’s glorious vibrant red error container. It seems when your schema file looks like ours did above, symfony generates the sqldb.map file for that file without a database name, our data/sql/sqldb.map file looked similar to this:

Which was stopping the build. After looking at various places, it seems this is a problem with Creole, (propel’s DBAL).

Before PHP v5.2.4, you could do a strtotime on a value of 000-00-00 00:00:00 and you would get a weird date, something in 1999. Not sure why :S Since the 5.2.4 update however this bug has been fixed and you will now get boolean false returned.

The symfony plugin sfGuard, which is widely used, uses 0000-00-00 00:00:00 as a default value in it’s schema for some fields and this will now cause problems. To fix this, edit sfGuard’s schema and change those default values to something like 1970-01-01 and that should do the trick.

I’ve been working over the weekend on a site and I got to the part of writing the client side MVC using jQuery (because I love the idea now!).

I found out that jQuery has a nice little method for loading external scripts into the page at run time called getScript(). It basically gets the contents of the external file using an AJAX request and then eval()s the code at runtime, which seems like a perfect way of doing it. I reckon it’s much better than appending new <script> tags to the <head> tag as well.

The only issue is that it can take some time to load an external file so you can’t always run functions from within that file straight away. Thankfully there is a workaround for this using another jQuery method. You basically set all AJAX request to be synchronous before you run getScript() and then set AJAX requests to be asynchronous again after the script is loaded. If you end up loading like 20 files at the beginning of a page load this could take a while but hopefully you won’t be doing that 😛