-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PISHPS-385: creditline items on refund (#884)
* PISHPS-385: refunds with just an amount and no reference to the line item * PISHPS-385: custom amount line item * PISHPS-385: stan fix
- Loading branch information
1 parent
54555e8
commit 72244a9
Showing
7 changed files
with
214 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Service\Refund; | ||
|
||
/** | ||
* Service to handle the calculation of refund amounts from line items. | ||
* This class provides functionality to sum up the refund amounts for a collection of line items. | ||
*/ | ||
class RefundSummarizationService | ||
{ | ||
/** | ||
* Calculates the total refund sum for a given list of line items. | ||
* | ||
* This method accepts an array of items, each containing an 'amount' field, and calculates the total refund amount | ||
* by summing the values of these fields. The 'amount' field is expected to be convertible to a float, and this method | ||
* will treat all values as floating-point numbers. | ||
* | ||
* @param array<int|string, mixed> $items Array of items, each containing an 'amount' field. | ||
* The 'amount' field should be convertible to a float. | ||
* @return float Total refund sum calculated from the 'amount' values in the provided items. | ||
*/ | ||
public function getLineItemsRefundSum(array $items): float | ||
{ | ||
// Extracts the 'amount' values from each item in the array | ||
$amounts = array_column($items, 'amount'); | ||
|
||
// Converts each extracted amount to a float to ensure accurate summation | ||
$amounts = array_map('floatval', $amounts); | ||
|
||
// Sums up all converted amounts and returns the result as the total refund amount | ||
return array_sum($amounts); | ||
} | ||
|
||
/** | ||
* @param array<int|string, mixed> $items | ||
* @param float $refundAmounts | ||
* @return float | ||
*/ | ||
public function customAmountInLineItems(array $items, float $refundAmounts): float | ||
{ | ||
// Extracts the 'amount' values from each item in the array | ||
$amounts = array_column($items, 'amount'); | ||
|
||
// Converts each extracted amount to a float to ensure accurate summation | ||
$amounts = array_map('floatval', $amounts); | ||
|
||
foreach ($amounts as $amount) { | ||
$refundAmounts -= $amount; | ||
} | ||
|
||
return (float) $refundAmounts; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/PHPUnit/Service/Refund/Fakes/RefundSummarizationServiceFake.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MolliePayments\Tests\Service\Refund\Fakes; | ||
|
||
|
||
use Kiener\MolliePayments\Service\Refund\RefundSummarizationService; | ||
|
||
class RefundSummarizationServiceFake extends RefundSummarizationService | ||
{ | ||
#[\Override] | ||
public function getLineItemsRefundSum(array $items): float | ||
{ | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
tests/PHPUnit/Service/Refund/RefundSummarizationServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MolliePayments\Tests\Service\Refund; | ||
|
||
|
||
use Kiener\MolliePayments\Service\Refund\RefundSummarizationService; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RefundSummarizationServiceTest extends TestCase | ||
{ | ||
/** | ||
* @var RefundSummarizationService | ||
*/ | ||
private $service; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->service = new RefundSummarizationService(); | ||
} | ||
|
||
/** | ||
* @dataProvider summarizeDataProvider | ||
*/ | ||
public function testCanSummarize(float ...$values): void | ||
{ | ||
$expected = 0; | ||
$dataSet = []; | ||
|
||
foreach ($values as $value) { | ||
$dataSet[] = ['amount' => $value]; | ||
$expected += $value; | ||
} | ||
|
||
$actual = $this->service->getLineItemsRefundSum($dataSet); | ||
|
||
static::assertEquals($expected, $actual); | ||
} | ||
|
||
public static function summarizeDataProvider(): array | ||
{ | ||
return [ | ||
'single value' => [10.0], | ||
'multiple values' => [10.0, 20.0, 30.0], | ||
]; | ||
} | ||
} |