/** * 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); } }
/** * getErrorMessage - returns the translated error message appropriate for a * validation error on the specified field, of the specified type. * @param string $field - The common name of the field containing the data * that is causing the error. * @param string $type - The type of error being caused, from a set. * Possible values are: * 'not_empty' - the value is required and not currently present * 'valid_type' - in general, the wrong format * 'calculated' - fields that failed some kind of multiple-field data * integrity check. * @param string $language MediaWiki language code * @param string $country ISO country code * @return String */ public static function getErrorMessage($field, $type, $language, $country = null) { //this is gonna get ugly up in here. //error_log( __FUNCTION__ . " $field, $type, $value " ); //NOTE: We are just using the next bit because it's convenient. //getErrorToken is actually for something entirely different: //Figuring out where on the form the error should land. $message_field = self::getErrorToken($field); if ($field === 'expiration') { ///the inevitable special case. $message_field = $field; } //postal code is a weird one. More L10n than I18n. //'donate_interface-error-msg-postal' => 'postal code', $error_message_field_key = 'donate_interface-error-msg-' . $message_field; if ($country !== null) { $translated_field_name = MessageUtils::getCountrySpecificMessage($error_message_field_key, $country, $language); } else { if (WmfFramework::messageExists($error_message_field_key, $language)) { $translated_field_name = WmfFramework::formatMessage($error_message_field_key); } else { $translated_field_name = false; } } //Empty messages should get: //'donate_interface-error-msg' => 'Please enter your $1'; //If they have no defined error message, give 'em the default. if ($type === 'not_empty') { if ($message_field != 'general' && $translated_field_name) { return WmfFramework::formatMessage('donate_interface-error-msg', $translated_field_name); } } if ($type === 'valid_type' || $type === 'calculated') { //NOTE: We are just using the next bit because it's convenient. //getErrorToken is actually for something entirely different: //Figuring out where on the form the error should land. $token = self::getErrorToken($field); $error_key_calc = 'donate_interface-error-msg-' . $token . '-calc'; if ($type === 'calculated') { // try for the special "calculated" error message. if (WmfFramework::messageExists($error_key_calc, $language)) { return WmfFramework::formatMessage($error_key_calc); } } //try for new more specific default correction message if ($message_field != 'general' && $translated_field_name && WmfFramework::messageExists('donate_interface-error-msg-field-correction', $language)) { return WmfFramework::formatMessage('donate_interface-error-msg-field-correction', $translated_field_name); } } //ultimate defaultness. return WmfFramework::formatMessage('donate_interface-error-msg-general'); }
/** * Get a message value specific to the donor's country and language. * * @param array $params first value is used as message key * TODO: use the rest as message parameters * @return string */ public static function l10n($params) { if (!$params) { throw new BadMethodCallException('Need at least one message key'); } $language = RequestContext::getMain()->getLanguage()->getCode(); $key = array_shift($params); return MessageUtils::getCountrySpecificMessage($key, Gateway_Form_Mustache::$country, $language, $params); }
public function testGetCountrySpecificMessage() { $actual = MessageUtils::getCountrySpecificMessage('donate_interface-donor-fiscal_number', 'BR', 'pt'); $expected = wfMessage('donate_interface-donor-fiscal_number-br')->inLanguage('pt')->text(); $this->assertEquals($expected, $actual, 'Not using the country specific message'); }