Example #1
0
 /**
  * Fetch the PClasses from file
  *
  * @param    integer    $iSum    The sum of the objects to be bought
  * @param    integer    $iFlag    The KlarnaFlag to be used. Either Checkout or ProductPage flag.
  * @return    void
  */
 public function fetchPClasses($iSum, $iFlag, $aTypes = NULL)
 {
     if ($this->oKlarna == NULL) {
         throw new KlarnaApiException("No klarna class is set.", "1000");
     }
     $aPClasses = array();
     $default = NULL;
     foreach ($this->oKlarna->getPClasses() as $pclass) {
         if ($aTypes == NULL || in_array($pclass->getType(), $aTypes)) {
             $sType = $pclass->getType();
             if ($sType != KlarnaPClass::SPECIAL) {
                 if ($iSum < $pclass->getMinAmount()) {
                     continue;
                 }
                 if ($pclass->getType() == KlarnaPClass::FIXED) {
                     if ($iFlag == KlarnaFlags::PRODUCT_PAGE) {
                         continue;
                     }
                     $iMonthlyCost = -1;
                 } else {
                     $lowest_payment = KlarnaCalc::get_lowest_payment_for_account($pclass->getCountry());
                     $iMonthlyCost = KlarnaCalc::calc_monthly_cost($iSum, $pclass, $iFlag);
                     if ($iMonthlyCost < 0.01) {
                         continue;
                     }
                     if ($iFlag == KlarnaFlags::CHECKOUT_PAGE && $pclass->getType() == KlarnaPClass::ACCOUNT && $iMonthlyCost < $lowest_payment) {
                         $iMonthlyCost = $lowest_payment;
                     }
                     if ($pclass->getType() == KlarnaPClass::CAMPAIGN && $iMonthlyCost < $lowest_payment) {
                         continue;
                     }
                 }
             } else {
                 $iMonthlyCost = -1;
             }
             if ($this->sType == 'part') {
                 if ($sType == KlarnaPClass::ACCOUNT) {
                     $default = $pclass;
                 } else {
                     if ($sType == KlarnaPClass::CAMPAIGN) {
                         if ($default === NULL || $default->getType() != KlarnaPClass::ACCOUNT) {
                             $default = $pclass;
                         }
                     } else {
                         if ($sType == KlarnaPClass::FIXED) {
                             if ($default === NULL) {
                                 $default = $pclass;
                             }
                         } else {
                             continue;
                         }
                     }
                 }
             } else {
                 if ($this->sType == 'spec') {
                     if ($sType != KlarnaPClass::SPECIAL) {
                         continue;
                     }
                     $default = $pclass;
                 }
             }
             $aPClasses[$pclass->getId()]['pclass'] = $pclass;
             $aPClasses[$pclass->getId()]['monthlyCost'] = $iMonthlyCost;
             $aPClasses[$pclass->getId()]['default'] = FALSE;
         }
     }
     if ($default !== NULL) {
         $aPClasses[$default->getId()]['default'] = TRUE;
     }
     $this->aPClasses = $aPClasses;
 }
 public function getMonthlyCoast($cart, $countries, $country)
 {
     if (!$this->klarna->active) {
         return;
     }
     $klarna = new Klarna();
     $klarna->config(Configuration::get('KLARNA_STORE_ID_' . $countries[$country->iso_code]['name']), Configuration::get('KLARNA_SECRET_' . $countries[$country->iso_code]['name']), $countries[$country->iso_code]['code'], $countries[$country->iso_code]['langue'], $countries[$country->iso_code]['currency'], Configuration::get('KLARNA_MOD'), 'mysql', array('user' => _DB_USER_, 'passwd' => _DB_PASSWD_, 'dsn' => _DB_SERVER_, 'db' => _DB_NAME_, 'table' => _DB_PREFIX_ . 'klarna_payment_pclasses'));
     $accountPrice = array();
     $pclasses = array_merge($klarna->getPClasses(KlarnaPClass::ACCOUNT), $klarna->getPClasses(KlarnaPClass::CAMPAIGN));
     $total = (double) $cart->getOrderTotal();
     foreach ($pclasses as $val) {
         if ($val->getMinAmount() < $total) {
             $accountPrice[$val->getId()] = array('price' => KlarnaCalc::calc_monthly_cost($total, $val, KlarnaFlags::CHECKOUT_PAGE), 'month' => (int) $val->getMonths(), 'description' => htmlspecialchars_decode(Tools::safeOutput($val->getDescription())));
         }
     }
     return $accountPrice;
 }
