Skip to content

Commit

Permalink
recurrent payments: Drop support for quarterly, add daily and weekly
Browse files Browse the repository at this point in the history
  • Loading branch information
torotil committed Jan 4, 2024
1 parent 3cac240 commit 8f55573
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/PaymentExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
*/
class PaymentExporter {

/**
* Map interval units used by payment_recurrence to ISO 8601 interval units.
*/
const INTERVAL_UNITS = [
'monthly' => [1, 'M'],
'yearly' => [1, 'Y'],
'quarterly' => [3, 'M'],
'yearly' => 'Y',
'monthly' => 'M',
'daily' => 'D',
'weekly' => 'W',
];

/**
Expand Down Expand Up @@ -72,9 +76,8 @@ public function lineItemData(\PaymentLineItem $item) : array {
'recurrence_interval' => NULL,
];
if (($recurrence = $item->recurrence ?? NULL) && $recurrence->interval_unit) {
if ($f = static::INTERVAL_UNITS[$recurrence->interval_unit] ?? NULL) {
list($unit_factor, $unit) = $f;
$factor = $unit_factor * ($recurrence->interval_value ?? 1);
if ($unit = static::INTERVAL_UNITS[$recurrence->interval_unit] ?? NULL) {
$factor = $recurrence->interval_value ?? 1;
if ($factor > 0) {
$data['recurrence_interval'] = "P{$factor}{$unit}";
}
Expand Down
5 changes: 3 additions & 2 deletions tests/PaymentExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,11 @@ public function testLineItemData() {
$exporter = new PaymentExporter($this->createMock(SubmissionExporter::class));

$cases = [
['unit' => 'daily', 'value' => 7, 'interval' => 'P7D'],
['unit' => 'weekly', 'value' => 4, 'interval' => 'P4W'],
['unit' => 'monthly', 'value' => 1, 'interval' => 'P1M'],
['unit' => 'quarterly', 'value' => 1, 'interval' => 'P3M'],
['unit' => 'monthly', 'value' => 3, 'interval' => 'P3M'],
['unit' => 'monthly', 'value' => 6, 'interval' => 'P6M'],
['unit' => 'quarterly', 'value' => 2, 'interval' => 'P6M'],
['unit' => 'yearly', 'value' => 2, 'interval' => 'P2Y'],
['unit' => 'unknown', 'value' => 0, 'interval' => NULL],
['unit' => 'monthly', 'value' => 0, 'interval' => NULL],
Expand Down

0 comments on commit 8f55573

Please # to comment.