/**
  * Retrieved from reference on Jul 6, 2012
  * http://fasteri.com/list/country-code-dialing-code-currency-timezone-of-countries.html
  */
 static function getNationalCurrency($country)
 {
     $country = substr($country, 0, 2);
     $country = strtoupper($country);
     $nationalCurrencies = NationalCurrencies::getNationalCurrencies();
     if (isset($nationalCurrencies[$country])) {
         return $nationalCurrencies[$country];
     } else {
         return NULL;
     }
 }
 /**
  * Called when a currency code error exists. If a fallback currency
  * conversion is enabled for this adapter, convert intended amount to
  * default currency.
  *
  * @throws DomainException
  */
 protected function fallbackToDefaultCurrency()
 {
     $adapterClass = $this->gateway->getGatewayAdapterClass();
     $defaultCurrency = null;
     if ($this->gateway->getGlobal('FallbackCurrencyByCountry')) {
         $country = $this->getVal('country');
         if ($country !== null) {
             $defaultCurrency = NationalCurrencies::getNationalCurrency($country);
         }
     } else {
         $defaultCurrency = $this->gateway->getGlobal('FallbackCurrency');
     }
     if (!$defaultCurrency) {
         return;
     }
     // Our conversion rates are all relative to USD, so use that as an
     // intermediate currency if converting between two others.
     $oldCurrency = $this->getVal('currency_code');
     if ($oldCurrency === $defaultCurrency) {
         throw new DomainException(__FUNCTION__ . " Unsupported currency {$defaultCurrency} set as fallback for {$adapterClass}.");
     }
     $oldAmount = $this->getVal('amount');
     $usdAmount = 0.0;
     $newAmount = 0;
     $conversionRates = CurrencyRates::getCurrencyRates();
     if ($oldCurrency === 'USD') {
         $usdAmount = $oldAmount;
     } elseif (array_key_exists($oldCurrency, $conversionRates)) {
         $usdAmount = $oldAmount / $conversionRates[$oldCurrency];
     } else {
         // We can't convert from this unknown currency.
         return;
     }
     if ($defaultCurrency === 'USD') {
         $newAmount = floor($usdAmount);
     } elseif (array_key_exists($defaultCurrency, $conversionRates)) {
         $newAmount = floor($usdAmount * $conversionRates[$defaultCurrency]);
     }
     $this->setVal('amount', $newAmount);
     $this->setVal('currency_code', $defaultCurrency);
     $this->logger->info("Unsupported currency {$oldCurrency} forced to {$defaultCurrency}");
     // We have a fallback, so let's revalidate.
     $this->getValidationErrors(true);
     $notify = $this->gateway->getGlobal('NotifyOnConvert');
     // If we're configured to notify, or if there are already other errors,
     // add a notification message.
     if ($notify || !empty($this->validationErrors)) {
         $error['general'] = MessageUtils::getCountrySpecificMessage('donate_interface-fallback-currency-notice', $this->getVal('country'), $this->getVal('language'), array($this->gateway->getGlobal('FallbackCurrency')));
         $this->gateway->addManualError($error);
     }
 }