Example #3
0
 /**
  * Returns the cheapest, per month, PClass related to the specified sum.
  *
  * <b>Note</b>: This choose the cheapest PClass for the current country.<br>
  * {@link Klarna::setCountry()}
  *
  * <b>Flags can be</b>:<br>
  * {@link KlarnaFlags::CHECKOUT_PAGE}<br>
  * {@link KlarnaFlags::PRODUCT_PAGE}<br>
  *
  * @param  float  $sum       The product cost, or total sum of the cart.
  * @param  int    $flags     Which type of page the info will be displayed on.
  * @throws KlarnaException
  * @return KlarnaPClass or false if none was found.
  */
 public function getCheapestPClass($sum, $flags)
 {
     if (!is_numeric($sum)) {
         throw new KlarnaException('Error in ' . __METHOD__ . ': Argument sum is not numeric!');
     }
     if (!is_numeric($flags) || !in_array($flags, array(KlarnaFlags::CHECKOUT_PAGE, KlarnaFlags::PRODUCT_PAGE))) {
         throw new KlarnaException('Error in ' . __METHOD__ . ': Flags argument invalid!');
     }
     $lowest_pp = $lowest = false;
     foreach ($this->getPClasses() as $pclass) {
         $lowest_payment = KlarnaCalc::get_lowest_payment_for_account($pclass->getCountry());
         if ($pclass->getType() < 2 && $sum >= $pclass->getMinAmount()) {
             $minpay = KlarnaCalc::calc_monthly_cost($sum, $pclass, $flags);
             if ($minpay < $lowest_pp || $lowest_pp === false) {
                 if ($pclass->getType() == KlarnaPClass::ACCOUNT || $minpay >= $lowest_payment) {
                     $lowest_pp = $minpay;
                     $lowest = $pclass;
                 }
             }
         }
     }
     return $lowest;
 }
 /**
  * Update list of pclasses
  *
  * @param int   $sum   order total for filtering
  * @param int   $page  PRODUCT_PAGE or CHECKOUT_PAGE
  * @param array $types PClass types to include
  *
  * @return void
  */
 public function update($sum, $page, $types)
 {
     $pclasses = array();
     $default = null;
     $minimum = null;
     $minval = null;
     foreach ($this->_kapi->getPClasses() as $pclass) {
         $type = $pclass->getType();
         if (!in_array($type, $types) || $sum < $pclass->getMinAmount()) {
             continue;
         }
         // Get monthly cost
         if (in_array($type, array(KlarnaPClass::FIXED, KlarnaPClass::SPECIAL))) {
             if ($page == KlarnaFlags::PRODUCT_PAGE) {
                 continue;
             }
             $monthlyCost = -1;
         } else {
             $lowestPayment = KlarnaCalc::get_lowest_payment_for_account($pclass->getCountry());
             $monthlyCost = KlarnaCalc::calc_monthly_cost($sum, $pclass, $page);
             if ($monthlyCost < 0.01) {
                 continue;
             }
             if ($monthlyCost < $lowestPayment) {
                 if ($pclass->getType() == KlarnaPClass::CAMPAIGN) {
                     continue;
                 }
                 if ($page == KlarnaFlags::CHECKOUT_PAGE && $pclass->getType() == KlarnaPClass::ACCOUNT) {
                     $monthlyCost = $lowestPayment;
                 }
             }
         }
         // Select the minimum
         if ($minimum === null || $minval > $monthlyCost) {
             $minimum = $pclass;
             $minval = $monthlyCost;
         }
         // Select the default
         if ($type == KlarnaPClass::ACCOUNT) {
             $default = $pclass;
         } else {
             if ($type == KlarnaPClass::CAMPAIGN) {
                 if ($default === null || $default->getType() != KlarnaPClass::ACCOUNT) {
                     $default = $pclass;
                 }
             } else {
                 if ($default === null) {
                     $default = $pclass;
                 }
             }
         }
         $pclasses[$pclass->getId()] = array('pclass' => $pclass, 'locale' => new KiTT_Locale($pclass->getCountry()), 'monthlyCost' => $monthlyCost);
     }
     // Save result
     $this->pclasses = $pclasses;
     if ($default != null) {
         $this->_default = $default;
     }
     if ($minimum != null) {
         $this->_minimum = $minimum;
     }
 }
 public function hookdisplayPayment($params)
 {
     if (!Configuration::get('KLARNA_ACTIVE_INVOICE') && !Configuration::get('KLARNA_ACTIVE_PARTPAYMENT')) {
         return false;
     }
     $smarty = $this->context->smarty;
     $klarna = new Klarna();
     $address_invoice = new Address((int) $params['cart']->id_address_invoice);
     $country = new Country((int) $address_invoice->id_country);
     $currency = new Currency((int) $params['cart']->id_currency);
     if (isset($this->countries[$country->iso_code])) {
         $this->context->cart->deleteProduct((int) Configuration::get('KLARNA_INV_FEE_ID_' . $this->countries[$country->iso_code]['name']));
     }
     if (!$this->verifCountryAndCurrency($country, $currency)) {
         return false;
     }
     if (!$this->_verifRange($params['cart']->getOrderTotal(), $this->countries[$country->iso_code]['name'])) {
         return false;
     }
     try {
         $klarna->config(Configuration::get('KLARNA_STORE_ID_' . $this->countries[$country->iso_code]['name']), Configuration::get('KLARNA_SECRET_' . $this->countries[$country->iso_code]['name']), $this->countries[$country->iso_code]['code'], $this->countries[$country->iso_code]['langue'], $this->countries[$country->iso_code]['currency'], Configuration::get('KLARNA_MOD'), 'mysql', $this->_getDb());
         $pclass = $klarna->getCheapestPClass((double) $this->context->cart->getOrderTotal(), KlarnaFlags::CHECKOUT_PAGE);
         if ($pclass && $pclass->getMinAmount() < $this->context->cart->getOrderTotal()) {
             if ($country->iso_code == 'NL' && $this->context->cart->getOrderTotal() > 250) {
                 return false;
             } else {
                 $value = KlarnaCalc::calc_monthly_cost((double) $this->context->cart->getOrderTotal(), $pclass, KlarnaFlags::CHECKOUT_PAGE);
             }
         }
         $pclassSpec = $klarna->getPClasses(KlarnaPClass::SPECIAL);
         if (count($pclassSpec) && $pclassSpec[0]->getExpire() > time()) {
             $smarty->assign('special', $pclassSpec[0]->getDescription());
         }
     } catch (Exception $e) {
         return false;
     }
     $smarty->assign(array('var' => array('path' => $this->_path, 'this_path_ssl' => (_PS_VERSION_ >= 1.4 ? Tools::getShopDomainSsl(true, true) : '') . __PS_BASE_URI__ . 'modules/' . $this->moduleName . '/'), 'iso_code' => strtolower($country->iso_code), 'monthly_amount' => (double) $value, 'invoiceActive' => Configuration::get('KLARNA_ACTIVE_INVOICE'), 'accountActive' => Configuration::get('KLARNA_ACTIVE_PARTPAYMENT'), 'specialActive' => true));
     return $this->display(__FILE__, 'tpl/payment.tpl');
 }
        ?>
