Schedule exchange rate updates in Commerce

I was poking around n Quicksilver and I found an interesting piece of code for adding exchange rates to your site. In Quicksilver it is done once, during initialization, so I decided to create a scheduled job for it as rates tend to change 🙂 Pluggable, so you can add your own provider.

The scheduled job gets the exchange rates from the provider of your choice and adds it to Commerce the way it is explained/done in Quicksilver. You can either use one of the two providers I created, or create your own.  I have created one for currencylayer.com, which has a free plan for 1000 requests a month. Or you can use the one for fixer.io which is more limited, but you don’t need to subscribe.

If you want to create your own provider, you will need to implement the following interface

public interface IExchangeRateService
    {
        /// <summary>
        ///     Gets the exchange rates.
        /// </summary>
        /// <returns>ReadOnlyCollection&lt;CurrencyConversion&gt;.</returns>
        ReadOnlyCollection<CurrencyConversion> GetExchangeRates(out List<string> messages);
    }


or you can use the base class, that provides some methods for getting the currency name, which you need when adding a rate to Commerce

/// <summary>
        ///     Gets the name of the currency.
        /// </summary>
        /// <param name="isoCurrencySymbol">The ISO currency symbol.</param>
        /// <returns>System.String.</returns>
        protected string GetCurrencyName(string isoCurrencySymbol)
        {
            RegionInfo currencyRegion =
                this.RegionsInfo.FirstOrDefault(
                    r => r.ISOCurrencySymbol.Equals(isoCurrencySymbol, StringComparison.OrdinalIgnoreCase));

            return currencyRegion == null ? isoCurrencySymbol : currencyRegion.CurrencyEnglishName;
        }

        /// <summary>
        ///     Gets the regions.
        /// </summary>
        /// <returns>List&lt;RegionInfo&gt;.</returns>
        private ReadOnlyCollection<RegionInfo> GetRegions()
        {
            List<RegionInfo> regions = new List<RegionInfo>();
            CultureInfo[] cultures;

            try
            {
                cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
            }
            catch (ArgumentOutOfRangeException argumentOutOfRangeException)
            {
                this.Log.Error("[Exchange Rates : Service] Error getting culture info", argumentOutOfRangeException);
                return new ReadOnlyCollection<RegionInfo>(regions);
            }

            //loop through all the cultures found
            foreach (CultureInfo culture in cultures)
            {
                //pass the current culture's Locale ID (http://msdn.microsoft.com/en-us/library/0h88fahh.aspx)
                //to the RegionInfo constructor to gain access to the information for that culture
                try
                {
                    if (culture.IsNeutralCulture)
                    {
                        continue;
                    }

                    RegionInfo region = new RegionInfo(culture.LCID);
                    regions.Add(region);
                }
                catch (ArgumentException argumentException)
                {
                    this.Log.Error("[Exchange Rates : Service] Error adding region info for: {0}", culture.EnglishName, argumentException);
                }
            }

            return new ReadOnlyCollection<RegionInfo>(regions);
        }

How the exchange rates are added to Commerce you can see in Quicksilver. I used that code in the scheduled job.

You can find the complete code on GitHub or install the package from NuGet.

Same goes for the providers: Currency  layer, Fixer

Leave a comment