MooTools: Class Methods and Inheritance

Posted by anup.narkhede on July 12, 2010

In my last project, I enjoyed scripting using mootools and started exploring it more deeply. One of my extensive use was to define UI elements’ functionality in classes and then create multiple instances for re-usability.

Mootools makes use of Javascript’s native prototype model for inheritance. To extend a base functionality into sub class, mootools provide ‘Extends’ method. However, if you have class methods in your base model, Extends is insufficient to get them into the sub classes. After some research, I managed to solve this by using Class Mutators and overriding the Mootools ‘Extends’ to take care of class methods inheritance.

First of all, we need to define a ClassMethods mutator. Mutators belong to a class, not to their instances and they are called only when we define a new Class.

Next, we need to redefine Mootools Extends mutator:

Check out the example below:

You can see the above code working here.
http://jsfiddle.net/rZvJE/11/

Continuous Authority and repeated transactions using SagePay Gateway

Posted by anup.narkhede on February 04, 2010

SagePay allows us to process repeated capture transactions against a successful payment authorization. You can get more information about the introduction and instructions for obtaining a Continuous Authority Internet Merchant Number here.

This example makes use of a forked version of ActiveMerchant and SagePay Simulator environment. Please refer to the previous article for setup instructions.

Setup:

Make sure to check ‘Continuous Authority’ in the simulator account settings.

Case 1: A non 3D Authorized Transaction

response.authorization shows a value simiar to:
00001;{E5AC4385-06F8-40DD-8486-22EFB23768AE};9030;03FQG5AJA6;authorization

Capture the Payment

Repeat the transaction using previous authorization reference.

Case 2: 3D Authorized transaction

Follow the steps 1-3 for 3D Secure Payment Transaction as shown in the previous article. You’ll notice that the authorization returned by a three_d_complete process is incomplete and it looks something like “;{FB448BBF-CB72-414A-B293-316004162EEB};6598;OUWEBUCWL3;three_d_complete”
Prepend the order_id used in step 1 to this authorization reference so that it matches following format:

three_d_auth_reference = “00003;{FB448BBF-CB72-414A-B293-316004162EEB};6598;OUWEBUCWL3;three_d_complete”

Where “00003″ is the order_id used for authorization in step 1.

Capture this 3D Authorized payment for the first time by calling:

This transaction can be repeated by using the previous successful authorization (three_d_auth_reference) with a new order_id.

3D Secure Transactions using SagePay Gateway and ActiveMerchant

Posted by anup.narkhede on December 25, 2009

Introduction

What is 3D secure?

3D Secure is a latest initiative for fraud prevention launched by Visa and MasterCard. 3D Secure adds additional password authentication step to complete the online transactions. A detailed introduction is available at http://www.sagepay.com/developers/industry_knowledge/3d_secure.asp.
This tutorial is a guide to set up a Ruby on Rails test environment for 3D Secure payment transactions using ActiveMerchant plugin.

And why should we care?

3D Secure is becoming an industry standard and it is mandatory for processing Mastero card payments. Also, the deadline for 3-D Secure mandate (United Kingdom) for Maestro transactions is January 31st, 2010 and we want to make sure our applications are compliant by that date.

How does it work?


Fig 1: 3D Secure transaction

Setup

1. Patch ActiveMerchant plugin

To get this working, apply this patch to add 3D Secure support for SagePay gateway. The patch is taken from http://github.com/tekin/active_merchant, however it is modified for SagePayGateway instead of ProtxGateway. Alternately, you can use http://github.com/dynamic50/active_merchant to follow this tutorial.

2. Set up SagePay simulator environment

Apply for a simulator account at https://support.sagepay.com/apply/. The registration process is instant and the support team is quick in responding to your queries. Once your account is created, log on to configure the simulator for direct messages.

Clicking on the “Direct” button takes us to Direct Options and parameters admin page.

Set response for authorization POSTs to 3DAUTH as shown below. In short, we are forcing the response for all validate requests to be 3DAUTH.

http://whatismyip.com for help.

Also, select the “Simulate Direct” checkbox.

In config/environments/development.rb

Make a note that test and simulator are different environments for SagePay payment gateway.

3D Secure Payment Transaction

A typical 3D secure transaction works in four steps as shown in Fig 1. Initialize the gateway and a dummy valid credit card:

Step 1: Authorize

Note: the amount is in pence and order_id can be any unique identifier for your transaction.
The response of this request gives us the PAReq, MD and ACSURL values:

@three_d_secure=true and @success=false imply that we need to complete the 3DSecure authorization first.

Step 2: 3D Authorization

To initiate the 3D Secure authorization, we should post PAReq, MD and the callback url (TermURL) to ACSURL.
We can do it using curl command as:

The response is a html page, captured in temp.html. Edit it and pick up the VPSTxID.
The simulator allows us to send different responses for multiple scenarios. We assume that we have entered correct password and we want to simulate a successful 3D transaction. In this case, we respond to this action by clicking the ‘OK’ button.
This can be done as:

Essentially, we have simulated a “correct 3d password submit” action. The gateway should return a PARes token (for the initiated PAReq value) which can be found out from temp.html.

Step 3: Complete 3D authorization

On the console, call:

The response should be similar to:

@message=”Success” confirms that the 3D authorization was successful. As a last step, we need to complete the transaction by capturing this payment.

Step 4: Capture Payment

SagePay requires you to remember the order_id that you used to initiate the authorization. Get the authorization code from previous response and prefix it with the order id.

Ex: =”a6f8c776ac58dcf08;{FB448BBF-CB72-414A-B293-316004162EEB};6598;OUWEBUCWL3;three_d_complete”
Where order_id=’a6f8c776ac58dcf08′

The aim of this tutorial was to demonstrate a 3D secure payment transaction with details of the tokens exchanged between gateway and the client application. On these lines, we can extend rails support for other gateways to handle 3D Secure authentication. Please leave your comments with suggestions or issues.

Using AutoComplete with ActiveScaffold forms

Posted by anup.narkhede on July 01, 2009

This is a quick guide to set up auto_complete text fields in forms rendered by ActiveScaffold plugin.
I am not a huge fan of ActiveScaffold, but found these steps worth publishing after doing this for one typical project requirement.

1. Models

2. Install ActiveScaffold and AutoComplete Plugins

3. Controller Configuration

This renders a drop down list for country field, instead of the default create/replace form. However, we need to render an auto completing text box for which we need to replace the form column.

4. Form column override

Next step is to override the form column. Create a file named _country_form_column.html.erb in app/views/cities folder.

The second option :method => :get is needed to avoid the InvalidAuthenticityToken error generated by the ajax post request.

5. Since the form now returns country[name] instead of record[country_id] in params, we need to assign country object before creating city record. The CitiesController finally looks like:

Tips: Replace Missing Images using JQuery

Posted by anup.narkhede on March 26, 2009

Checking for non-existing images and replacing them by placeholders is simple when the images are handled on server (db/filestore). However, for externally linked images, we can do something like this using JQuery:

This script will replace the broken image urls with ‘/images/noimage.jpg’.