/**
  * Get a translated text from the language pack
  *
  * @param string     $text     the string to be translated
  * @param string|int $language target language, iso code or KlarnaLanguage
  *
  * @return string  the translated text
  */
 public function fetch($text, $language)
 {
     if (is_numeric($language)) {
         $language = KlarnaLanguage::getCode($language);
     } else {
         $language = strtolower($language);
     }
     // XPath query to get translation
     $xpath = "//string[@id='{$text}']/{$language}";
     $aResult = (array) @$this->_xml->xpath($xpath);
     if (count($aResult) > 0) {
         return strval($aResult[0]);
     }
     // Fallback to the english text
     if ($language != 'en') {
         return $this->fetch($text, 'en');
     }
     // Or failing that, the placeholder
     return $text;
 }
 /**
  * {@inheritDoc}
  */
 protected function populateConfig(ArrayObject $config)
 {
     if (!class_exists('KlarnaCurrency')) {
         throw new \LogicException('You must install "fp/klarna-invoice" library.');
     }
     $config->defaults(array('payum.factory_name' => 'klarna_invoice', 'payum.factory_title' => 'Klarna Invoice', 'sandbox' => true, 'pClassStorage' => 'json', 'pClassStoragePath' => './pclasses.json', 'xmlRpcVerifyHost' => 2, 'xmlRpcVerifyPeer' => true, 'payum.action.capture' => new CaptureAction(), 'payum.action.authorize' => new AuthorizeAction(), 'payum.action.status' => new StatusAction(), 'payum.action.sync' => new SyncAction(), 'payum.action.refund' => new RefundAction(), 'payum.action.api.activate' => new ActivateAction(), 'payum.action.api.activate_reservation' => new ActivateReservationAction(), 'payum.action.api.cancel_reservation' => new CancelReservationAction(), 'payum.action.api.check_order_status' => new CheckOrderStatusAction(), 'payum.action.api.get_addresses' => new GetAddressesAction(), 'payum.action.api.populate_klarna_from_details' => new PopulateKlarnaFromDetailsAction(), 'payum.action.api.credit_invoice' => new CreditInvoiceAction(), 'payum.action.api.credit_part' => new CreditPartAction(), 'payum.action.api.reserve_amount' => new ReserveAmountAction(), 'payum.action.api.return_amount' => new ReturnAmountAction(), 'payum.action.api.email_invoice' => new EmailInvoiceAction(), 'payum.action.api.send_invoice' => new SendInvoiceAction(), 'payum.action.api.update' => new UpdateAction()));
     if (false == $config['payum.api']) {
         $config['payum.default_options'] = array('eid' => '', 'secret' => '', 'country' => '', 'language' => '', 'currency' => '', 'sandbox' => true);
         $config->defaults($config['payum.default_options']);
         $config['payum.required_options'] = array('eid', 'secret', 'country', 'language', 'currency');
         $config->defaults(array('sandbox' => true));
         $config['payum.api'] = function (ArrayObject $config) {
             $config->validateNotEmpty($config['payum.required_options']);
             $config['mode'] = $config['sandbox'] ? \Klarna::BETA : \Klarna::LIVE;
             if (null === ($country = \KlarnaCountry::fromCode($config['country']))) {
                 throw new LogicException(sprintf('Given %s country code is not valid. Klarna cannot recognize it.', $config['country']));
             }
             if (null === ($language = \KlarnaLanguage::fromCode($config['language']))) {
                 throw new LogicException(sprintf('Given %s language code is not valid. Klarna cannot recognize it.', $config['language']));
             }
             if (null === ($currency = \KlarnaCurrency::fromCode($config['currency']))) {
                 throw new LogicException(sprintf('Given %s currency code is not valid. Klarna cannot recognize it.', $config['currency']));
             }
             $klarnaConfig = new Config();
             $klarnaConfig->eid = $config['eid'];
             $klarnaConfig->secret = $config['secret'];
             $klarnaConfig->mode = $config['mode'];
             $klarnaConfig->country = $country;
             $klarnaConfig->language = $language;
             $klarnaConfig->currency = $currency;
             $klarnaConfig->pClassStorage = $config['pClassStorage'];
             $klarnaConfig->pClassStoragePath = $config['pClassStoragePath'];
             $klarnaConfig->xmlRpcVerifyHost = $config['xmlRpcVerifyHost'];
             $klarnaConfig->xmlRpcVerifyHost = $config['xmlRpcVerifyHost'];
             return $klarnaConfig;
         };
     }
 }
 /**
  * get the language of the locale
  *
  * @return int language constant
  */
 public function getLanguage()
 {
     return KlarnaLanguage::fromCode($this->_language);
 }
 /**
  * Converts a KlarnaLanguage constant to the respective language code.
  *
  * @param int $val KlarnaLanguage constant
  *
  * @return lowercase string|null
  */
 public static function getCode($val)
 {
     if (self::$_languageFlip === array()) {
         self::$_languageFlip = array_flip(self::$_languages);
     }
     if (array_key_exists($val, self::$_languageFlip)) {
         return strtolower(self::$_languageFlip[$val]);
     }
     return null;
 }
