Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Sundell committed Jun 19, 2019
2 parents 9c3684c + c4c60b2 commit c3fd4d7
Show file tree
Hide file tree
Showing 25 changed files with 2,670 additions and 42 deletions.
547 changes: 536 additions & 11 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sveaekonomi/webpay",
"version": "3.10.1",
"version": "3.11.0",
"description": "Php integration library for Svea Ekonomis payment methods",
"license": "Apache-2.0",
"authors": [
Expand Down
1 change: 1 addition & 0 deletions example/checkout/createCheckoutOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
->setTermsUri('http://localhost:51898/terms')
//->setValidationCallbackUri('http://localhost:51898/validation-callback')
//->setPartnerKey("77FB33EC-505D-4CCF-AA21-D9DF50DC8344")
//->setMerchantData("merchantData")
->setLocale($locale);

$presetPhoneNumber = WebPayItem::presetValue()
Expand Down
1 change: 1 addition & 0 deletions example/checkout/updateCheckoutOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
$orderBuilder = WebPay::checkout($myConfig);

$orderBuilder->setCheckoutOrderId(7479)
//->setMerchantData("merchantData")
->setCountryCode('SE');

// create and add items to order
Expand Down
19 changes: 19 additions & 0 deletions src/BuildOrder/RowBuilders/OrderRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class OrderRow
*/
public $vatDiscount = 0;

/**
* @var string $merchantData
*/
public $merchantData;

/**
* Optional
* @param string $articleNumberAsString
Expand Down Expand Up @@ -116,6 +121,20 @@ public function setTemporaryReference($temporaryReference)
return $this;
}

/**
* Optional - Can be used when creating or updating an order.
* The returned rows will have their corresponding merchant data as they were given in the in-data.
* It will be stored by Svea and will be returned in GetOrder.
* Checkout orders only. Will not be applicable for other order types.
* @param $merchantData
* @return $this
*/
public function setMerchantData($merchantData)
{
$this->merchantData = $merchantData;
return $this;
}

/**
* Recommended - precisely two of these values must be set in the Svea\WebPay\WebPayItem object: AmountExVat, AmountIncVat or VatPercent for Orderrow.
* Use functions setAmountExVat(), setAmountIncVat() or setVatPercent(). The recommended is to use setAmountExVat() and setVatPercent().
Expand Down
12 changes: 12 additions & 0 deletions src/Checkout/CheckoutOrderEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,16 @@ public function addIdentityFlag($identityFlag)

return $this;
}

/**
* Set merchantData on order
*
* @param string $merchantData
* @return $this
*/
public function setMerchantData($merchantData)
{
$this->checkoutOrderBuilder->setMerchantData($merchantData);
return $this;
}
}
16 changes: 16 additions & 0 deletions src/Checkout/Helper/CheckoutOrderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class CheckoutOrderBuilder extends OrderBuilder
*/
protected $identityFlags = array();

/**
* @var string $merchantData
*/
protected $merchantData;

/**
* CheckoutOrderBuilder constructor.
* @param \Svea\WebPay\Config\ConfigurationProvider $config
Expand Down Expand Up @@ -332,4 +337,15 @@ public function addIdentityFlag($identityFlag)

return $this;
}

public function setMerchantData($merchantData)
{
$this->merchantData = $merchantData;
return $this;
}

public function getMerchantData()
{
return $this->merchantData;
}
}
19 changes: 19 additions & 0 deletions src/Checkout/Model/CheckoutOrderRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ class CheckoutOrderRow
*/
private $temporaryReference;

/**
* @var $merchantData
*/
private $merchantData;

/**
* @return string
*/
public function getMerchantData()
{
return $this->merchantData;
}

public function setMerchantData($merchantData)
{
$this->merchantData = $merchantData;
return $this;
}

