Laser card custom payment module for Magento

August 28th, 2009

While helping my brother out with bringing his sports shop online, I discovered Magento which is a pretty kick ass piece of php. One thing that Magento doesn’t have at the moment is a payment module for Irish Laser Debit cards, so I decided to write my own.

Some resources that I found useful were this and this, The first being from the Magento wiki and the second from a forum post.

The process is pretty straight forward once you get your head around all the config files, there are basically 10 files needed for the module (Coolbreeze is just the package name I gave the modules akin to the Mage group in magento) :

app/code/local/Coolbreese/LaserPayment/Block/Form.php
the controller for displaying the input form in the store and admin

app/code/local/Coolbreese/LaserPayment/Block/Info.php
the controller for displaying the card details in the store and admin

app/code/local/Coolbreese/LaserPayment/etc/config.xml
config stuff

app/code/local/Coolbreese/LaserPayment/etc/system.xml
more config stuff

app/code/local/Coolbreese/LaserPayment/Model/Laser.php
where the validation etc happens

app/design/adminhtml/default/default/template/laserpayment/form.phtml
the form template for the admin interface

app/design/adminhtml/default/default/template/laserpayment/info.phtml
the details display template for the admin interface

app/design/frontend/default/default/template/laserpayment/form.phtml
form display in the store

app/design/frontend/default/default/template/laserpayment/info.phtml
details display in the store

app/etc/modules/Coolbreeze_LaserPayment.xml
the main config file which tells magento that this module exist.

Here for your viewing pleasure is the module code to download.

To install it in a Magento installation just copy the app folder into the magento root and all the files will be put in the right place. Then go to admin -> System -> Configuration -> Payment Methods and enable the module.

Some cavaets :

I take no responsibility for breaking anything whatsoever so install this at your own risk or better yet look at the code and improve it.

The Laser number validation is pretty simple at the moment with no modulus checks. Something like this would probably be useful also as there is no client side validation just server side which works ok but is not ideal.

I’d love to hear feedback from anyone who uses this or finds it useful.

Windows 7 first impressions

January 27th, 2009

The first surprising thing about the Windows 7 beta is that it works great running in VMware, so if you have a relativly new vesion of VMware installed then it is well worth installing the beta to have a look.

The UI is distinctly vista like with the the round start buton and start menu, and similar dialogs, but for some reason it doesnt give off that hateful vibe that Vista does (having installed vista and immediatly removed it reverting back to trusty XP, Im no fan).

The beta comes with some nifty little apps like sticky notes and a great snipping tool for taking screen shots along with the usual games, calculator and of course paint (which is a nicely updated version in the vein of office 2007). It also comes with an IE 8 beta which may be of interest to some.

Overall i like the feel of Windows 7 and after seeing this beta I have high hopes of eventually upgrading my Windows XP machine to it.

So Microsoft, please dont f*$k it up before release.

Crop and resize an image using MiniMagick

January 21st, 2009

During my dealings with twitter avatars (see here) I discovered how to resize and crop an image using MiniMagick. This is useful when dealing with twitter avatars as the large size ones that the twitter api provides are not always square and the smaller ones may be too small for some applications. Below is the simple function :

def resize_and_crop(image, size)
  if image.width < image.height
    remove = ((image.height - image.width)/2).round
    image.shave("0x#{remove}")
  elsif image.width > image.height
    remove = ((image.width - image.height)/2).round
    image.shave("#{remove}x0")
  end
  image.resize("#{size}x#{size}")
  return image
end

Professor Farnsworth mindgames

January 21st, 2009

Pulling avatars from twitter in a Rails App

January 19th, 2009

Something which may be of use to all bloggers and social app builders is that it is pretty easy to pull someones twitter profile photo in RoR heres the code :

# call the twitter api
url = 'http://twitter.com/users/show/show.xml?email='+CGI.escape(test@test.test)

# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body

# extract profile image tag information
doc = REXML::Document.new(xml_data)
profile_image = nil
doc.elements.each('user/profile_image_url') do |ele|
        profile_image = ele.text
end

# if we have an image and its not a default avatar then continue
if profile_image and ! profile_image.include? "static.twitter.com"
	begin
		profile_image=profile_image.gsub("_normal","_bigger")
		filename="/tmp/"+rand(100000).to_s+File.extname(profile_image).downcase
		file=open(filename,"w")
		file.write(open(profile_image).read)
		file.close
		# do as you will with the avatar
	rescue
		#fail
	end
else
	#fail
end

Passing a parameter to a before filter

December 5th, 2008

Creating a controller filter that accepts a parameter is less than obvious as I found today while trying to add more than basic authentication to a controller.

Turns out that you need to do it like this :

before_filter :only => [:create, :update, :destroy] do |controller|
controller.filter_function(parameter)
end

rather than allowing a parameters array to pass into the before_filter function which would be nice.