Skip to content

Commit

Permalink
Release 1.0.72
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Schurter committed Dec 30, 2019
1 parent 42efdb4 commit 775d41f
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 126 deletions.
15 changes: 15 additions & 0 deletions Model/Service/LineItemReductionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* PostFinance Checkout Magento 2
*
* This Magento 2 extension enables to process payments with PostFinance Checkout (https://www.postfinance.ch/checkout/).
*
* @package PostFinanceCheckout_Payment
* @author customweb GmbH (http://www.customweb.com/)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License (ASL 2.0)
*/
namespace PostFinanceCheckout\Payment\Model\Service;

class LineItemReductionException extends \Exception
{
}
121 changes: 7 additions & 114 deletions Model/Service/LineItemReductionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function __construct(Helper $helper, LineItemReductionHelper $reductionHe
*
* @param Creditmemo $creditmemo
* @return LineItemReductionCreate[]
* @throws LineItemReductionException
*/
public function convertCreditmemo(Creditmemo $creditmemo)
{
Expand Down Expand Up @@ -130,7 +131,8 @@ public function convertCreditmemo(Creditmemo $creditmemo)
'creditmemo' => $creditmemo,
'baseLineItems' => $baseLineItems
]);
return $this->fixReductions($transport->getData('items'), $creditmemo, $baseLineItems);
$this->validateReductions($transport->getData('items'), $creditmemo, $baseLineItems);
return $transport->getData('items');
}

/**
Expand Down Expand Up @@ -203,130 +205,21 @@ private function convertShipping(Creditmemo $creditmemo)
}

/**
* Returns the fixed line item reductions for the creditmemo.
*
* If the amount of the given reductions does not match the creditmemo's grand total, the amount to refund is
* distributed equally to the line items.
* Validates whether the given reductions total amount matches the one of the creditmemo.
*
* @param LineItemReductionCreate[] $reductions
* @param Creditmemo $creditmemo
* @param \PostFinanceCheckout\Sdk\Model\LineItem[] $baseLineItems
* @return LineItemReductionCreate[]
* @throws LineItemReductionException
*/
private function fixReductions(array $reductions, Creditmemo $creditmemo, $baseLineItems)
private function validateReductions(array $reductions, Creditmemo $creditmemo, $baseLineItems)
{
$reducedAmount = $this->reductionHelper->getReducedAmount($baseLineItems, $reductions,
$creditmemo->getOrderCurrencyCode());
if ($this->helper->compareAmounts($reducedAmount, $creditmemo->getGrandTotal(),
$creditmemo->getOrderCurrencyCode()) != 0) {
$baseAmount = $this->lineItemHelper->getTotalAmountIncludingTax($baseLineItems);
$rate = $creditmemo->getGrandTotal() / $baseAmount;
$fixedReductions = [];
foreach ($baseLineItems as $lineItem) {
if ($lineItem->getQuantity() > 0) {
$reduction = new LineItemReductionCreate();
$reduction->setLineItemUniqueId($lineItem->getUniqueId());
$reduction->setQuantityReduction(0);
$reduction->setUnitPriceReduction(
$this->helper->roundAmount(
$lineItem->getAmountIncludingTax() * $rate / $lineItem->getQuantity(),
$creditmemo->getOrderCurrencyCode()));
$fixedReductions[] = $reduction;
}
}
$fixedReductionAmount = $this->reductionHelper->getReducedAmount($baseLineItems, $fixedReductions,
$creditmemo->getOrderCurrencyCode());
$roundingDifference = $creditmemo->getGrandTotal() - $fixedReductionAmount;
return $this->distributeRoundingDifference($fixedReductions, 0, $roundingDifference, $baseLineItems,
$creditmemo->getOrderCurrencyCode());
} else {
return $reductions;
}
}

/**
*
* @param LineItemReductionCreate[] $reductions
* @param int $index
* @param number $remainder
* @param \PostFinanceCheckout\Sdk\Model\LineItem[] $baseLineItems
* @param string $currencyCode
* @throws \Exception
* @return LineItemReductionCreate[]
*/
private function distributeRoundingDifference(array $reductions, $index, $remainder, array $baseLineItems,
$currencyCode)
{
$digits = $this->helper->getCurrencyFractionDigits($currencyCode);
$currentReduction = $reductions[$index];
$delta = $remainder;
$change = false;
$positive = $delta > 0;
$newReduction = null;
$appliedDelta = null;
if ($currentReduction->getUnitPriceReduction() != 0 && $currentReduction->getQuantityReduction() == 0) {
$lineItem = $this->getLineItemByUniqueId($baseLineItems, $currentReduction->getLineItemUniqueId());
if ($lineItem != null) {
while ($delta != 0) {
if ($currentReduction->getUnitPriceReduction() < 0) {
$newReduction = $this->helper->roundAmount(
$currentReduction->getUnitPriceReduction() - ($delta / $lineItem->getQuantity()),
$currencyCode);
} else {
$newReduction = $this->helper->roundAmount(
$currentReduction->getUnitPriceReduction() + ($delta / $lineItem->getQuantity()),
$currencyCode);
}
$appliedDelta = ($newReduction - $currentReduction->getUnitPriceReduction()) *
$lineItem->getQuantity();
if ($appliedDelta <= $delta &&
$this->helper->compareAmounts($newReduction, $lineItem->getUnitPriceIncludingTax(),
$currencyCode) <= 0) {
$change = true;
break;
}

$newDelta = \round((\abs($delta) - \pow(0.1, $digits + 1)) * ($positive ? 1 : - 1), 10);
if (($positive xor $newDelta > 0) && $delta != 0) {
break;
}
$delta = $newDelta;
}
}
}

if ($change) {
$currentReduction->setUnitPriceReduction($newReduction);
$newRemainder = $remainder - $appliedDelta;
} else {
$newRemainder = $remainder;
}

if ($index + 1 < \count($reductions) && $newRemainder != 0) {
return $this->distributeRoundingDifference($reductions, $index + 1, $newRemainder, $baseLineItems,
$currencyCode);
} else {
if ($newRemainder > \pow(0.1, $digits + 1)) {
throw new LocalizedException(\__('Could not distribute the rounding difference.'));
} else {
return $reductions;
}
}
}

/**
*
* @param \PostFinanceCheckout\Sdk\Model\LineItem[] $lineItems
* @param string $uniqueId
*/
private function getLineItemByUniqueId(array $lineItems, $uniqueId)
{
foreach ($lineItems as $lineItem) {
if ($lineItem->getUniqueId() == $uniqueId) {
return $lineItem;
}
throw new LineItemReductionException();
}
return null;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion Plugin/Sales/Model/Service/CreditmemoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PostFinanceCheckout\Payment\Model\ApiClient;
use PostFinanceCheckout\Payment\Model\RefundJobFactory;
use PostFinanceCheckout\Payment\Model\Payment\Method\Adapter as PaymentMethodAdapter;
use PostFinanceCheckout\Payment\Model\Service\LineItemReductionException;
use PostFinanceCheckout\Payment\Model\Service\LineItemReductionService;
use PostFinanceCheckout\Sdk\Model\RefundCreate;
use PostFinanceCheckout\Sdk\Model\RefundType;
Expand Down Expand Up @@ -168,7 +169,14 @@ private function createRefund(Creditmemo $creditmemo)
{
$refund = new RefundCreate();
$refund->setExternalId(\uniqid($creditmemo->getOrderId() . '-'));
$refund->setReductions($this->lineItemReductionService->convertCreditmemo($creditmemo));

try {
$reductions = $this->lineItemReductionService->convertCreditmemo($creditmemo);
$refund->setReductions($reductions);
} catch (LineItemReductionException $e) {
$refund->setAmount($creditmemo->getGrandTotal());
}

$refund->setTransaction($creditmemo->getOrder()
->getPostfinancecheckoutTransactionId());
$refund->setType(RefundType::MERCHANT_INITIATED_ONLINE);
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ This repository contains the Magento 2.3 extension that enables to process payme

## Documentation

* [Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.71/docs/en/documentation.html)
* [Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.72/docs/en/documentation.html)

## Supported One Step Checkouts

We do provide special integrations for the following one step checkouts:

* [OneStepCheckout](https://www.onestepcheckout.com/magento-2)
* [Checkout Suite One Page / Step Checkout](https://www.iwdagency.com/extensions/one-step-page-checkout.html)
* [One Step Checkout for Magento 2](https://amasty.com/one-step-checkout-for-magento-2.html)

## License

Please see the [license file](https://github.com/pfpayments/magento-2.3/blob/1.0.71/LICENSE) for more information.
Please see the [license file](https://github.com/pfpayments/magento-2.3/blob/1.0.72/LICENSE) for more information.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
}
],
"type" : "magento2-module",
"version" : "1.0.71",
"version" : "1.0.72",
"require" : {
"php": "~7.1.3||~7.2.0",
"magento/framework" : "^102.0.0",
"postfinancecheckout/sdk": "2.0.9"
"postfinancecheckout/sdk": "2.0.10"
},
"autoload" : {
"files" : [
Expand Down
2 changes: 1 addition & 1 deletion docs/en/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h2>Documentation</h2> </div>
</a>
</li>
<li>
<a href="https://github.com/pfpayments/magento-2.3/releases/tag/1.0.71/">
<a href="https://github.com/pfpayments/magento-2.3/releases/tag/1.0.72/">
Source
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<resource>PostFinanceCheckout_Payment::config</resource>
<group id="information" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Information</label>
<comment><![CDATA[If you need help setting up the PostFinance Checkout extension, check out the <a href="https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.71/docs/en/documentation.html" target="_blank">documentation</a>.]]></comment>
<comment><![CDATA[If you need help setting up the PostFinance Checkout extension, check out the <a href="https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.72/docs/en/documentation.html" target="_blank">documentation</a>.]]></comment>
<field id="version" translate="label" type="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Module Version</label>
</field>
Expand Down
4 changes: 2 additions & 2 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<default>
<postfinancecheckout_payment>
<information>
<version>1.0.71</version>
<sdk_version>2.0.9</sdk_version>
<version>1.0.72</version>
<sdk_version>2.0.10</sdk_version>
</information>
<general>
<base_gateway_url>https://www.postfinance-checkout.ch/</base_gateway_url>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PostFinanceCheckout_Payment" setup_version="1.0.71">
<module name="PostFinanceCheckout_Payment" setup_version="1.0.72">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
Expand Down
2 changes: 1 addition & 1 deletion i18n/de_DE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"Gift Wrap","Geschenkverpackung"
"Hold Delivery","Lieferung halten"
"ID required","ID erforderlich"
"If you need help setting up the PostFinance Checkout extension, check out the <a href=""https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.71/docs/en/documentation.html"" target=""_blank"">documentation</a>.","Falls Sie Hilfe benötigen beim Einrichten der PostFinance Checkout-Erweiterung, sehen Sie sich die <a href=""https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.71/docs/en/documentation.html"" target=""_blank"">Dokumentation</a> an."
"If you need help setting up the PostFinance Checkout extension, check out the <a href=""https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.72/docs/en/documentation.html"" target=""_blank"">documentation</a>.","Falls Sie Hilfe benötigen beim Einrichten der PostFinance Checkout-Erweiterung, sehen Sie sich die <a href=""https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.72/docs/en/documentation.html"" target=""_blank"">Dokumentation</a> an."
"Inactive","Inaktiv"
"Information","Informationen"
"Invoice","Rechnung"
Expand Down
2 changes: 1 addition & 1 deletion i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"Gift Wrap","Gift Wrap"
"Hold Delivery","Hold Delivery"
"ID required","ID required"
"If you need help setting up the PostFinance Checkout extension, check out the <a href=""https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.71/docs/en/documentation.html"" target=""_blank"">documentation</a>.","If you need help setting up the PostFinance Checkout extension, check out the <a href=""https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.71/docs/en/documentation.html"" target=""_blank"">documentation</a>."
"If you need help setting up the PostFinance Checkout extension, check out the <a href=""https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.72/docs/en/documentation.html"" target=""_blank"">documentation</a>.","If you need help setting up the PostFinance Checkout extension, check out the <a href=""https://plugin-documentation.postfinance-checkout.ch/pfpayments/magento-2.3/1.0.72/docs/en/documentation.html"" target=""_blank"">documentation</a>."
"Inactive","Inactive"
"Information","Information"
"Invoice","Invoice"
Expand Down

0 comments on commit 775d41f

Please sign in to comment.