I think in this case I would suggest you do the conversion in the payment provider as allowing the stored code to be changeable could have adverse effects on code that expects it in the 2 letter format (ie, I think consistency is more beneficial than flexibility here).
With that being the case you have 2 options really. Store a hard coded lookup table of 2 to 3 letter ISO3166 codes. Or potentially look at using CultureInfo to do it dynamically
public string ConvertTwoLetterNameToThreeLetterName(string name)
{
if (name.Length != 2)
{
throw new ArgumentException("name must be two letters.");
}
name = name.ToUpper();
var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (var culture in cultures)
{
var region = new RegionInfo(culture.LCID);
if (region.TwoLetterISORegionName.ToUpper() == name)
{
return region.ThreeLetterISORegionName;
}
}
return null;
}
3 char ISO country codes
Hi Matt
It seems we can only specify the 2 char country code? The payment gateway we are working with requires the 3 char code.
I can do a lookup as we're only targeting UK at the moment and then possibly USA soon after. But I can see it being a problem for global sites.
Hi Sean,
I think in this case I would suggest you do the conversion in the payment provider as allowing the stored code to be changeable could have adverse effects on code that expects it in the 2 letter format (ie, I think consistency is more beneficial than flexibility here).
With that being the case you have 2 options really. Store a hard coded lookup table of 2 to 3 letter ISO3166 codes. Or potentially look at using
CultureInfo
to do it dynamicallyThis is modified from a stack overflow post which has some good other alternatives too https://stackoverflow.com/questions/4884692/converting-country-codes-in-net
/Matt
is working on a reply...