Skip to content

Commit

Permalink
[OBOL] Add more pricing data
Browse files Browse the repository at this point in the history
  • Loading branch information
that-guy-iain committed Sep 25, 2024
1 parent 48a8d18 commit a7f1fea
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Obol/Model/Enum/AggregateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2020-2024 Iain Cambridge
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Obol\Model\Enum;

enum AggregateType
{
case LAST_DURING_PERIOD;
case LAST_EVER;
case MAX;
case SUM;

public static function fromStripe(string $value): AggregateType
{
return match ($value) {
'last_during_period' => self::LAST_DURING_PERIOD,
'last_ever' => self::LAST_EVER,
'max' => self::MAX,
default => self::SUM,
};
}
}
37 changes: 37 additions & 0 deletions src/Obol/Model/Enum/BillingType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2020-2024 Iain Cambridge
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Obol\Model\Enum;

enum BillingType
{
case PER_UNIT;
case TIERED;

public static function fromStripe(?string $value): ?BillingType
{
return match ($value) {
'tiered' => self::TIERED,
'per_unit' => self::PER_UNIT,
default => null,
};
}
}
37 changes: 37 additions & 0 deletions src/Obol/Model/Enum/TierMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2020-2024 Iain Cambridge
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Obol\Model\Enum;

enum TierMode
{
case GRADUATED;
case VOLUME;

public static function fromString(?string $value): ?TierMode
{
return match ($value) {
'graduated' => self::GRADUATED,
'volume' => self::VOLUME,
default => null,
};
}
}
37 changes: 37 additions & 0 deletions src/Obol/Model/Enum/UsageType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2020-2024 Iain Cambridge
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Obol\Model\Enum;

enum UsageType
{
case METERED;
case PACKAGE;

public static function fromStripe(?string $value): ?UsageType
{
return match ($value) {
'metered' => self::METERED,
'licensed' => self::PACKAGE,
default => null,
};
}
}
53 changes: 53 additions & 0 deletions src/Obol/Model/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

namespace Obol\Model;

use Obol\Model\Enum\AggregateType;
use Obol\Model\Enum\BillingType;
use Obol\Model\Enum\TierMode;
use Obol\Model\Enum\UsageType;

class Price
{
protected ?string $id = null;
Expand All @@ -39,6 +44,14 @@ class Price

private ?string $schedule = null;

private ?TierMode $tierMode = null;

private ?BillingType $billingType = null;

private ?AggregateType $aggregateType = null;

private ?UsageType $usageType = null;

public function isIncludingTax(): bool
{
return $this->includingTax;
Expand Down Expand Up @@ -118,4 +131,44 @@ public function setSchedule(?string $schedule): void
{
$this->schedule = $schedule;
}

public function getTierMode(): ?TierMode
{
return $this->tierMode;
}

public function setTierMode(?TierMode $tierMode): void
{
$this->tierMode = $tierMode;
}

public function getBillingType(): ?BillingType
{
return $this->billingType;
}

public function setBillingType(?BillingType $billingType): void
{
$this->billingType = $billingType;
}

public function getAggregateType(): ?AggregateType
{
return $this->aggregateType;
}

public function setAggregateType(?AggregateType $aggregateType): void
{
$this->aggregateType = $aggregateType;
}

public function getUsageType(): ?UsageType
{
return $this->usageType;
}

public function setUsageType(?UsageType $usageType): void
{
$this->usageType = $usageType;
}
}
61 changes: 61 additions & 0 deletions src/Obol/Model/Tier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2020-2024 Iain Cambridge
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Obol\Model;

class Tier
{
private int $flatAmount;

private int $unitAmount;

private int $upTo;

public function getFlatAmount(): int
{
return $this->flatAmount;
}

public function setFlatAmount(int $flatAmount): void
{
$this->flatAmount = $flatAmount;
}

public function getUnitAmount(): int
{
return $this->unitAmount;
}

public function setUnitAmount(int $unitAmount): void
{
$this->unitAmount = $unitAmount;
}

public function getUpTo(): int
{
return $this->upTo;
}

public function setUpTo(int $upTo): void
{
$this->upTo = $upTo;
}
}
11 changes: 11 additions & 0 deletions src/Obol/Provider/Stripe/PriceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

use Obol\Exception\ProviderFailureException;
use Obol\Model\CreatePrice;
use Obol\Model\Enum\AggregateType;
use Obol\Model\Enum\BillingType;
use Obol\Model\Enum\TierMode;
use Obol\Model\Enum\UsageType;
use Obol\Model\Price;
use Obol\Model\PriceCreation;
use Obol\PriceServiceInterface;
Expand Down Expand Up @@ -122,6 +126,13 @@ public function populatePrice(\Stripe\Price $stripePrice): Price
$price->setSchedule($stripePrice->recurring?->interval);
$price->setIncludingTax('inclusive' === $stripePrice->tax_behavior);

if ($stripePrice->recurring?->aggregate_usage) {
$price->setAggregateType(AggregateType::fromStripe($stripePrice->recurring->aggregate_usage));
}
$price->setTierMode(TierMode::fromString($stripePrice->tiers_mode));
$price->setBillingType(BillingType::fromStripe($stripePrice->billing_scheme));
$price->setUsageType(UsageType::fromStripe($stripePrice->recurring?->usage_type));

return $price;
}
}

0 comments on commit a7f1fea

Please sign in to comment.