/**
* @return string
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Checkout/Service/CheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ protected function mapOrderItem($item)
if (isset($item->TemporaryReference)) {
$checkoutOrderItem->setTemporaryReference($item->TemporaryReference);
}
if (isset($item->MerchantData)) {
$checkoutOrderItem->setMerchantData($item->MerchantData);
}

return $checkoutOrderItem->toArray();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Checkout/Service/CreateOrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ protected function mapCreateOrderData(CheckoutOrderBuilder $order)
}
}

$data['merchantData'] = $order->getMerchantData();

return $data;
}
}
2 changes: 1 addition & 1 deletion src/Checkout/Service/UpdateOrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function mapCreateOrderData(CheckoutOrderBuilder $order)
}

$data['orderId'] = $order->getId();

$data['merchantData'] = $order->getMerchantData();
return $data;
}
}
20 changes: 18 additions & 2 deletions src/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,16 @@ static function splitMeanAcrossTaxRates($discountAmount, $discountMeanVat, $disc
* the flag is set to false or left out, the values array will not include
* such amounts, which may result in an empty values array in the result.
*
* @deprecated Use Svea\WebPay\Helper\PaymentPlanHelper instead, will be removed in the future
* @param float $price
* @param $paymentPlanParamsResponseObject
* @param boolean $ignoreMaxAndMinFlag ; optional, defaults to false
* @param int $decimals ; optional, defaults to 0
* @return PaymentPlanPricePerMonth
*/
public static function paymentPlanPricePerMonth($price, $paymentPlanParamsResponseObject, $ignoreMaxAndMinFlag = false)
public static function paymentPlanPricePerMonth($price, $paymentPlanParamsResponseObject, $ignoreMaxAndMinFlag = false, $decimals = 0)
{
return new PaymentPlanPricePerMonth($price, $paymentPlanParamsResponseObject, $ignoreMaxAndMinFlag);
return new PaymentPlanPricePerMonth($price, $paymentPlanParamsResponseObject, $ignoreMaxAndMinFlag, $decimals);
}

public static function getCardPayCurrencies()
Expand Down Expand Up @@ -365,4 +367,18 @@ public static function isValidPeppolId($peppolId)
}
return true;
}

