diff --git a/src/Obol/Model/Enum/AggregateType.php b/src/Obol/Model/Enum/AggregateType.php new file mode 100644 index 00000000..7eafc0a1 --- /dev/null +++ b/src/Obol/Model/Enum/AggregateType.php @@ -0,0 +1,40 @@ +. + */ + +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, + }; + } +} diff --git a/src/Obol/Model/Enum/BillingType.php b/src/Obol/Model/Enum/BillingType.php new file mode 100644 index 00000000..78071a85 --- /dev/null +++ b/src/Obol/Model/Enum/BillingType.php @@ -0,0 +1,37 @@ +. + */ + +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, + }; + } +} diff --git a/src/Obol/Model/Enum/TierMode.php b/src/Obol/Model/Enum/TierMode.php new file mode 100644 index 00000000..9257dcee --- /dev/null +++ b/src/Obol/Model/Enum/TierMode.php @@ -0,0 +1,37 @@ +. + */ + +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, + }; + } +} diff --git a/src/Obol/Model/Enum/UsageType.php b/src/Obol/Model/Enum/UsageType.php new file mode 100644 index 00000000..3872a64e --- /dev/null +++ b/src/Obol/Model/Enum/UsageType.php @@ -0,0 +1,37 @@ +. + */ + +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, + }; + } +} diff --git a/src/Obol/Model/Price.php b/src/Obol/Model/Price.php index 48638c52..3aeccdb7 100644 --- a/src/Obol/Model/Price.php +++ b/src/Obol/Model/Price.php @@ -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; @@ -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; @@ -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; + } } diff --git a/src/Obol/Model/Tier.php b/src/Obol/Model/Tier.php new file mode 100644 index 00000000..b15f8d71 --- /dev/null +++ b/src/Obol/Model/Tier.php @@ -0,0 +1,61 @@ +. + */ + +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; + } +} diff --git a/src/Obol/Provider/Stripe/PriceService.php b/src/Obol/Provider/Stripe/PriceService.php index 09a8a78c..ce1a2fd9 100644 --- a/src/Obol/Provider/Stripe/PriceService.php +++ b/src/Obol/Provider/Stripe/PriceService.php @@ -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; @@ -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; } }