/** * Adds a PClass to the storage. * * @param KlarnaPClass $pclass PClass object. * * @throws KlarnaException * @return void */ public function addPClass($pclass) { if (!$pclass instanceof KlarnaPClass) { throw new Klarna_InvalidTypeException('pclass', 'KlarnaPClass'); } if (!isset($this->pclasses) || !is_array($this->pclasses)) { $this->pclasses = array(); } if ($pclass->getDescription() === null || $pclass->getType() === null) { //Something went wrong, do not save these! return; } if (!isset($this->pclasses[$pclass->getEid()])) { $this->pclasses[$pclass->getEid()] = array(); } $this->pclasses[$pclass->getEid()][$pclass->getId()] = $pclass; }
/** * Adds a PClass to the storage. * * @param KlarnaPClass $pclass PClass object. * @throws KlarnaException * @return void */ public function addPClass($pclass) { if($pclass instanceof KlarnaPClass) { if(!isset($this->pclasses) || !is_array($this->pclasses)) { $this->pclasses = array(); } if($pclass->getDescription() === null || $pclass->getType() === null) { //Something went wrong, do not save these! return; } if(!isset($this->pclasses[$pclass->getEid()])) { $this->pclasses[$pclass->getEid()] = array(); } $this->pclasses[$pclass->getEid()][$pclass->getId()] = $pclass; } else { throw new KlarnaException('Error in ' . __METHOD__ . ': Supplied pclass object is not an KlarnaPClass instance!'); } }
/** * Calculates APR for the specified values.<br> * Result is rounded with two decimals.<br> * * <b>Flags can be either</b>:<br> * {@link KlarnaFlags::CHECKOUT_PAGE}<br> * {@link KlarnaFlags::PRODUCT_PAGE}<br> * * @param float $sum The sum for the order/product. * @param KlarnaPClass $pclass KlarnaPClass used to calculate the APR. * @param int $flags Checkout or Product page. * @param int $free Number of free months. * * @throws KlarnaException * @return float APR in % */ public static function calc_apr($sum, $pclass, $flags, $free = 0) { if (!is_numeric($sum)) { throw new Klarna_InvalidTypeException('sum', 'numeric'); } if (is_numeric($sum) && (!is_int($sum) || !is_float($sum))) { $sum = floatval($sum); } if (!$pclass instanceof KlarnaPClass) { throw new Klarna_InvalidTypeException('pclass', 'KlarnaPClass'); } if (!is_numeric($free)) { throw new Klarna_InvalidTypeException('free', 'integer'); } if (is_numeric($free) && !is_int($free)) { $free = intval($free); } if ($free < 0) { throw new KlarnaException('Error in ' . __METHOD__ . ': Number of free months must be positive or zero!'); } if (is_numeric($flags) && !is_int($flags)) { $flags = intval($flags); } if (!is_numeric($flags) || !in_array($flags, array(KlarnaFlags::CHECKOUT_PAGE, KlarnaFlags::PRODUCT_PAGE))) { throw new Klarna_InvalidTypeException('flags', KlarnaFlags::CHECKOUT_PAGE . ' or ' . KlarnaFlags::PRODUCT_PAGE); } $monthsfee = 0; if ($flags === KlarnaFlags::CHECKOUT_PAGE) { $monthsfee = $pclass->getInvoiceFee(); } $startfee = 0; if ($flags === KlarnaFlags::CHECKOUT_PAGE) { $startfee = $pclass->getStartFee(); } //Include start fee in sum $sum += $startfee; $lowest = self::get_lowest_payment_for_account($pclass->getCountry()); if ($flags == KlarnaFlags::CHECKOUT_PAGE) { $minpay = $pclass->getType() === KlarnaPClass::ACCOUNT ? $lowest : 0; } else { $minpay = 0; } //add monthly fee $payment = self::_annuity($sum, $pclass->getMonths(), $pclass->getInterestRate()) + $monthsfee; $type = $pclass->getType(); switch ($type) { case KlarnaPClass::CAMPAIGN: case KlarnaPClass::ACCOUNT: return round(self::_aprAnnuity($sum, $pclass->getMonths(), $pclass->getInterestRate(), $pclass->getInvoiceFee(), $minpay), 2); case KlarnaPClass::SPECIAL: throw new Klarna_PClassException('Method is not available for SPECIAL pclasses'); case KlarnaPClass::FIXED: throw new Klarna_PClassException('Method is not available for FIXED pclasses'); default: throw new Klarna_PClassException('Unknown PClass type! (' . $type . ')'); } }
/** * Comparison function * * @param KlarnaPClass $a object 1 * @param KlarnaPClass $b object 2 * * @return int */ function pcCmp($a, $b) { if ($a->getDescription() == null && $b->getDescription() == null) { return 0; } else { if ($a->getDescription() == null) { return 1; } else { if ($b->getDescription() == null) { return -1; } else { if ($b->getType() === 2 && $a->getType() !== 2) { return 1; } else { if ($b->getType() !== 2 && $a->getType() === 2) { return -1; } } } } } return strnatcmp($a->getDescription(), $b->getDescription()) * -1; }
/** * Calculates APR for the specified values.<br> * Result is rounded with two decimals.<br> * * <b>Flags can be either</b>:<br> * {@link KlarnaFlags::CHECKOUT_PAGE}<br> * {@link KlarnaFlags::PRODUCT_PAGE}<br> * * @param float $sum The sum for the order/product. * @param KlarnaPClass $pclass {@link KlarnaPClass PClass} used to calculate the APR. * @param int $flags Indicates if it is the checkout or a product page. * @param int $free Number of free months. * @throws KlarnaException * @return float APR in % */ public static function calc_apr($sum, $pclass, $flags, $free = 0) { if(!is_numeric($sum)) { throw new KlarnaException('Error in ' . __METHOD__ . ': Argument sum is not numeric!'); } else if(is_numeric($sum) && (!is_int($sum) || !is_float($sum))) { $sum = floatval($sum); } if(!($pclass instanceof KlarnaPClass)) { throw new KlarnaException('Error in ' . __METHOD__ . ': Supplied PClass is not a PClass object!'); } if(!is_numeric($free)) { throw new KlarnaException('Error in ' . __METHOD__ . ': Argument free is not an integer!'); } else if(is_numeric($free) && !is_int($free)) { $free = intval($free); } if($free < 0) { throw new KlarnaException('Error in ' . __METHOD__ . ': Number of free months must be positive or zero!'); } if(is_numeric($flags) && !is_int($flags)) { $flags = intval($flags); } if(!is_numeric($flags) || !in_array($flags, array(KlarnaFlags::CHECKOUT_PAGE, KlarnaFlags::PRODUCT_PAGE))) { throw new KlarnaException('Error in ' . __METHOD__ . ': Flags argument invalid!'); } $monthsfee = (($flags === KlarnaFlags::CHECKOUT_PAGE) ? $pclass->getInvoiceFee() : 0); $startfee = (($flags === KlarnaFlags::CHECKOUT_PAGE) ? $pclass->getStartFee() : 0); //Include start fee in sum $sum += $startfee; $lowest = self::get_lowest_payment_for_account($pclass->getCountry()); if($flags == KlarnaFlags::CHECKOUT_PAGE) { $minpay = ($pclass->getType() === KlarnaPClass::ACCOUNT) ? $lowest : 0; } else { $minpay = 0; } //add monthly fee $payment = self::annuity($sum, $pclass->getMonths(), $pclass->getInterestRate()) + $monthsfee; //echo "annuity $payment, $sum " . $pclass->getMonths() . " " . $pclass->getInterestRate() . "\n"; $type = $pclass->getType(); switch($type) { case KlarnaPClass::CAMPAIGN: case KlarnaPClass::ACCOUNT: $apr = self::apr_annuity($sum, $pclass->getMonths(), $pclass->getInterestRate(), $pclass->getInvoiceFee(), $minpay); break; case KlarnaPClass::SPECIAL: $apr = self::apr_payin_X_months($sum, $payment, $pclass->getInterestRate(), $pclass->getInvoiceFee(), $minpay, $free); break; case KlarnaPClass::FIXED: $apr = self::apr_fixed($sum, $payment, $pclass->getInterestRate(), $pclass->getInvoiceFee(), $minpay); break; default: throw new KlarnaException('Error in ' . __METHOD__ . ': Unknown PClass type! ('.$type.')'); } return round($apr, 2); }