public static function objectToArray($data)
{
if (is_array($data) || is_object($data))
{
$result = array();
foreach ($data as $key => $value)
{
$result[$key] = Helper::objectToArray($value);
}
return $result;
}
return $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php


namespace Svea\WebPay\Helper\PaymentPlanHelper\CampaignTypeCalculator;

use Svea\WebPay\BuildOrder\Validator\ValidationException;
use Svea\WebPay\Helper\Helper;

class EffectiveInterestRateCalculator
{
private $oneMonth = 0.08327627652; // Factor of month average length divided by average year length (leap years etc)
private $tolerance = 0.00000001;
private $initialLowerBound = 0;
private $initialUpperBound = 10000000;
private $payments = array();
private $sizeOfLoan;

public function __construct($sizeOfLoan)
{
$this->sizeOfLoan = $sizeOfLoan;
}

/**
* @param $sizeOfLoan
* @param $firstPayment
* @param $monthlyPayment
* @param $contactLengthInMonths
* @param $deferralPeriodInMonths
* @return float|int
* @throws ValidationException
*/
public function calculate($sizeOfLoan, $firstPayment, $monthlyPayment, $contactLengthInMonths, $deferralPeriodInMonths)
{
if($monthlyPayment < 0)
{
throw new ValidationException("Monthly payment can not be below 0");
}
if($contactLengthInMonths < 1)
{
throw new ValidationException("Contract length must be at least 1 month");
}

$firstPaymentMonth = min($contactLengthInMonths, $deferralPeriodInMonths + 1);
$this->addPayment($this->oneMonth * $firstPaymentMonth, $firstPayment);

for($month = $firstPaymentMonth + 1; $month <= $contactLengthInMonths; $month++)
{
$this->addPayment($this->oneMonth * $month, $monthlyPayment);
}

if(array_sum(array_column($this->payments, 'amount')) - $sizeOfLoan < 0.1)
{
return 0;
}

return Helper::bround($this->solveUsingBisection() * 100, 2);
}

/**
* @return float|int
* @throws ValidationException
*/
public function solveUsingBisection()
{
$lowerBound = $this->initialLowerBound;
$upperBound = $this->initialUpperBound;

while($upperBound - $lowerBound > $this->tolerance)
{
$newPoint = $lowerBound + ($upperBound - $lowerBound) / 2;
if($this->sign($this->evaluate($lowerBound)) == $this->sign($this->evaluate($newPoint)))
{
$lowerBound = $newPoint;
}
else
{
$upperBound = $newPoint;
}
}

if(abs($lowerBound - $this->initialLowerBound) < 2 * $this->tolerance || abs($upperBound - $this->initialUpperBound) < 2 * $this->tolerance)
{
throw new ValidationException("No solution found");
}
return $lowerBound;
}

private function sign( $number ) {
return ( $number > 0 ) ? 1 : ( ( $number < 0 ) ? -1 : 0 );
}

private function evaluate($val)
{
$sum = 0;
foreach($this->payments as $value)
{
$sum += $value['amount'] / pow(1 + $val, $value['timeToPaymentInYears']);
}
return $sum - $this->sizeOfLoan;
}

public function addPayment($timeToPaymentInYears, $amount)
{
array_push($this->payments, array('timeToPaymentInYears' => $timeToPaymentInYears, 'amount' => $amount));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace Svea\WebPay\Helper\PaymentPlanHelper\CampaignTypeCalculator;


interface IPaymentPlanCalculator
{
static public function calculateTotalAmountToPay($totalPrice, $campaign, $decimals = 0);
static public function calculateMonthlyAmountToPay($totalPrice, $campaign, $decimals = 0);
static public function calculateEffectiveInterestRate($totalPrice, $campaign, $decimals = 0);
static public function calculatePaymentFactor($numberOfPayments, $yearlyInterestRate, $paymentFrequencyPerYear = 12);
static public function calculateMonthlyAnnuityAmount($totalPrice, $campaign);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


namespace Svea\WebPay\Helper\PaymentPlanHelper\CampaignTypeCalculator;

use Svea\WebPay\Helper\Helper;

class InterestAndAmortizationFreePaymentPlanCalculator implements IPaymentPlanCalculator
{
public static function calculateTotalAmountToPay($totalPrice, $campaign, $decimals = 0)
{
return $totalPrice + $campaign['notificationFee'] + $campaign['initialFee'];
}

public static function calculateMonthlyAmountToPay($totalPrice, $campaign, $decimals = 0)
{
return $totalPrice + $campaign['notificationFee'] + $campaign['initialFee'];
}

public static function calculateEffectiveInterestRate($totalPrice, $campaign, $decimals = 0)
{
$effectiveCalculator = new EffectiveInterestRateCalculator($totalPrice);
$firstPayment = $campaign['initialFee'] + $campaign['notificationFee'] + Helper::bround(self::calculateMonthlyAnnuityAmount($totalPrice, $campaign), $decimals);
$monthlyAmount = Helper::bround(self::calculateMonthlyAnnuityAmount($totalPrice, $campaign), $decimals) + $campaign['notificationFee'];
return $effectiveCalculator->calculate($totalPrice, $firstPayment, $monthlyAmount, $campaign['contractLengthInMonths'], $campaign['numberOfPaymentFreeMonths']);
}

public static function calculatePaymentFactor($numberOfPayments, $yearlyInterestRate, $paymentFrequencyPerYear = 12)
{
return 1;
}

public static function calculateMonthlyAnnuityAmount($totalPrice, $campaign)
{
return $totalPrice;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php


namespace Svea\WebPay\Helper\PaymentPlanHelper\CampaignTypeCalculator;

use Svea\WebPay\Helper\Helper;

class InterestFreePaymentPlanCalculator implements IPaymentPlanCalculator
{
public static function calculateTotalAmountToPay($totalPrice, $campaign, $decimals = 0)
{
return $totalPrice + $campaign['initialFee'] + $campaign['notificationFee'] * max(1, $campaign['contractLengthInMonths'] - $campaign['numberOfPaymentFreeMonths']);
}

public static function calculateMonthlyAmountToPay($totalPrice, $campaign, $decimals = 0)
{
return Helper::bround(self::calculateTotalAmountToPay($totalPrice, $campaign) / max(1, $campaign['contractLengthInMonths'] - $campaign['numberOfPaymentFreeMonths']), $decimals);
}

public static function calculateEffectiveInterestRate($totalPrice, $campaign, $decimals = 0)
{
$effectiveCalculator = new EffectiveInterestRateCalculator($totalPrice);
$firstPayment = $campaign['initialFee'] + $campaign['notificationFee'] + Helper::bround(self::calculateMonthlyAnnuityAmount($totalPrice, $campaign), $decimals);
$monthlyAmount = Helper::bround(self::calculateMonthlyAnnuityAmount($totalPrice, $campaign), $decimals) + $campaign['notificationFee'];
return $effectiveCalculator->calculate($totalPrice, $firstPayment, $monthlyAmount, $campaign['contractLengthInMonths'], $campaign['numberOfPaymentFreeMonths']);
}

public static function calculatePaymentFactor($numberOfPayments, $yearlyInterestRate, $paymentFrequencyPerYear = 12)
{
return 1 / $numberOfPayments;
}

public static function calculateMonthlyAnnuityAmount($totalPrice, $campaign)
{
$paymentFactor = self::calculatePaymentFactor($campaign['contractLengthInMonths'] - $campaign['numberOfPaymentFreeMonths'],
$campaign['interestRatePercent']);
return $totalPrice * $paymentFactor;
}
}
Loading

0 comments on commit c3fd4d7

Please sign in to comment.