/** * 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 . ')'); } }
/** * Creates DOMElement for all fields for specified PClass. * * @ignore Do not show in PHPDoc. * @param KlarnaPClass $pclass * @return array Array of DOMElements. */ protected function createFields($pclass) { $fields = array(); //This is to prevent HTMLEntities to be converted to the real character. $fields[] = $this->dom->createElement('description'); end($fields)->appendChild($this->dom->createTextNode($pclass->getDescription())); $fields[] = $this->dom->createElement('months', $pclass->getMonths()); $fields[] = $this->dom->createElement('startfee', $pclass->getStartFee()); $fields[] = $this->dom->createElement('invoicefee', $pclass->getInvoiceFee()); $fields[] = $this->dom->createElement('interestrate', $pclass->getInterestRate()); $fields[] = $this->dom->createElement('minamount', $pclass->getMinAmount()); $fields[] = $this->dom->createElement('country', $pclass->getCountry()); $fields[] = $this->dom->createElement('expire', $pclass->getExpire()); return $fields; }
/** * 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); }