/** * 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; }
/** * 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; }
/** * 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; } }