Esempio n. 1
0
 /**
  * Convert supplied currency information to the matching iso-4217 code
  *
  * @param string|int $currency currency iso code
  *
  * @return iso-4217 code
  */
 private function _convertCurrency($currency)
 {
     if (is_numeric($currency)) {
         $currency = KlarnaCurrency::getCode($currency);
     }
     return strtoupper($currency);
 }
 /**
  * {@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;
         };
     }
 }
Esempio n. 3
0
 public function setCurrency($currency)
 {
     if (!is_numeric($currency)) {
         $currency = KlarnaCurrency::fromCode($currency);
     } else {
         $currency = intval($currency);
     }
     switch ($currency) {
         case KlarnaCurrency::SEK:
         case KlarnaCurrency::NOK:
         case KlarnaCurrency::DKK:
             $this->addSetupValue('currency_suffix', ' kr');
             $this->addSetupValue('currency_prefix', '');
             break;
         case KlarnaCurrency::EUR:
             $this->addSetupValue('currency_prefix', '€');
             $this->addSetupValue('currency_suffix', '');
             break;
         default:
             $this->addSetupValue('currency_suffix', '');
             $this->addSetupValue('currency_prefix', '');
             break;
     }
 }
Esempio n. 4
0
 /**
  * get the currency of the locale
  *
  * @return int currency constant
  */
 public function getCurrency()
 {
     return KlarnaCurrency::fromCode($this->_currency);
 }
Esempio n. 5
0
 /**
  * Returns the the currency code for the set currency constant.
  *
  * @param  int     {@link KlarnaCurrency Currency} constant.
  * @return string  Three letter currency code.
  */
 public function getCurrencyCode($currency = null)
 {
     $currency = $currency === null ? $this->currency : $currency;
     $code = KlarnaCurrency::getCode($currency);
     return $code === null ? '' : $code;
 }
Esempio n. 6
0
 /**
  * Returns the the currency code for the set currency constant.
  *
  * @param int $currency {@link KlarnaCurrency Currency} constant.
  *
  * @return string  Three letter currency code.
  */
 public function getCurrencyCode($currency = null)
 {
     if ($currency === null) {
         $currency = $this->_currency;
     }
     $code = KlarnaCurrency::getCode($currency);
     return (string) $code;
 }
Esempio n. 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');
 }