"
				        name="<?php 
        echo esc_attr($klarna_select_pclass_element);
        ?>
" class="woocommerce-select"
				        style="max-width:100%;width:100% !important;">

					<?php 
        foreach ($pclasses as $pclass) {
            // Loop through the available PClasses stored in the file srv/pclasses.json
            if (in_array($pclass->getType(), $pclass_type)) {
                // Get monthly cost for current pclass
                $monthly_cost = KlarnaCalc::calc_monthly_cost($sum, $pclass, $flag);
                // Get total credit purchase cost for current pclass (only required in Norway)
                $total_credit_purchase_cost = KlarnaCalc::total_credit_purchase_cost($sum, $pclass, $flag);
                // Check that Cart total is larger than min amount for current PClass
                if ($sum > $pclass->getMinAmount()) {
                    echo '<option value="' . $pclass->getId() . '">';
                    if ($pclass->getType() == 1) {
                        // If Account - Do not show startfee. This is always 0.
                        echo sprintf(__('%s - %s %s/month - %s%s', 'woocommerce-gateway-klarna'), $pclass->getDescription(), $monthly_cost, $this->selected_currency, $pclass->getInterestRate(), '%');
                    } else {
                        // Sweden, Denmark, Finland, Germany & Netherlands - Don't show total cost
                        echo sprintf(__('%s - %s %s/month - %s%s - Start %s', 'woocommerce-gateway-klarna'), $pclass->getDescription(), $monthly_cost, $this->selected_currency, $pclass->getInterestRate(), '%', $pclass->getStartFee());
                    }
                    echo '</option>';
                }
                // End if ($sum > $pclass->getMinAmount())
            }
            // End PClass type check
Example #7
0
    /**
     * add rates to Productlistings
     *
     * @param Enlight_Event_EventArgs $piKlarnaArgs
     */
    public static function piKlarnaOnPostDispatchListing(Enlight_Event_EventArgs $piKlarnaArgs) {
        $piKlarnaConfig = array();
        Shopware()->Template()->addTemplateDir(dirname(__FILE__) . '/Views/Frontend/');
        $piKlarnaConfig = Shopware()->Plugins()->Frontend()->PigmbhKlarnaPayment()->Config();
        $klarnaShopLang = checkKlarnaCountryCurrencys();
        if ($klarnaShopLang == 'de') $klarnaShopLang = Shopware()->Locale()->getLanguage();
        
        if ($piKlarnaConfig->pi_klarna_rate_listing && $piKlarnaConfig->pi_klarna_active && checkKlarnaCountrys($klarnaShopLang)) {
            $piKlarnaArticlePrice = array();
            $piKlarnaView = $piKlarnaArgs->getSubject()->View();
            $piKlarnaView->KlarnaJS = true;

            if (sizeof($piKlarnaView->sArticles) > 0) {
                $piKlarnaView->piKlarnaArticles = true;
                foreach($piKlarnaView->sArticles as $key => $article) {
                    $piKlarnaArticlePrice[$key] = str_replace(",", ".", $article['price']);
                }
            }
            else {
                $piKlarnaArticlePrice[0] = 0;
                $piKlarnaView->piKlarnaOffers = true;
                foreach($piKlarnaView->sOffers as $key => $article) {
                    $piKlarnaArticlePrice[$key] = str_replace(",", ".", $article['price']);
                }
            }
            $counter = array();
            $pi_klarna_value = array();
            //for ($i = 0; $i < sizeof($piKlarnaArticlePrice); $i++) {
            $i = 0;
            foreach($piKlarnaArticlePrice as $key=>$price) {
                if($price){
                    $k = piKlarnaCreateKlarnaInstance();
                    $k->setCountry($klarnaShopLang);
                    if ($k->getCheapestPClass($price, KlarnaFlags::PRODUCT_PAGE)){ 
                        $pi_klarna_value[$key] = number_format(KlarnaCalc::calc_monthly_cost($piKlarnaArticlePrice[$key], 
                            $k->getCheapestPClass($piKlarnaArticlePrice[$key], KlarnaFlags::PRODUCT_PAGE), 
                            KlarnaFlags::PRODUCT_PAGE), 2, ',', '.');
                    }
                    $counter[$key] = $key;
                }
                $i++;
            }
            $piKlarnaView->pi_klarna_counter = $counter;
            $piKlarnaView->pi_klarna_rate = $pi_klarna_value;
            $piKlarnaView->pi_klarna_sum = $piKlarnaView->extendsTemplate('listing/box_article.tpl');
        }
    }