Example #5
0
 /**
  * Returns the {@link KlarnaLanguage language} constant from the language code.
  *
  * @param  string  $code  Two letter code, e.g. "da", "de", etc.
  * @throws KlarnaException
  * @return int  {@link KlarnaLanguage Language} constant.
  */
 public static function getLanguageForCode($code)
 {
     $language = KlarnaLanguage::fromCode($code);
     if ($language === null) {
         throw new KlarnaException('Error in ' . __METHOD__ . ': Unknown language! (' . $code . ')', 50003);
     }
     return $language;
 }
Example #6
0
 /**
  * Returns the {@link KlarnaLanguage language} constant from the language code.
  *
  * @param string $code Two letter code, e.g. "da", "de", etc.
  *
  * @throws KlarnaException
  * @return int  {@link KlarnaLanguage Language} constant.
  */
 public static function getLanguageForCode($code)
 {
     $language = KlarnaLanguage::fromCode($code);
     if ($language === null) {
         throw new Klarna_UnknownLanguageException($code);
     }
     return $language;
 }
Example #7
0
 function getKlarnaAddress()
 {
     if (JVM_VERSION >= 2) {
         require_once JPATH_ROOT . DS . 'plugins' . DS . 'vmpayment' . DS . 'klarna' . DS . 'klarna.php';
         require_once JPATH_ROOT . DS . 'plugins' . DS . 'vmpayment' . DS . 'klarna' . DS . 'klarna' . DS . 'api' . DS . 'klarnaaddr.php';
     } else {
         require_once JPATH_ROOT . DS . 'plugins' . DS . 'vmpayment' . DS . 'klarna.php';
         require_once JPATH_ROOT . DS . 'plugins' . DS . 'vmpayment' . DS . 'klarna' . DS . 'api' . DS . 'klarnaaddr.php';
     }
     $klarna = new Klarna();
     $q = "select * from #__virtuemart_paymentmethods where payment_element = 'klarna' and published = '1' limit 0,1";
     $db = JFactory::getDBO();
     $db->setQuery($q);
     $res = $db->loadAssoc();
     if (empty($res)) {
         return null;
     }
     $id = $res['virtuemart_paymentmethod_id'];
     jimport('joomla.html.parameter');
     $params = explode('|', $res['payment_params']);
     $obj = new stdclass();
     foreach ($params as $item) {
         $item = explode('=', $item);
         $key = $item[0];
         unset($item[0]);
         $item = implode('=', $item);
         if (!empty($item)) {
             $obj->{$key} = @json_decode($item);
         }
     }
     $cData = KlarnaHandler::countryData($obj, 'SWE');
     $language = KlarnaLanguage::fromCode('SE');
     $currency = KlarnaCurrency::fromCode($cData['currency_code']);
     $klarna->config($cData['eid'], $cData['secret'], $cData['country_code'], $language, $currency, $cData['mode']);
     /*
     	$country = JRequest::getVar('virtuemart_country_id', ''); 
     	
     	if (!empty($country) && (is_numeric($country)))
     {
       $q = 'select * from #__virtuemart_countries where virtuemart_country_id = '.$country.' limit 0,1'; 
       $db->setQuery($q); 
       $r = $db->loadAssoc(); 
       $e = $db->getErrorMsg(); 
       
       if (empty($r)) $c = 'se';
       else
       $c = strtolower($r['country_2_code']); 
       
     }
     else 
     */
     $c = 'se';
     $klarna->setCountry($c);
     $klarna->setLanguage($language);
     $klarna->setCurrency($currency);
     //Attempt to get the address(es) associated with the SSN/PNO.
     $pn = JRequest::getVar('socialNumber', '');
     $addrs = $klarna->getAddresses($pn);
     if (empty($addrs)) {
         return null;
     }
     $a = array();
     foreach ($addrs as $key => $addr) {
         $a = $addr->toArray();
         foreach ($a as $k => $v) {
             $a[$k] = utf8_encode($v);
         }
         return $a;
         //if (empty($ar)) return null;
         if ($addr->isCompany) {
             $a['company_name'] = $addr->getCompanyName();
         } else {
             $a['company_name'] = '';
         }
         $a['first_name'] = $addr->getFirstName();
         $a['last_name'] = $addr->getLastName();
         $a['address_1'] = $addr->getStreet();
         $a['email'] = $addr->getEmail();
         $a['phone_1'] = $addr->getTelno();
         $a['phone_2'] = $addr->getCellno();
         $a['address_2'] = $addr->getHouseExt();
         $a['zip'] = $addr->getZipCode();
         $a['city'] = $addr->getCity();
         return $a;
     }
     return null;
     /* If there exists several addresses you would want to output a list in 
          which the customer could choose the address which suits him/her. 
        */
     // Print them if available:
     foreach ($addrs as $key => $addr) {
         echo "<table>\n";
         // This only works if the right getAddresses type is used.
         if ($addr->isCompany) {
             echo "\t<tr><td>Company</td><td> {$addr->getCompanyName()} </td></tr>\n";
         } else {
             echo "\t<tr><td>First name</td><td>{$addr->getFirstName()}</td></tr>\n";
             echo "\t<tr><td>Last name</td><td>{$addr->getLastName()}</td></tr>\n";
         }
         echo "\t<tr><td>Street</td><td>{$addr->getStreet()}</td></tr>\n";
         echo "\t<tr><td>Zip code</td><td>{$addr->getZipCode()}</td></tr>\n";
         echo "\t<tr><td>City</td><td>{$addr->getCity()}</td></tr>\n";
         echo "\t<tr><td>Country</td><td>{$addr->getCountryCode()}</td></tr>\n";
         echo "</table>\n";
     }
     //Something went wrong
     return null;
     echo "{$e->getMessage()} (#{$e->getCode()})\n";
     return null;
     return null;
 }
 public function hookExtraRight(array $params)
 {
     if (!$this->active) {
         return false;
     }
     if (!Configuration::get('KLARNA_ACTIVE_PARTPAYMENT')) {
         return false;
     }
     if ($this->context->language->iso_code == 'no') {
         $iso_code = $this->_verifCurrencyLanguage(KlarnaCurrency::fromCode($this->context->currency->iso_code), KlarnaLanguage::fromCode('nb'));
     } else {
         $iso_code = $this->_verifCurrencyLanguage(KlarnaCurrency::fromCode($this->context->currency->iso_code), KlarnaLanguage::fromCode($this->context->language->iso_code));
     }
     $product = new Product((int) Tools::getValue('id_product'));
     if (Validate::isLoadedObject($params['cart'])) {
         $cart = $params['cart'];
         $address_delivery = new Address($params['cart']->id_address_delivery);
         $country = new Country($address_delivery->id_country);
         $currency = new Currency($params['cart']->id_currency);
         if (!$this->verifCountryAndCurrency($country, $currency)) {
             return false;
         }
     } else {
         if ($iso_code) {
             $country = new Country(pSQL(Country::getByIso($iso_code)));
             $currency = new Currency($this->context->currency->id);
         } else {
             return false;
         }
     }
     if ($currency->iso_code == 'SEK') {
         $countryIsoCode = 'SE';
     } else {
         if ($currency->iso_code == 'DKK') {
             $countryIsoCode = 'DK';
         } else {
             if ($currency->iso_code == 'NOK') {
                 $countryIsoCode = 'NO';
             } else {
                 if ($currency->iso_code == 'EUR' && $this->context->language->iso_code == 'fi') {
                     $countryIsoCode = 'FI';
                 } else {
                     if ($currency->iso_code == 'EUR' && $this->context->language->iso_code == 'de') {
                         if (isset($cart) && $country->iso_code == 'DE' || $this->context->country->iso_code == 'DE' && !isset($cart)) {
                             $countryIsoCode = 'DE';
                         } else {
                             return false;
                         }
                     } else {
                         if ($currency->iso_code == 'EUR' && $this->context->language->iso_code == 'nl') {
                             if (isset($cart) && $country->iso_code == 'NL' || $this->context->country->iso_code == 'NL' && !isset($cart)) {
                                 $countryIsoCode = 'NL';
                             } else {
                                 return false;
                             }
                         } else {
                             return false;
                         }
                     }
                 }
             }
         }
     }
     if (!Configuration::get('KLARNA_STORE_ID_' . $this->countries[$countryIsoCode]['name'])) {
         return false;
     }
     $amount = $product->getPrice();
     $smarty = $this->context->smarty;
     $klarna = new Klarna();
     try {
         $klarna->config(Configuration::get('KLARNA_STORE_ID_' . $this->countries[$countryIsoCode]['name']), Configuration::get('KLARNA_SECRET_' . $this->countries[$countryIsoCode]['name']), $this->countries[$countryIsoCode]['code'], $this->countries[$countryIsoCode]['langue'], $this->countries[$countryIsoCode]['currency'], Configuration::get('KLARNA_MOD'), 'mysql', $this->_getDb());
         $pclass = $klarna->getCheapestPClass((double) $amount, KlarnaFlags::PRODUCT_PAGE);
         if ($pclass) {
             $value = KlarnaCalc::calc_monthly_cost((double) $amount, $pclass, KlarnaFlags::PRODUCT_PAGE);
         }
         if (!isset($value) || $value == 0) {
             return false;
         }
         $pclasses = array_merge($klarna->getPClasses(KlarnaPClass::ACCOUNT), $klarna->getPClasses(KlarnaPClass::CAMPAIGN));
         $accountPrice = array();
         foreach ($pclasses as $val) {
             $accountPrice[$val->getId()] = array('price' => KlarnaCalc::calc_monthly_cost((double) $amount, $val, KlarnaFlags::PRODUCT_PAGE), 'month' => (int) $val->getMonths(), 'description' => htmlspecialchars_decode(Tools::safeOutput($val->getDescription())));
         }
     } catch (Exception $e) {
         return false;
     }
     $this->context->smarty->assign(array('minValue' => (double) $value, 'accountPrices' => $accountPrice, 'productcss' => './modules/klarnaprestashop/css/klarnaproduct.css', 'country' => $countryIsoCode, 'linkTermsCond' => 'https://online.klarna.com/account_' . strtolower($countryIsoCode) . '.yaws?eid=' . (int) Configuration::get('KLARNA_STORE_ID_' . $this->countries[$countryIsoCode]['name'])));
     return $this->display(__FILE__, 'tpl/product.tpl');
 }