Currency Convertor Tutorial

Ever wanted to know how much something was in another currency? Now you can with this short and sweet piece of code?

This tutorial is very simple to implement and has endless uses. The biggest benefit will be to those running e-commerce stores who ship worldwide and want to get the latest exchange rates. The method uses Web Services to consume the latest exchange rate into your application. A web service is simply data that can be gathered and manipulated by your application.

The converter uses a free web service from www.xmethods.net which supplies literally thousands of different web services that you can just gobble up. For now though we are only interested in this one: http://www.xmethods.net/ve2/ViewListing.po?key=uuid:D784C184-99B2-DA25-ED45-3665D11A12E5

To get going, create a new page called exchange.cfm

Here's the code in full:

<form action="exchange.cfm?do" method="post" name="form">

Change this amount:

<input type="text" name="amount">

into

<select name="currency" size="1">

<option value="united states">US dollars</option>

<option value="euro">Euros</option>

</select>

<br><br>

<input type="submit" value="Convert it!">

</form>

<p>&nbsp;</p>

<cfif IsDefined("url.do")>

<cfinvoke

webservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"

method="getRate"

returnvariable="aRate">

<cfinvokeargument name="country1" value="united kingdom"/>

<cfinvokeargument name="country2" value="#form.currency#"/>

</cfinvoke>

<cfoutput>

You entered <b>#form.amount#</b> which is worth #NumberFormat(Evaluate(amount * aRate), '.__')# in #form.currency# currency

</cfoutput>

</cfif>

OK lets break it down into chunks:

<form action="exchange.cfm?do" method="post" name="form">

Change this amount:

<input type="text" name="amount">

into

<select name="currency" size="1">

<option value="united states">US dollars</option>

<option value="euro">Euros</option>

</select>

<br><br>

<input type="submit" value="Convert it!">

</form>

This just sets up a simple form for you to enter the value you want to change and choose the currency you want to change it to. The full list of countries is show at the same location as the web service above.

<cfif IsDefined("url.do")>

<cfinvoke

webservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"

method="getRate"

returnvariable="aRate">

<cfinvokeargument name="country1" value="united kingdom"/>

<cfinvokeargument name="country2" value="#form.currency#"/>

</cfinvoke>

OK the code here checks to see if the form has been submitted and calls the web service using the CFINVOKE tag. The two arguments are the currency you are converting from and to. The latter is set by our form.

<cfoutput>

You entered <b>#form.amount#</b> which is worth #NumberFormat(Evaluate(amount * aRate), '.__')# in #form.currency# currency

</cfoutput>

</cfif>

Finally we display the conversion by multiplying our entered amount by the exchange rate and format the number so we don't get loads of trailing zeros.

That's it! You can take this forward by adding more currencies, save the rate in a session var to stop multiple request, etc but for now you have a working currency converter!

About This Tutorial
Author: Phil Williams
Skill Level: Beginner 
 
 
 
Platforms Tested: CFMX
Total Views: 133,243
Submission Date: April 21, 2004
Last Update Date: June 05, 2009
All Tutorials By This Autor: 8
Discuss This Tutorial
  • Hi, I was wondering if and how this method could be modified to work in CF5 as cfinvoke is not supported in CF5 Thanks for any help Hooman

  • Just wanted to say thanks. Really helped! Cheers, David

  • Hi I have identified an issue with the tutorial and I am currently rewriting it to use a more robust feed. Watch this space...

  • Hi, I'm testing the service posted in this tutorial and I get this error: Could not perform web service invocation "getRate" because AxisFault faultCode: http://xml.apache.org/axis/:Server.userException faultString: org.xml.sax.SAXParseException: Missing whitespace before SYSTEM literal URI. faultActor: null faultDetail Why? You can view the page with the entire error at: www.tropicaltours.com.mx, in the "Tipos de Cambio" link. Any help will be strongly apreciated.

Advertisement

Sponsored By...
Dripping Springs, Texas - Deep Tissue Massages and Swedish Massages!