[thelist] multinational pricing

Glen Pike postmaster at glenpike.co.uk
Wed Sep 3 12:53:21 CDT 2008


Hi,

    To do currency conversion, you can use a webservice to obtain the 
exchange rate at the time you want.

    http://xmethods.net/ has a list of webservices - they have a 
currency one here that they used to proxy.

    In order to avoid excessive calls to a webservice, one of the sites 
I built did this once a day:

    We stored a list of "countries" in a database.  When the time came 
to update the currency exchange rates, we ran a script which listed each 
currency and got the exchange rate for our currency from the 
webservice.  The results were stored in the database so we always had a 
fairly up-to-date value.

    Here is some example code in PHP (not sure the webservice address or 
WSDL is correct, but you can find the values from the xmethods site).  
This also uses the PHP Pear libraries for handling webservice requests.
  
    The issues here are incorrect / not up-to-date values so your client 
needs to know that there is a risk they could be charging too little / 
too much.  Also, you may find that the webservice is not free to use for 
certain purposes.

    Hope this helps.

    Glen

<?php  //WebService_CurrencyExchangeService.class.php

require_once("SOAP/Client.php");
 
class WebService_CurrencyExchangeService extends SOAP_Client
{
    function WebService_CurrencyExchangeService($path = 
'http://services.xmethods.net:9090/soap')
    {
        $this->SOAP_Client($path, 0);
    }
    function &getRate($country1, $country2)
    {
        return $this->call('getRate',
                           $v = array('country1' => $country1, 
'country2' => $country2),
                           array('namespace' => 
'urn:xmethods-CurrencyExchange',
                                 'soapaction' => '',
                                 'style' => 'rpc',
                                 'use' => 'encoded'));
    }
}

?>

Here is a code snippet from the update function:

/**
     *  function:   _autoUpdate...
     *  params:     none
     *  returns:    Boolean - TRUE if the update worked, FALSE if not.
     *  does:       Attempts to update the exchange rates in the system 
for each currency in the list of currencies
     *              It does this by using a webservice to obtain the 
currency information and extract results..
     *
     */
     function _autoUpdate() {
        $result = TRUE;
        $this->errorMessage = "Could not complete the autoUpdate 
because:\n";
       
        //List the currencies in our table.
        $currencies = $this->listCurrencies();
       
        if(FALSE == $currencies) {
            $this->errorMessage .= 'I could not get a list of the 
currencies from the database.';
            return FALSE;
        }
       
        //For each currency
        foreach($currencies as $currency) {           
            //Get the CurrencyCode
            //Ask the webservice to provide the exchange rate for our code..
            $rate = $this->_doWebServiceRequest('uk', 
$currency['CountryCode']);
            //If this worked, try to update the currency information.
            //Otherwise retain an error message...
            if(FALSE == $rate) {
                $this->errorMessage .= "requesting rate for country '" 
.$currency['CountryCode'] ."' returned an error\n<br>";
                $this->errorMessage .= "Maybe the \"webservice\" server 
is down\n";
               
                $result = FALSE;
            } else {
                $currency['ExchangeRate'] = $rate;
                if(FALSE == $this->modifyCurrency($currency)) {
                    $this->errorMessage .= "modifying rate for country 
'" .$currency['CountryCode'] ."' returned an error\n";
                    $result = FALSE;   
                }   
            }
        }
        return $result;
           
     }
     
     function _doWebServiceRequest($fromCountry, $toCountry) {
            require_once "WebService_CurrencyExchangeService.php.inc";
            //require_once('nusoap.php');

            // Define needed parameters and put them in an array..
            $parameters = array(
                'country1'         => $fromCountry,
                'country2'         => $toCountry
            );   

            // Define new object and specify location of wsdl file.
            //"http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"
            //$soapclient = new soapclient('Currency.wsdl','wsdl');
            //$soapclient = new SOAP_Client('Currency.wsdl', true);
            $soapclient = new WebService_CurrencyExchangeService();
            // call the method and get the result.
            //$result = $soapclient->call('getRate',$parameters);
            $result = $soapclient->getRate($fromCountry, $toCountry);
            if(PEAR::isError($result)) {
                trigger_error("soap return is " 
.var_export($result->getDetail(), TRUE));
                return FALSE;
            }
           
            return $result;
    }



graham.hays wrote:
> Hi all
>
> I have a client who offers services paid via PayPal currently in pounds
> sterling and euros with the prices hard coded in to the site (only a small
> number of prices and rare price changes).
>
> The client now wants to add US$ prices (and possibly other currencies) - so
> I'd like to change the site to dynamically reference exchange rates over the
> net and display prices in the currency selected by customers.
>
> Any pointers/suggestions about obtaining the rates etc.
>
> Regards
>
> Graham
>
>   

-- 

Glen Pike
01326 218440
www.glenpike.co.uk <http://www.glenpike.co.uk>




More information about the thelist mailing list