Interspire Shopping Cart Currency Exchange Rate Modules
Interspire Shopping Cart 3.5 Required
This documentation has been written for the upcoming release of Interspire Shopping Cart 3.5. There are numerous changes between Interspire Shopping Cart 3.5 and earlier releases for modules.
Contents
Introduction
Currency exchange rate modules allow for additional services that provide currency exchange rate updates to be tied in to the currency system. Currency exchange modules are called by Interspire Shopping Cart when a store administrator updates the store currencies in the control panel or the update is performed automatically using a cronjob.
This document continues on from the Interspire Shopping Cart Modules Documentation which provides an overview of how Interspire Shopping Cart modules work and their structure.
Currency exchange rate modules will generally contact a third party service via the web to return an updated exchange rate between two currencies.
Currency exchange rate modules exist within the /modules/currency/ directory and extend upon the base ISC_CURRENCY class.
Callbacks
FetchExchangeRate
Arguments:
- The currency code to use as the base rate for conversion. ($fromCurrency)
- The currency code of the currency that we wish to fetch the exchange rate for based on the above currency code. ($toCurrency)
Returns: Returns the exchange rate between the two currencies or false if there was an error retrieving the exchange rate.
The FetchExchangeRate method is called when the exchange rate between two different currency codes needs to be calculated. This method should contact the third party currency service and calculate the exchange rate for one unit between the from currency and to currency arguments.
If there was a problem retrieving the exchange rate, this function should return false. It may also call $this->SetError() to set any error messages that should be displayed to the user.
An example callback that calls the WebServiceX (http://www.webservicex.net) service to calculate the exchange rate between two currencies is as follows:
protected function FetchExchangeRate($fromCode, $toCode)
{
// The fields to post
$postFields = "FromCurrency=" . urlencode(strtoupper($fromCode)) . "&ToCurrency=" . urlencode(strtoupper($toCode));
$rtn = PostToRemoteFileAndGetResponse("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate", $postFields);
// If we have failed then there is nothing really useful you can tell the client other than this service is temporarily unavailable
if (!$rtn) {
$this->SetError(GetLang("CurrencyProviderRequestUnavailable"));
return false;
}
// Now we parse the return XML.
$xml = simplexml_load_string($rtn);
if(!is_object($xml)) {
$this->SetError(GetLang("CurrencyProviderRequestInvalidCode"));
return false;
}
if (count($xml) === 0) {
return (double)$xml;
} else {
return (double)$xml[0];
}
}
